Infinite pipe insanity

落花浮王杯 提交于 2020-01-05 08:58:38

问题


I'm trying to create an infinte set of pipes to traverse from the left process to the right process. I'm using a fd to keep the previous out fd and input it to the new process. Can anyone see where I'm going wrong. It should be pretty simple to see at this point. I documented well.

    //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]);

    }

来源:https://stackoverflow.com/questions/14780894/infinite-pipe-insanity

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!