What happens when you call close() on a pipe file descriptor that was duplicated with dup2()?

两盒软妹~` 提交于 2021-02-17 04:52:34

问题


I have a question regarding file descriptors in Unix and C programming.

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.

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. If I now do close(fd[write_end]) will it close descriptor 1 or descriptor 4?


回答1:


After a successful call to dup2, both file descriptors are valid.

When you then call close(fd[write_end]), because fd[write_end] is set to 4 this is the same as close(4). So file descriptor 1 remains open and usable.



来源:https://stackoverflow.com/questions/52897615/what-happens-when-you-call-close-on-a-pipe-file-descriptor-that-was-duplicated

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!