C语言中的文件描述符
让我们假设在我的操作系统中存在N个文件描述符。执行以下代码后,操作系统将有多少个文件描述符:C语言中的文件描述符,c,file,posix,C,File,Posix,让我们假设在我的操作系统中存在N个文件描述符。执行以下代码后,操作系统将有多少个文件描述符: int fd = dup(oldfd); 那么: int fd = dup2(oldfd,newfd); 谢谢 它在手册页中给出。调用任意一个文件描述符后,您将拥有N+1文件描述符 From manpages ... dup and dup2 create a copy of the file descriptor oldfd. After successfu
int fd = dup(oldfd);
那么:
int fd = dup2(oldfd,newfd);
谢谢 它在手册页中给出。调用任意一个文件描述符后,您将拥有
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.
...
因此,使用未使用的描述符作为新描述符。这应该可以回答您的问题。手册中给出了答案。调用任意一个文件描述符后,您将拥有
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.
...
因此,使用未使用的描述符作为新描述符。这应该能回答你的问题。你为什么不自己试试呢?这是家庭作业吗?听起来像是我的系统编程课。这不是家庭作业,我的电脑上也没有安装c编译器。你为什么不自己试试呢?这是家庭作业吗?听起来像是我的系统编程课。这不是家庭作业,我也没有在pc上安装c编译器。