以下のコードでは、プロセスは 1 つの子 ( fork() ) を作成し、次に子はexec()を呼び出して自分自身を置き換えます。execのstdoutは、シェルではなくパイプに書き込まれます。次に、親プロセスはパイプから exec が書き込んだもの を読み取ります while (read(pipefd[0], buffer, sizeof(buffer)) != 0)
誰かが上記とまったく同じことを行う方法を教えてもらえますか? ただし、N 個の子プロセス (上記のように自分自身を exec に置き換えます) を使用します。
int pipefd[2];
pipe(pipefd);
if (fork() == 0)
{
    close(pipefd[0]);    // close reading end in the child
    dup2(pipefd[1], 1);  // send stdout to the pipe
    dup2(pipefd[1], 2);  // send stderr to the pipe
    close(pipefd[1]);    // this descriptor is no longer needed
    exec(...);
}
else
{
    // parent
    char buffer[1024];
    close(pipefd[1]);  // close the write end of the pipe in the parent
    while (read(pipefd[0], buffer, sizeof(buffer)) != 0)
    {
    }
}