fcntl 函数与文件锁
一、fcntl函数
功能:操纵文件描述符,改变已打开的文件的属性
int fcntl(int fd, int cmd, ... /* arg */ );
cmd的取值可以如下:
复制文件描述符
F_DUPFD (long)
设置/获取文件描述符标志
F_GETFD (void)
F_SETFD (long)
设置/获取文件状态标志
F_GETFL (void)
F_SETFL (long)
获取/设置文件锁
F_GETLK
F_SETLK,F_SETLKW
其中复制文件描述符可参见《linux系统编程之文件与I/O(五):打开文件的内核结构file和重定向》,文件描述符的标志只有一个即FD_CLOEXEC,设置/获取文件描述符标志看这里。下面先来看设置/获取文件状态标志。
F_SETFL:
On Linux this command can change only the O_APPEND, O_ASYNC, O_DIRECT, O_NOATIME, and O_NONBLOCK flags.
示例程序如下:
|
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
/*************************************************************************
> File Name: file_fcntl.c > Author: Simba > Mail: dameng34@163.com > Created Time: Sat 23 Feb 2013 02:34:02 PM CST ************************************************************************/ #include<sys/types.h> #include<sys/stat.h> #include<unistd.h> #include<fcntl.h> #include<stdio.h> #include<stdlib.h> #include<errno.h> #include<string.h> #define ERR_EXIT(m) \ void set_flag(int, int); int main(int argc, char *argv[]) printf("buf=%s\n", buf); void set_flag(int fd, int flags) void clr_flag(int fd, int flags) |
测试输出:
simba@ubuntu:~/Documents/code/linux_programming/APUE/File_IO$ ./file_fcntl
read error: Resource temporarily unavailable
因为将标准输入的状态更改为非阻塞,则read不会阻塞等待输入而立即返回错误,errno将被置为EAGAIN,即可以重新尝试。
二、文件锁结构体
struct flock {
...
short l_type; /* Type of lock: F_RDLCK,
F_WRLCK, F_UNLCK */
short l_whence; /* How to interpret l_start:
SEEK_SET, SEEK_CUR, SEEK_END */
off_t l_start; /* Starting offset for lock */
off_t l_len; /* Number of bytes to lock */
pid_t l_pid; /* PID of process blocking our lock
(F_GETLK only) */
...
};
文件锁的类型只有两种,一种是写锁也叫排他锁,一种是读锁也就共享锁,可以有多个进程各持有一个读锁,但只能有一个进程持有写锁,只有对文件有对应的读写权限才能施加对应的锁类型。中间三个参数
l_whence, l_start, l_len 决定了被锁定的文件范围。当fcntl 函数的cmd为F_GETLK时,flock 结构体的
l_pid 参数会返回持有写锁的进程id。进程退出或者文件描述符被关闭时,会释放所有的锁。
示例程序如下:
|
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
/*************************************************************************
> File Name: file_flock.c > Author: Simba > Mail: dameng34@163.com > Created Time: Sat 23 Feb 2013 02:34:02 PM CST ************************************************************************/ #include<sys/types.h> #include<sys/stat.h> #include<unistd.h> #include<fcntl.h> #include<stdio.h> #include<stdlib.h> #include<errno.h> #include<string.h> #define ERR_EXIT(m) \ int main(int argc, char *argv[]) if (fcntl(fd, F_SETLK, &lock) == 0) return 0; //进程退出会对所有文件解锁 |
测试如下:
我们先在一个 终端执行程序:
simba@ubuntu:~/Documents/code/linux_programming/APUE/File_IO$ ./file_flock
lock success
press any key to unlock
现在文件已经被锁住了,而且没有按下任何按键,所以卡在这里,也还没解锁,接着在另一个终端再次执行同个程序:
simba@ubuntu:~/Documents/code/linux_programming/APUE/File_IO$ ./file_flock
lock fail: Resource temporarily unavailable
会立即返回错误,因为我们希望施加的是排他锁,而现在前面一个进程正在占用写锁还没释放,所以尝试施加锁失败,而如果fcntl 函数的cmd 设置为 F_SETLKW,即带w的版本,则此进程会一直阻塞直到前面一个进程释放了锁。
参考:《APUE》
fcntl 函数与文件锁的更多相关文章
- fcntl函数加文件锁
对文件加锁是原子性的,可以用于进程间文件操作的同步.在linux下,有三个函数可以对文件进程加锁,分别是fcntl.flock.lockf.这里只说fcntl,它的用法也是最复杂的. fcntl是fi ...
- [Linux]fcntl函数文件锁概述
概述 fcntl函数文件锁有几个比较容易忽视的地方: 1.文件锁是真的进程之间而言的,调用进程绝对不会被自己创建的锁锁住,因为F_SETLK和F_SETLKW命令总是替换调用进程现有的锁(若已存在), ...
- Linux 系统 文件锁 fcntl函数详解
#include <unistd.h> #include <fcntl.h> int fcntl(int fd, int cmd); int fcntl(int fd, int ...
- fcntl函数用法——设置文件锁
fcntl函数.锁定文件,设置文件锁.设置获取文件锁:F_GETLK .F_SETLK .F_SETLKW文件锁结构,设置好用于fcntl函数的第三个参数.struct flock{ shor ...
- linxu fcntl 函数用法 【转】
功能描述:根据文件描述词来操作文件的特性. 文件控制函数 fcntl -- file control 头文件: #include <fcntl.h>; i ...
- Linux系统编程(3)——文件与IO之fcntl函数
linux文件I/O用:open.read.write.lseek以及close函数实现了文件的打开.读写等基本操作.fcntl函数可以根据文件描述词来操作文件. 用法: int fcntl(int ...
- 六、文件IO——fcntl 函数 和 ioctl 函数
6.1 fcntl 函数 6.1.1 函数介绍 #include <unistd.h> #include <fcntl.h> int fcntl(int fd, int cmd ...
- fcntl函数用法详解
功能描述:根据文件描述词来操作文件的特性. #include <unistd.h> #include <fcntl.h> int fcntl(int fd, int cmd) ...
- 文件控制 fcntl函数具体解释
摘要:本文主要讨论文件控制fcntl函数的基本应用.dup函数能够拷贝文件描写叙述符,而fcntl函数与dup函数有着异曲同工之妙.而且还有更加强大的功能,能够获取或设置已打开文件的性质,操作文件锁. ...
随机推荐
- cesiumjs学习笔记之三——cesium-navigation插件 【转】
http://blog.csdn.net/Prepared/article/details/68940997?locationNum=10&fps=1 插件源码地址:https://githu ...
- [leetcode]Construct Binary Tree from Inorder and Postorder Traversal @ Python
原题地址:http://oj.leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/ 题意: ...
- DIV焦点事件详解 --【focus和tabIndex】
添加 tabindex='-1' 属性: 默认:获取不到焦点事件(blur) 1 <div class="wl-product" id="wl-product&qu ...
- org.dom4j.DocumentException: unknown protocol: d Nested exception: unknown
最近用dom4j写了一个修改XML文件的小例子,抛出了如下异常: org.dom4j.DocumentException: unknown protocol: d Nested exception: ...
- HDU 4585 Shaolin (STL)
没想到map还有排序功能,默认按照键值从小到大排序 #include <cstdio> #include <iostream> #include <cstring> ...
- SSIS包部署错误之运行环境设置
在SQLServer代理处新建了一个job,步骤里面的操作是指向一个SSIS模型包 执行,报错如下 根据报错信息是64bit,于是做出修改如下图 再次执行 OK
- OleView.exe:查看机器上的COM 组件。
OleView.exe可以查看机器上安装的所有COM组件的类别以及各个类别下的COM组件.
- java编程思想---对象
一.对象 对于每种语言来说,都有自己操纵内存中元素的方法. 在java中,一切被视为对象.可是操纵对象的是一个"引用".举个样例,能够比作为遥控器对电视的操作,遥控器就是引用,而电 ...
- 算法笔记_173:历届试题 斐波那契(Java)
目录 1 问题描述 2 解决方案 1 问题描述 问题描述 斐波那契数列大家都非常熟悉.它的定义是: f(x) = 1 .... (x=1,2) f(x) = f(x-1) + f(x-2) ... ...
- vb sendmessage 详解1
SendMessage函数的常用消息及其应用(有点长,希望能对大家有所帮助)函数原型: Declare Function SendMessage Lib "user32" Alia ...