1

我正在尝试创建一组无限的管道来从左进程遍历到右进程。我正在使用 fd 来保留以前的 fd 并将其输入到新进程中。任何人都可以看到我要去哪里错了。在这一点上应该很容易看到。我记录得很好。

    //Keep the previous out fd for the in of the subsequent process
    int prev_out_fd;

    for (x = 0; x < prog_count; ++x)
    {

        //Create a pipe for both processes to share
        int pipefd[2];

        if (x != prog_count -1)
        {
            pipe(pipefd);
        }

        prog_defs[x].pid = fork();

        if(prog_defs[x].pid == 0)
        {

            //If this is the first process we don't need a read end
            if (x == 0)
            {
                close(pipefd[0]);
            }

            //If this is not the first process, set the input to the output of the previous pipe
            if (x != 0)
            {
                dup2(prev_out_fd, STDIN_FILENO);

                //Pipe now garbage.  Get rid of it.
                close (prev_out_fd);
            }



            if(x != prog_count - 1)
            {
                dup2(pipefd[1], STDOUT_FILENO);
                close(pipefd[0]);
                close(pipefd[1]);
            }


            execvp(prog_defs[x].bin, prog_defs[x].args);
        }

        if (x != 0)
            close(prev_out_fd);

        prev_out_fd = pipefd[0];
        close(pipefd[1]);

    }
4

0 回答 0