[英]What happens when you call close() on a pipe file descriptor that was duplicated with dup2()?
I have a question regarding file descriptors in Unix and C programming.我有一个关于 Unix 和 C 编程中的文件描述符的问题。
Let's say I use pipe(fd)
to get file descriptor 3 and 4 for the pipe ends, 3 connects to the read end and 4 to the write end.假设我使用
pipe(fd)
来获取管道末端的文件描述符 3 和 4,3 连接到读取端,4 连接到写入端。
Now I use dup2(fd[write_end],1)
to copy the descriptor of the write end (which was 4) to file descriptor 1 in my process.现在我使用
dup2(fd[write_end],1)
将写端的描述符(为 4)复制到我的进程中的文件描述符 1。 If I now do close(fd[write_end])
will it close descriptor 1 or descriptor 4?如果我现在执行
close(fd[write_end])
会关闭描述符 1 还是描述符 4?
After a successful call to dup2
, both file descriptors are valid.成功调用
dup2
,两个文件描述符都有效。
When you then call close(fd[write_end])
, because fd[write_end]
is set to 4 this is the same as close(4)
.当您随后调用
close(fd[write_end])
,因为fd[write_end]
设置为 4,这与close(4)
相同。 So file descriptor 1 remains open and usable.所以文件描述符 1 保持打开和可用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.