我试图创建一个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]);
}