我有一个问题,这个代码,我怎么能发送一个数字在这个过程中,像1发送到2,2发送到3,3发送到1,每次减少一个I*10,像第一次10,第二次20,…30。。。直到数字为负数,然后停止程序?
我做了这个代码,但是我有一个问题,数字'a'的值不能像a=1342那样在1中减少20,在2中需要1322,它是从1342开始的。
这是我的代码:
#include <sys/types.h>
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
int i=0;
int main()
{
pid_t p;
int a=1324;
int fd[2];
int fd1[2];
pipe(fd);
p=fork():
if(p < 0)
{
perror("Fork error");
exit(1);
}
else if(p==0)
{
while(1)
{
close(fd1[1]);
read(fd1[0],&a, sizeof(int));
printf("Procces 2, a is %d.\n",a);
wait(1);
close(fd[0]);
a=a-(i*10);
i++;
write(fd[1],&a,sizeof(int));
}
}
else
{
p=fork();
if(p < 0)
{
perror("Fork error");
exit(1);
}
if(p > 0)
{
while(1)
{
close(fd1[1]);
read(fd1[0],&a, sizeof(int));
printf("Procces 1, a is %d.\n",a);
wait(1);
close(fd1[0]);
a=a-(i*10);
i++;
write(fd1[1],&a,sizeof(int));
}
}
else
{
while(1)
{
close(fd[1]);
read(fd[0],&a, sizeof(int));
printf("Procces 3, a is %d.\n",a);
wait(1);
close(fd1[0]);
a=a-(i*10);
i++;
write(fd1[1],&a,sizeof(int));
}
}
}
return 1;
}
我想这样打印:
进程1,a是1324(减少0并发送到2)
流程2,a为1324(减少10,发送到3)
过程3,a是1314(减少20并发送到1)
流程1,a为1294(减少30并发送到2)
...
但程序有点像这样:
流程1,a is 1324
流程2,a is 1324
流程3,a is 1324
流程1,a is 1314
...
有人能告诉我问题在哪里?谢谢。
最佳答案
如果这不是你想接近的。您需要清楚地标识每个进程使用哪些管道进行写入和读取。我发现最简单的方法就是用宏标记索引。例如。六根管子(三对)的顺序:
// This arrangement means;
//
// P1 listens to P2, writes to P3
// P2 listens to P3, writes to P1
// P3 listens to P1, writes to P2
#define P1_READ 0
#define P2_WRITE 1
#define P2_READ 2
#define P3_WRITE 3
#define P3_READ 4
#define P1_WRITE 5
#define NUM_PIPES 6
这使得客户机代码更容易理解:
int main()
{
int a = 1324;
int i=1;
int fd[NUM_PIPES];
// create pipes
if (pipe(fd) < 0 || pipe(fd+2) < 0 || pipe(fd+4) < 0)
{
perror("Failed to create pipes");
exit(EXIT_FAILURE);
}
// fork P2 child process
if (fork() == 0)
{
close(fd[P1_READ]);
close(fd[P1_WRITE]);
close(fd[P3_READ]);
close(fd[P3_WRITE]);
while (read(fd[P2_READ], &a, sizeof(a)) == sizeof(a))
{
fprintf(stderr, "P2: (i==%d) received %d\n", i, a);
if ((a -= (i++ * 10)) < 0)
break;
write(fd[P2_WRITE], &a, sizeof(a));
}
// close our other pipes
close(fd[P2_READ]);
close(fd[P2_WRITE]);
return EXIT_SUCCESS;
}
////////////////////////////////////////////////////////////////
// fork P3 child process
if (fork() == 0)
{
close(fd[P1_READ]);
close(fd[P1_WRITE]);
close(fd[P2_READ]);
close(fd[P2_WRITE]);
while (read(fd[P3_READ], &a, sizeof(a)) == sizeof(a))
{
fprintf(stderr, "P3: (i==%d) received %d\n", i, a);
if ((a -= (i++ * 10)) < 0)
break;
write(fd[P3_WRITE], &a, sizeof(a));
}
// close our other pipes
close(fd[P3_READ]);
close(fd[P3_WRITE]);
return EXIT_SUCCESS;
}
////////////////////////////////////////////////////////////////
// parent process. close the pipes we don't need
close(fd[P2_READ]);
close(fd[P2_WRITE]);
close(fd[P3_READ]);
close(fd[P3_WRITE]);
// kick things off with a write of the first value
write(fd[P1_WRITE], &a, sizeof(a));
// same code as before
while (read(fd[P1_READ], &a, sizeof(a)) == sizeof(a))
{
fprintf(stderr, "P1: (i==%d) received %d\n", i, a);
if ((a -= (i++ * 10)) < 0)
break;
write(fd[P1_WRITE], &a, sizeof(a));
}
// close the pipes we no longer need
close(fd[P1_READ]);
close(fd[P1_WRITE]);
wait(NULL);
return EXIT_SUCCESS;
}
结果输出如下所示:
P3: (i==1) received 1324
P2: (i==1) received 1314
P1: (i==1) received 1304
P3: (i==2) received 1294
P2: (i==2) received 1274
P1: (i==2) received 1254
P3: (i==3) received 1234
P2: (i==3) received 1204
P1: (i==3) received 1174
P3: (i==4) received 1144
P2: (i==4) received 1104
P1: (i==4) received 1064
P3: (i==5) received 1024
P2: (i==5) received 974
P1: (i==5) received 924
P3: (i==6) received 874
P2: (i==6) received 814
P1: (i==6) received 754
P3: (i==7) received 694
P2: (i==7) received 624
P1: (i==7) received 554
P3: (i==8) received 484
P2: (i==8) received 404
P1: (i==8) received 324
P3: (i==9) received 244
P2: (i==9) received 154
P1: (i==9) received 64
在你的闲暇时间玩弄数字(
i
,a
,)。希望有帮助。