c - Linux 系统调用中 fcntl 中 F_DUPFD 的用法是什么

标签 c linux posix

我试图理解这行代码的含义:

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 of fd. This is different from dup2(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/

相关文章:

php - Codeigniter 类 cron 不起作用

条件变量 - 意外行为

c - 如何从信号编号中获取人类可读的描述?

linux - 有没有办法只编译 POSIX API

c - 文件中的垃圾字符

arrays - 如何用C实现埃拉托色尼筛法算法?

c++ - 强制将文件写入磁盘

linux - 我正在寻找一个工具来跟踪我的服务器,我们到处都使用 Chef-solo,但很难跟踪

C 从实时文件中读取(文件大小不断增加)

c - 二维字符数组出现问题,程序无法打印