我试图理解这行代码的含义:
int dfd;
fd2 = fcntl(dfd, F_DUPFD);
最佳答案
它旨在将 fd2
设置为另一个文件描述符,该文件描述符引用与 dfd
相同的打开文件记录。
应该有第三个参数,即要返回的最低可接受文件描述符。由于缺少第三个参数,调用可能会做任何事情。正确的调用可能如下所示:
fd2 = fcntl(dfd, F_DUPFD, 0);
来自fcntl
man page :
F_DUPFD
(int)Find the lowest numbered available file descriptor greater than or equal to
arg
and make it be a copy offd
. This is different fromdup2
(2), which uses exactly the descriptor specified.On success, the new descriptor is returned.
See
dup
(2) for further details.
关于c - Linux 系统调用中 fcntl 中 F_DUPFD 的用法是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46397567/