这是我要执行的某些管道的代码示例。问题是pid2 [0]没有为我提供子进程。我如何解决它? pid2 [1]和pid2 [2]等为父项提供子项。

int numCommands = numPipes + 1;      /// not worrying about '>' and '<' right now
int *pipes = newint[numPipes*2];     /// two ends for each pipe
for(int i = 0; i < numPipes*2; i+=2) /// Offset by two since each pipe has two ends
   pipe(pipes + i);
int *pid2 = new int [numCommands];

for(int i = 0; i < numCommands; i++)
{
   pid2[i] = fork();
   if(pid2[i] < 0)
   {
      std::cerr << "Failure to fork..." << std:endl;
      return EXIT_FAILURE
   }

   if(pid2[i] == 0)    /// Child process
   {
      if(i == 0)       /// First Command
      {
         dup2(pipes[1], 1);
      }

      else if(i == numCommands -1)  /// Last Command
      {
         dup2(pipes[2*(numCommands-1)-1], 0);
      }

      else             /// Middle commands
      {
         dup2(pipes[2*(i-1)], 0);
         dup2(pipes[(2*i)+1],1);
      }
      for(int j = 0; j < numPipes*2;j++)
         close(pipes[j]);
      execvp(pipeCommands[i][0], pipeCommands[i].data());  ///pipeCommands is a vector<vector<char*>>
      perror("exec failed");
      return EXIT_SUCCESS;
   }
   else        /// The parent
   {
      for(int j = 0; j <numPipes*2;j++)
          close(pipes[j]);
      for(int k = 0; k < numCommands; k++)
         waitpid(pid2[k],nullptr,0);
   }
}

最佳答案

您索引到pipes数组有点问题。对于sort file.txt | head | wc,我假设numPipes是2。让我们遍历每个i值的for循环。

i == 0

dup2(pipes[1], 1); // Send stdout to pipes[1]


i == 1

dup2(pipes[2*(i-1)], 0); // dup2(pipes[0], 0), stdin from pipes[0]
dup2(pipes[(2*i)+1],1);  // dup2(pipes[3], 1), stdout to pipes[3]


我== 2

dup2(pipes[2*(numCommands-1)-1], 0); //dup2(pipes[3], stdin from pipes[3]


换句话说,来自进程0的任何标准输出都将进入死胡同。第二个进程将永远不会从其标准输出中读取。因此,您放入其中的调试语句(cout == stdout,请记住)也会丢失。

关于linux - 循环fork(),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35821547/

10-11 18:39