linux c编程之fcntl
fcntl可实现对指定文件描述符的各种操作,其函数原型如下:
int fcntl(int fd, int cmd, ... /* arg */ );
其中,操作类型由cmd决定。cmd可取如下值:
- F_DUPFD:复制文件描述符
- F_DUPFD_CLOEXEC:复制文件描述符,新文件描述符被设置了close-on-exec
- F_GETFD:读取文件描述标识
- F_SETFD:设置文件描述标识
- F_GETFL:读取文件状态标识
- F_SETFL:设置文件状态标识
- F_GETLK:如果已经被加锁,返回该锁的数据结构。如果没有被加锁,将l_type设置为F_UNLCK
- F_SETLK:给文件加上进程锁
- F_SETLKW:给文件加上进程锁,如果此文件之前已经被加了锁,则一直等待锁被释放。
接下来看两段代码:

#include <fcntl.h>
#include <unistd.h>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#define ERR_EXIT(msg) \
do \
{ \
perror(msg); \
exit(-1); \
} while(0)
int main()
{
int fd = open("test.dat", O_CREAT | O_RDWR | O_TRUNC, 0644);
if (fd < 0)
ERR_EXIT("open file failed");
struct flock f;
memset(&f, 0, sizeof(f));
f.l_type = F_WRLCK;
f.l_whence = SEEK_SET;
f.l_start = 0;
f.l_len = 0;
if (fcntl(fd, F_SETLK, &f) < 0)
ERR_EXIT("lock file failed");
printf("press any key to unlock\n");
getchar();
f.l_type = F_UNLCK;
if (fcntl(fd, F_SETLK, &f) < 0)
ERR_EXIT("unlock file failed");
return 0;
}

上述代码实现了加锁和解锁两个操作。

#include <unistd.h>
#include <fcntl.h>
#include <cstdio>
#include <cstdlib>
#include <cerrno>
#include <sys/types.h>
#include <sys/stat.h>
#define ERR_EXIT(msg) \
do \
{ \
perror(msg); \
exit(-1); \
} while(0)
void set_flag(int fd, int flags);
void clr_flag(int fd, int flags);
int main()
{
char buf[1024];
set_flag(0, O_NONBLOCK);
int ret = read(0, buf, 1024);
if (ret < 0)
ERR_EXIT("read failed");
return 0;
}
void set_flag(int fd, int flags)
{
int val = fcntl(fd, F_GETFL, 0);
if (val < 0)
ERR_EXIT("get flag failed");
val |= flags;
if (fcntl(fd, F_SETFL, val) < 0)
ERR_EXIT("set flag failed");
}
void clr_flag(int fd, int flags)
{
int val = fcntl(fd, F_GETFL, 0);
if (val < 0)
ERR_EXIT("get flag failed");
val &= ~flags;
if (fcntl(fd, F_SETFL, val) < 0)
ERR_EXIT("set flag failed");
}

其中set_flag设置文件状态标识,clr_flag清除文件状态标识。main函数中将stdout设置成非阻塞,所以执行read时,不等待用户输入而直接返回错误。
linux c编程之fcntl
linux c编程之fcntl的更多相关文章
- linux网络编程之shutdown() 与 close()函数详解
linux网络编程之shutdown() 与 close()函数详解 参考TCPIP网络编程和UNP: shutdown函数不能关闭套接字,只能关闭输入和输出流,然后发送EOF,假设套接字为A,那么这 ...
- Linux应用编程之lseek详解
Linux应用编程之lseek详解 1.lseek函数介绍 (1).文件指针:当我们要对一个文件进行读写时,一定要先打开这个文件,所以我们读写的所有文件都是动态文件.动态文件在内存中的形态就是文件流的 ...
- linux C编程之makefile
目的: 基本掌握了 make 的用法,能在Linux系统上编程.环境: Linux系统,或者有一台Linux服务器,通过终端连接.一句话:有Linux编译环境.准备: ...
- linux网络编程之IO模型
本文转自作者:huangguisu 1. 概念理解 在进行网络编程时,我们常常见到同步(Sync)/异步(Async),阻塞(Block)/非阻塞(Unblock)四种调用方式:同步: 所谓 ...
- linux网络编程之socket编程(十六)
继续学习socket编程,今天的内容会有些难以理解,一步步来分解,也就不难了,正入正题: 实际上sockpair有点像之前linux系统编程中学习的pipe匿名管道,匿名管道它是半双工的,只能用于亲缘 ...
- (十)Linux 网络编程之ioctl函数
1.介绍 Linux网络程序与内核交互的方法是通过ioctl来实现的,ioctl与网络协议栈进行交互,可得到网络接口的信息,网卡设备的映射属性和配置网络接口.并且还能够查看,修改,删除ARP高速缓存的 ...
- (ubuntu)linux C编程之sleep()和usleep()的使用和区别
### 函数名: sleep 头文件: #include <windows.h> // 在VC中使用带上头文件 #include <unistd.h> // 在gcc编译器中, ...
- linux socket编程之TCP与UDP
转:http://blog.csdn.net/gaoxin1076/article/details/7262482 TCP/IP协议叫做传输控制/网际协议,又叫网络通信协议 TCP/IP虽然叫传输控制 ...
- Linux内核模块编程之Helloworld(初级)
注意printk那里,KERN_ALERT和打印消息之间是没有逗号的,搞得劳资查了半天才发现一直没有提示信息的原因 #include <linux/init.h> #include < ...
随机推荐
- PGSQL-通过SQL语句来计算两个日期相差的天数
这是本人第一次写的~我在某次需求中遇到一个问题,如何在SQL语句中计算出两个日期的天数,然后用那个结果来进行数据的筛选呢?通过网上查阅了资料发现 date_part('day', cast(time1 ...
- the security settings could not be applied to the database(mysql安装error)【简记】
在安装mysql时,出现“The security settings could not be applied to the database because the connection has f ...
- June. 22 2018, Week 25th. Friday
Where words fail, music speaks. 言语无法表达时,音乐就会响起. From Hans Christian Andersen. Where words fail, musi ...
- ZooInspector 连接不到 Zookeeper 的解决方法
Zookeeper正常启动后,在使用 ZooInspector 连接 Zookeeper 时出现了连接不上的问题. [root@localhost bin]# zkServer.sh start Zo ...
- 安装mysql的踩坑之旅
近期的一个项目要求用mysql数据库,正好系统重装了,复习下mysql的安装,哪成想是踩了无数坑啊! 要安装首先自然是火速进官网下个安装包(下载地址https://dev.mysql.com/down ...
- 第一节 anaconda+jupyter+numpy简单使用
数据分析:是把隐藏在一些看似杂乱无章的数据背后的信息提炼出来,总结出所研究对象的内在规律 数据分析三剑客:Numpy,Pandas,Matplotlib 一 Anaconda 1 下载 官网:http ...
- 【English】20190321
Keep in mind记住[kip ɪn maɪnd] maintenance维护[ˈmentənəns] Also Keep in mind that table maintenance use ...
- 【Python 04】Python开发环境概述
1.Python概述 Python是一种计算机程序设计语言,一个python环境中需要有一个解释器和一个包集合. (1)Python解释器 使用python语言编写程序之前需要下载一个python解释 ...
- (golang)HTTP基本认证机制及使用gocolly登录爬取
内网有个网页用了HTTP基本认证机制,想用gocolly爬取,不知道怎么登录,只好研究HTTP基本认证机制 参考这里:https://www.jb51.net/article/89070.htm 下面 ...
- pip 升级 pip
For Python2 sudo pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ --upgrade pip For Python3 ...