![](/img/trans.png)
[英]After a pipe() and fork(), why does close(fd[1]) in the parent close the *child's* fd[1]?
[英]c pipe why is fd[0] and fd[1] 3 and 4
我下面有一个代码段。 我想看看fd1和fd0的价值是什么,在我创建的所有过程中它们最终都是3和4? 为什么是这样。
if (pipe(fd) < 0)
printf("Pipe Error");
if ((pid = fork()) < 0 )
{
printf("Fork Error");
}
else if (pid > 0) //daddy
{
close (fd[0]);
write(fd[1],"Hi, Im Parent \n", 15);
printf("Value of fd1 is %d and fd0 is %d in parent. \n", fd[1], fd[0]);
printf("Parent Process is %d \n", pid);
printf("My true id is: %d and my parent id is %d \n", getpid(), getppid());
wait();
}
文件描述符 (从open
或pipe
类的函数返回,尽管以不同的方式返回)是代表打开文件的小整数。
它们与文件句柄的概念不同,文件句柄是标准C中指向FILE
类型的指针(我使用术语“句柄”,尽管标准本身只是简单地声明了它将指针与流相关联并返回该指针)。
因此,你所期望的一样,你所看到的,行为pipe()
抓住当前未使用的前两个描述符。
文件描述符0
, 1
和2
,分别指的是标准输入,标准输出和标准误差,类似于FILE *
值stdin
, stdout
和stderr
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.