Linux fcntl函数

devtools/2024/10/19 11:42:15/

fcntl函数

读法:file control函数
作用:复制文件描述符、设置/获取文件的状态标志

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

fd是文件描述符,cmd是命令(定义的宏),… 表示可变的参数(可有可不有)

man 2 fcntl

DESCRIPTION:
fcntl() performs one of the operations described below on the open file descriptor fd. The operation is determined by cmd.
1、Duplicating a file descriptor 复制文件描述符

  • F_DUPFD (int)
    Duplicate the file descriptor fd using the lowest-numbered available file descriptor greater than or equal to arg. This is different from dup2(2), which uses exactly the file descriptor specified. (dup2复制指定的fd,而F_DUPFD用的是lowerst)On success, the new file descriptor is returned.

  • F_DUPFD_CLOEXEC (int; since Linux 2.6.24)

2、 File descriptor flags 文件描述标志
The following commands manipulate the flags associated with a file descriptor. Currently, only one such flag is defined: FD_CLOEXEC, the close-on-exec flag. If the FD_CLOEXEC bit is set, the file descriptor will automatically be closed during a successful execve(2). (If the execve(2) fails, the file descriptor is left open.) If the FD_CLOEXEC bit is not set, the file descriptor will remain open across an execve(2).

  • F_GETFD (void)

  • F_SETFD (int)

3、 File status flags 文件状态标志
Each open file description has certain associated status flags, initialized by open(2) and possibly modified by fcntl(). Duplicated file descriptors (made with dup(2), fcntl(F_DUPFD), fork(2), etc.) refer to the same open file description, and thus share the same file status flags.

- F_GETFL (void) 获取指定的文件描述符文件状态flag
Return (as the function result) the file access mode and the file status flags; arg is ignored.

- F_SETFL (int) 设置文件描述符文件状态flag
Set the file status flags to the value specified by arg. File access mode (O_RDONLY, O_WRONLY, O_RDWR) and file creation flags (i.e., O_CREAT, O_EXCL, O_NOCTTY, O_TRUNC) in arg are ignored. On Linux, this command can change only the O_APPEND, O_ASYNC, O_DIRECT, O_NOATIME, and O_NONBLOCK flags. It is not possible to change the O_DSYNC and O_SYNC flags.

4、…

总结:

#include <unistd.h>
#include <fcntl.h>int fcntl(int fd, int cmd, ...);
参数:fd : 表示需要操作的文件描述符cmd: 表示对文件描述符进行如何操作- F_DUPFD : 复制文件描述符,复制的是第一个参数fd,得到一个新的文件描述符(返回值)int ret = fcntl(fd, F_DUPFD);- F_GETFL : 获取指定的文件描述符文件状态flag获取的flag和我们通过open函数传递的flag是一个东西。- F_SETFL : 设置文件描述符文件状态flag必选项:O_RDONLY, O_WRONLY, O_RDWR 不可以被修改可选性:O_APPEND, O_NONBLOCKO_APPEND 表示追加数据O_NONBLOK 设置成非阻塞阻塞和非阻塞:描述的是函数调用的行为。阻塞函数:调用add函数,进程被发起,并且得到结果之后才会返回。比如终端等待你输入的状态非阻塞函数:调用函数时立马就能返回,不会阻塞当前的进程。比如输入命令后终端立马执行。

测试:

#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>int main() {// 1.复制文件描述符// int fd = open("1.txt", O_RDONLY);// int ret = fcntl(fd, F_DUPFD); //复制描述符// 2.修改或者获取文件状态flagint fd = open("1.txt", O_RDWR); // 这里是RDWR,不能是只有读权限RDONLY,O_APPEND只是往后面追加if(fd == -1) {perror("open");return -1;}// 获取文件描述符状态flagint flag = fcntl(fd, F_GETFL);if(flag == -1) {perror("fcntl");return -1;}flag |= O_APPEND;   // flag = flag | O_APPEND  在原先flag基础上加入O_APPEND// 修改文件描述符状态的flag,给flag加入O_APPEND这个标记int ret = fcntl(fd, F_SETFL, flag); //flag不能直接写O_APPEND,会直接覆盖掉用来的O_RDWRif(ret == -1) {perror("fcntl");return -1;}char * str = "nihao";write(fd, str, strlen(str));close(fd);return 0;
}

运行前后:

1
1nihao

http://www.ppmy.cn/devtools/23990.html

相关文章

Android kotlin 协程异步async与await介绍与使用

一、介绍 在kotlin语言中&#xff0c;协程是一个处理耗时的操作&#xff0c;但是很多人都知道同步和异步&#xff0c;但是不知道该如何正确的使用&#xff0c;如果处理不好&#xff0c;看似异步&#xff0c;其实在runBloacking模块中使用的结果是同步的。 针对如何同步和如何异…

【源码】Spring validation参数校验实现原理总结

Spring validation参数校验系列 1、Spring validation参数校验基本使用 2、Spring validation参数校验之自定义校验规则及编程式校验等进阶篇 3、【源码】Spring validation参数校验原理解析之Controller控制器参数校验中RequestBody参数校验实现原理 4、【源码】Spring va…

c++高级篇(三) ——Linux下IO多路复用之poll模型

poll模型 前言 poll模型与select的实现原理相近&#xff0c;所以绝大数的原理其实可以参考select&#xff0c;我们这里对二者的相同点不做过多探究&#xff0c;如果有需要可以去看一下博主的上一篇文章&#xff1a; c高级篇(二) ——Linux下IO多路复用之select模型 这里我们只…

Jmeter插件技术:性能测试中服务端资源监控

性能测试过程中我们需要不断的监测服务端资源的使用情况&#xff0c;例如CPU、内存、I/O等。 Jmeter的插件技术可以很好的实时监控到服务器资源的运行情况&#xff0c;并以图形化的方式展示出来&#xff0c;非常方便我们性能测试分析。 操作步骤&#xff1a; 1、安装插件管理…

封装形式,进化,DIP封装及键出方法

本文主要讨论芯片封装的主要形式&#xff0c;概念&#xff0c;以及芯片封装的演化&#xff0c;最后以DIP封装为例&#xff0c;分析键出方式。 1-IC封装的形式 IC 封装是指将组成电子器件的各个组成部分&#xff0c;包括半导体芯片、基板、管脚连接线等&#xff0c;按照要求布局…

力扣(leetcode) 407. 接雨水 II 3D接雨水

力扣(leetcode) 407. 接雨水 II 3D接雨水 给你一个 m x n 的矩阵&#xff0c;其中的值均为非负整数&#xff0c;代表二维高度图每个单元的高度&#xff0c;请计算图中形状最多能接多少体积的雨水。 示例 1: 输入: heightMap [[1,4,3,1,3,2],[3,2,1,3,2,4],[2,3,3,2,3,1]] 输…

【C语言】文件操作

1、为什么使用文件操作&#xff1f; 如果没有⽂件&#xff0c;我们写的程序的数据是存储在电脑的内存中&#xff0c;如果程序退出&#xff0c;内存回收&#xff0c;数据就丢失了&#xff0c;等再次运⾏程序&#xff0c;是看不到上次程序的数据的&#xff0c;如果要将数据进⾏持…

手撕C语言题典——移除元素(顺序表)

搭配使用更佳哦~~ 数据结构之顺顺顺——顺序表-CSDN博客 数据结构之顺序表的基本操作-CSDN博客 前面学了顺序表的相关知识&#xff0c;我们来尝试做一下关于顺序表的经典算法题~ 前言 27. 移除元素 - 力扣&#xff08;LeetCode&#xff09; 移除元素作为力扣上的一道不算太难…