Linux fcntl函数

embedded/2024/9/23 16:00:55/

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/embedded/13001.html

相关文章

Harmony OS应用开发性能优化全面指南

优化应用性能对于应用开发至关重要。通过高性能编程、减少丢帧卡顿、提升应用启动和响应速度&#xff0c;可以有效提升用户体验。本文将介绍一些优化应用性能的方法&#xff0c;以及常用的性能调优工具。 ArkTS高性能编程 为了提升代码执行速度&#xff0c;进而提升应用整体性…

LeetCode刷题实战4:寻找两个正序数组的中位数

题目内容 给定两个大小分别为 m 和 n 的正序&#xff08;从小到大&#xff09;数组 nums1 和 nums2。请你找出并返回这两个正序数组的 中位数 。 算法的时间复杂度应该为 O(log (mn)) 。 示例 1&#xff1a; 输入&#xff1a;nums1 [1,3], nums2 [2] 输出&#xff1a;2.0…

redis中的缓存穿透问题

缓存穿透 缓存穿透问题&#xff1a; 一般请求来到后端&#xff0c;都是先从缓存中查找数据&#xff0c;如果缓存中找不到&#xff0c;才会去数据库中查询数据。 而缓存穿透就是基于这一点&#xff0c;不断发送请求查询不存在的数据&#xff0c;从而使数据库压力过大&#xff…

Docker 更新容器状态 开启/关闭 开机自启动

开启&#xff08;开机 自启动 容器&#xff09; docker update --restartalways {容器ID/Name}关闭&#xff08;开机 自启动 容器&#xff09; docker update --restartno {容器ID/Name}我们下期见&#xff0c;拜拜&#xff01;

postgresql|数据库|实时数据库监控利器 pg_activity 的部署和初步使用

前言&#xff1a; postgresql的调优是比较重要的&#xff0c;那么&#xff0c;如何调优呢&#xff1f;自然是在某一个时间段内&#xff0c;通常是业务高峰期或者压测时间内实时观察数据库的运行情况&#xff0c;然后通过观察到的信息判断数据库的瓶颈&#xff0c;比如&#xf…

游戏中的伤害类型

在许多角色扮演游戏&#xff08;RPG&#xff09;、多人在线战斗竞技场&#xff08;MOBA&#xff09;游戏以及其他类型的游戏中&#xff0c;伤害类型是决定攻击效果如何与目标互动的重要因素。通常&#xff0c;伤害类型分为物理伤害、魔法伤害和真实伤害。下面是这些伤害类型的详…

1688商品详情API接口获取商品信息指南

在电子商务领域&#xff0c;API&#xff08;应用程序编程接口&#xff09;扮演着至关重要的角色&#xff0c;它允许开发者与平台进行交互&#xff0c;获取所需的数据。1688作为中国领先的B2B电子商务平台&#xff0c;提供了丰富的API接口&#xff0c;使得第三方开发者能够方便地…

手机误删照片如何恢复?怎么快速有效找回照片?

现在手机的功能越来越强大&#xff0c;手机厂家不断内卷&#xff0c;拍照功能越来越强&#xff0c;我们已经习惯用手机来记录生活了&#xff0c;因此储存的照片都特别珍贵&#xff0c;一旦误删&#xff0c;就容易造成不可磨灭的损失。那么手机误删照片如何恢复&#xff1f;本文…