问题
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