问题
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