无限管疯狂

时间:2013-02-08 20:35:01

标签: c posix pipe

我正在尝试创建一组infinte管道,以便从左进程遍历到正确进程。我正在使用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]);

    }

0 个答案:

没有答案