以下是一般 pipe()
用法示例:
int fd[2];
pipe(fd);
if((childpid = fork()) == -1)
{
perror("fork");
exit(1);
}
if(childpid == 0)
{
/* Child process closes up input side of pipe */
close(fd[0]);
////.....some code....////
exit(0);
}
else
{
/* Parent process closes up output side of pipe */
close(fd[1]);
////.....some code....////
}
我想知道的是是否需要调用 close(fd[0])
以及父进程中的close(fd[1])
。
如果我 close()
他们只会在子项中使用fd[1]
而在父项中使用fd[0]
会发生什么情况。
关闭它只是为了让我们不小心对那些描述符做一些吗?
答案 0 :(得分:3)
如果两个进程都打开了管道的两端,如果一个管理器死了,另一个进程将死锁而不是在读取时检测到EOF(因为还有一个编写器本身)或者在写入时被SIGPIPE杀死(因为它仍然存在)读者:本身)。