0
pid_t kids[argc];
int childCount = argc - 1;
int fd[2];
/* create the pipe*/
if (pipe(fd) == -1) {
    fprintf(stderr ,"Pipe failed");
    return 1;
for(i=0; i < childCount; i++) {
   kids[i] = fork();
   if(kids[i] < 0){
      //fork fail
   }
   else if(kids[i] == 0) {
     /* child process */
        sum = max + min;//sum and dif are results from each child process
        dif = max - min;
        /* close the unused end of pipe, in this case, the read end */
        close(fd[READ_END]);
        /* write to the pipe */
        write(fd[WRITE_END], &sum, sizeof(sum));
        write(fd[WRITE_END], &dif, sizeof(dif));
        /* close write end */
        close(fd[WRITE_END]);
        exit(0);
   }
   else {
        waitpid(kids[i], &status, 0);
        close(fd[WRITE_END]);

        read(fd[READ_END], &sum, sizeof(float));
        read(fd[READ_END], &dif, sizeof(float));
        close(fd[READ_END]);
   }
}

以上是代码,稍微简化了一点。
我想要做的是等待任何孩子完成并处理其数据,然后重复此操作直到所有孩子都完成。
有人可以告诉我如何将孩子生成的数据传递给父母吗?

4

1 回答 1

0

您没有提到当前代码的问题是什么,并且您显示的代码无法编译,所以我只能猜测这是您真实代码的近似值。

然而,这就是引起我注意的地方:

你永远不会在循环之前关闭主体if (pipe(fd) == -1),这意味着循环是if主体的一部分。这可能不是您想要的,尽管我不确定这是否是拼写错误/复制粘贴错误。也许在真正的代码中你确实关闭了if.

父进程的代码是错误的:因为它在for循环内运行,你反复调用close(2)管道的写入和读取端。这将导致在循环的第二次迭代时close(2)返回一个错误(EBADF),您公然忽略它。同样在第二次迭代中,分叉的子进程将尝试关闭不再存在的管道的读取通道(因为我们刚刚分叉的父进程在上一次迭代中关闭了两端,然后再次分叉),然后它尝试写入不存在的管道。

要修复它,您必须确保父母在每个孩子都完成之前不会关闭管道。不要close(2)在父循环里面;相反,在循环之后执行:

for(i=0; i < childCount; i++) {
   kids[i] = fork();
   if(kids[i] < 0){
      //fork fail
   }
   else if(kids[i] == 0) {
     /* child process */
        sum = max + min;//sum and dif are results from each child process
        dif = max - min;
        /* close the unused end of pipe, in this case, the read end */
        close(fd[READ_END]);
        /* write to the pipe */
        write(fd[WRITE_END], &sum, sizeof(sum));
        write(fd[WRITE_END], &dif, sizeof(dif));
        /* close write end */
        close(fd[WRITE_END]);
        exit(0);
   }
   else {
        waitpid(kids[i], &status, 0);
        read(fd[READ_END], &sum, sizeof(float));
        read(fd[READ_END], &dif, sizeof(float));
   }
}

close(fd[READ_END]);
close(fd[WRITE_END]);

由于您等待每个孩子在循环中终止,因此可以保证当仍有活动的编写者时您不会关闭管道。

于 2015-06-21T20:24:39.130 回答