我有以下简化的代码模板:
pid_t pid;
int pipe1[2], pipe2[2];
pid = fork();
pipe(pipe1); pipe(pipe2)
if(pid == 0) //child
{
read(pipe1[0],...);
write(pipe1[1],...);
close(pipe1[0]);
close(pipe1[1]);
close(pipe2[1]);
read(pipe2[0]...);
}
else //parent
{
write(pipe1[1],...);
wait(NULL);
read(pipe1[0]...);
close(pipe1[0]);
close(pipe1[1]);
close(pipe2[0]);
write(pipe2[1]...);
}
如果我不在父母和孩子中使用 pipe2,则代码可以完美运行,但如果我这样做,孩子似乎没有什么可读的(程序什么都不做,直到我插入它)。另外,有没有办法只使用一个管道进行 2 次以上的读/写?我尝试多次使用 wait(NULL) ,但没有奏效。