C 如何判断首先写入哪个管道

C 如何判断首先写入哪个管道,c,process,pipe,fork,C,Process,Pipe,Fork,因此,我有一些代码,我正在工作,我将叉N个孩子做一些事情与N个不同的文件和收集一些信息。父母只是在读书,孩子们只是在写字。如果一个子系统在另一个子系统之前完成,我希望在父系统中开始处理该数据,而其他子系统仍在运行 在这里,我正在制作N根管子 int pipefds[nFiles*2]; int k; for(k = 0; k < nFiles; k++) { if(pipe(pipefds[k*2])) { /* pipe failed */ } } 然

因此,我有一些代码,我正在工作,我将叉N个孩子做一些事情与N个不同的文件和收集一些信息。父母只是在读书,孩子们只是在写字。如果一个子系统在另一个子系统之前完成,我希望在父系统中开始处理该数据,而其他子系统仍在运行

在这里,我正在制作N根管子

int pipefds[nFiles*2];
int k;
for(k = 0; k < nFiles; k++)
{
    if(pipe(pipefds[k*2]))
    {
     /* pipe failed */
    }
}
然后我fork N个进程,并希望它们对该文件进行处理并将其发送给父进程

int i;
for(i = 0; i < nFiles; i++)
{
  pid = fork();
  if(pid < 0)
  {
    /* error */
  }
  else if(pid == 0)
  {
    /*child */

    close(pipefds[i*2]); //I think I want to close the Read End of each child's pipe
    getData(file[i]); // do something with the file this process is handling
    write(fd[i*2 +1], someData, sizeof(someData); // write something to the write end of the child's pipe
    exit(0);
  }
  else
  {
   /*parent*/
   if(i = nFiles -1) //do I have to make this condition so that I start once all processes have been started??
    {
      int j;
      for(j = 0; j < nFiles; j++)
      {
       close(pipefds[j*2+1]); //close all the parents write ends
      }
      /*Here I want to pick the pipe that finished writing first and do something with it */
    } 
  }

我是否必须等待for循环的最后一次迭代开始执行父级内容,因为我希望在执行任何操作之前启动所有流程?还有,我如何在pipefds中找到已完成编写的管道,以便在其他管道运行时开始处理它?谢谢

最简单的解决方案可能是首先创建所有子对象。然后围绕poll或select运行单独的循环。当您在其中一个管道端获得读取命中时,将数据读入缓冲区。如果该读取得到管道另一端已关闭的指示,则可以开始处理来自该子级的数据。不要忘记从正在轮询或选择的集合中删除该管道。当所有儿童管道都关闭时,您就完成了。

C,很抱歉造成混乱