当您对与dup2()复制的管道文件描述符调用close()时,会发生什么情况? [英] What happens when you call close() on a pipe file descriptor that was duplicated with dup2()?

查看:77
本文介绍了当您对与dup2()复制的管道文件描述符调用close()时,会发生什么情况?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Unix和C编程中的文件描述符有疑问.

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

假设我使用 pipe(fd)来获取文件描述符3和4的管道端,3连接到读取端,4连接到写入端.

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.

现在,我在处理过程中使用 dup2(fd [write_end],1)将写入端的描述符(为4)复制到文件描述符1.如果我现在执行 close(fd [write_end]),它将关闭描述符1或描述符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. If I now do close(fd[write_end]) will it close descriptor 1 or descriptor 4?

推荐答案

成功调用 dup2 后,两个文件描述符均有效.

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

当您随后调用 close(fd [write_end])时,由于 fd [write_end] 设置为4,因此与 close(4)相同.因此文件描述符1保持打开和可用状态.

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.

这篇关于当您对与dup2()复制的管道文件描述符调用close()时,会发生什么情况?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆