gpt4 book ai didi

c - 使用管道读/写值

转载 作者:太空宇宙 更新时间:2023-11-04 04:43:24 24 4
gpt4 key购买 nike

我对这段代码有疑问,我如何在这个过程之间发送一个数字,比如 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为1324
  • 进程2,a为1324
  • 进程3,a为1324
  • 进程1,a为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,) 以消遣。希望对您有所帮助。

关于c - 使用管道读/写值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23714659/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com