博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Linux编程-fcntl

Posted on 2023-03-17 09:54  乔55  阅读(12)  评论(0编辑  收藏  举报

fcntl详解

int fcntl(int fd, int cmd, ... /* arg */ );

fcntl获取flag

void test()
{
    // 获取文件flags标志
    int flags = fcntl(fd, F_GETFL);
    if(flags == -1)
    {
        perror("fcntl F_GETFL failed:");
        exit(1);
    }

    // ״设置文件的flags标志
    flags |= O_CREAT;
    int ret = fcntl(fd, F_SETFL, flags);
    if(ret == -1)
    {
        perror("fcntl F_SETTL failed");
        exit(1);
    }
}

fcntl实现fd拷贝

int newfd = fcntl(oldfd, F_DUPFD, 0);
// 第3参数0的意思:0被占用,fcntl使用文件描述符表中的最小可用fd

int newfd = fcntl(oldfd, F_DUPFD, 20);
// 指定20时,20未被占用,返回==20的文件描述符