C中的文件描述符

时间:2012-02-29 18:39:57

标签: c file posix

让我们假设在我的操作系统中存在N个文件描述符。执行以下代码后操作系统将拥有多少个文件描述符:

    int fd = dup(oldfd);

怎么样:

   int fd = dup2(oldfd,newfd);

谢谢!

1 个答案:

答案 0 :(得分:3)

在手册页中给出了它。在调用其中一个文件后,您将拥有N+1个文件描述符。

From manpages
    ...
    dup and dup2 create a copy of the file descriptor oldfd.

    After successful return of dup or dup2, the old and new descriptors 
    may be used interchangeably. 

    dup uses the lowest-numbered unused descriptor for the new descriptor.
    ...

因此,未使用的描述符用作新的描述符。这应该回答你的问题。

相关问题