gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 08:01:29 25 4
gpt4 key购买 nike

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

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com