[Linux]fcntl函数文件锁概述
概述
fcntl函数文件锁有几个比较容易忽视的地方:
1.文件锁是真的进程之间而言的,调用进程绝对不会被自己创建的锁锁住,因为F_SETLK和F_SETLKW命令总是替换调用进程现有的锁(若已存在),所以调用进程决不会阻塞在自己持有的锁上,于是,F_GETLK命令决不会报告调用进程自己持有的锁。
2.struct flock结构指针中的l_type成员3个short值分别是:
| 常量 | 值 |
| F_RDLCK | 0 |
| F_WRLCK | 1 |
| F_UNLCK | 2 |
3.如果两个锁之间的文件区域有交集,就会阻塞,无论交集的大小。

如图中section 1持有1到10字节区间的读锁,倘若此时需要创建section 2写锁,那么需要等待section 1区域释放才行。
示例代码:
进程A
#include <error.h>
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h> extern int *__errno_location(void);
#define errno (*__errno_location()) void report_err(int re); struct flock section_1 = {
F_RDLCK,
SEEK_SET,
, };
struct flock section_1_1 = {
F_RDLCK,
SEEK_SET,
, }; int main(void)
{
int re;
int file = open("/documents/test/test_2", O_RDWR);
printf("open file fd: %d\n", file);
//struct flock section_1_1 = section_1;
re = fcntl(file, F_GETLK, §ion_1);
printf("section_1 l_type: %d\n", (§ion_1)->l_type);
re = fcntl(file, F_SETLK, §ion_1_1);
report_err(re);
printf("section_1_1 l_type: %d\n", (§ion_1_1)->l_type); sleep();
return ;
} void report_err(int re)
{
if(re == -){
perror("file error");
}
}
进程B
#include <error.h>
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h> extern int *__errno_location(void);
#define errno (*__errno_location()) void report_err(int re); struct flock section_2 = {
F_WRLCK,
SEEK_SET,
, };
struct flock section_2_1 = {
F_WRLCK,
SEEK_SET,
, }; int main(void)
{
int re;
int file = open("/documents/test/test_2", O_RDWR);
printf("open file fd: %d\n", file); re = fcntl(file, F_GETLK, §ion_2);
report_err(re);
printf("section_2 l_type: %d\n", (§ion_2)->l_type);
re = fcntl(file, F_SETLKW, §ion_2_1);
report_err(re);
printf("section_2_1 l_type: %d\n", (§ion_2_1)->l_type);
return ;
} void report_err(int re)
{
if(re == -){
perror("file error");
}
}
进程A在创建section 1后阻塞10秒,期间启动进程B创建section 2,此时进程B阻塞等待进程A释放section 1。
4.锁与进程和文件关联。这里有两重含义:第一重很明显,当一个进程终止时,它所有建立的锁全部释放;第二重则不太明显,无论一个描述符何时关闭,该进程通过这一描述符引用的文件上的任何一把锁都会释放(这些锁都是该进程设置的),详情参见《Unix高级环境编程》396页。
[Linux]fcntl函数文件锁概述的更多相关文章
- Linux fcntl函数详解
功能描述:根据文件描述词来操作文件的特性. 文件控制函数 fcntl -- file control 头文件: #include <unistd.h> #include ...
- Linux fcntl函数设置阻塞与非阻塞
转自http://www.cnblogs.com/xuyh/p/3273082.html 用命令F_GETFL和F_SETFL设置文件标志,比如阻塞与非阻塞 F_SETFL 设置给arg描述符 ...
- Linux 系统 文件锁 fcntl函数详解
#include <unistd.h> #include <fcntl.h> int fcntl(int fd, int cmd); int fcntl(int fd, int ...
- fcntl函数加文件锁
对文件加锁是原子性的,可以用于进程间文件操作的同步.在linux下,有三个函数可以对文件进程加锁,分别是fcntl.flock.lockf.这里只说fcntl,它的用法也是最复杂的. fcntl是fi ...
- Linux系统编程(3)——文件与IO之fcntl函数
linux文件I/O用:open.read.write.lseek以及close函数实现了文件的打开.读写等基本操作.fcntl函数可以根据文件描述词来操作文件. 用法: int fcntl(int ...
- fcntl 函数与文件锁
一.fcntl函数 功能:操纵文件描述符,改变已打开的文件的属性 int fcntl(int fd, int cmd, ... /* arg */ ); cmd的取值可以如下: 复制文件描述符 F_D ...
- [转]Linux系统调用--fcntl函数详解
功能描述:根据文件描述词来操作文件的特性. 文件控制函数 fcntl -- file control头文件: #include <unistd.h> #include & ...
- fcntl函数用法——设置文件锁
fcntl函数.锁定文件,设置文件锁.设置获取文件锁:F_GETLK .F_SETLK .F_SETLKW文件锁结构,设置好用于fcntl函数的第三个参数.struct flock{ shor ...
- Linux下 fcntl 函数用法说明
功能描述:根据文件描述词来操作文件的特性. 文件控制函数 fcntl -- file control LIBRARY Standard C Library (libc, ...
随机推荐
- webpack code splitting
一.代码分割优化 const webpack = require('webpack'); const HtmlWebpackPlugin = require('html-webpack-plugin' ...
- 重写Sink合并多行
flume1.6+elasticsearch6.3.2 Pom <dependencies> <dependency> <groupId>junit</gro ...
- android 调用 screenrecord 实现录屏
首先要说明的是并未实现,本文讲一下自己的思路. adb 使用shell 命令 screenrecord 可录屏. 自己写了个app,通过Process p = Runtime.getRuntime() ...
- Going from u to v or from v to u? POJ - 2762(强连通 有向最长路径)
In order to make their sons brave, Jiajia and Wind take them to a big cave. The cave has n rooms, an ...
- Wireshark显示结果过滤基本语法
按IP地址过滤:1.仅显示源地址为192.168.1.95的项目: ip.src eq 192.168.1.95 2.仅显示目的地址为192.168.1.95的项目: ip.dst eq 192.16 ...
- 关于 mybatis-generator自定义注释生成 使用DefaultCommentGenerator重写来完成
项目里新建表时model,mapper以及mapper.xml基本都是用Mybatis Generator(以下简称为MBG)自动生成的,但是MBG自动生成的model的注释实在有点非人类,至少中国人 ...
- jemter+ant+jenkins进行集成测试
一下为我学习的一些笔记: 一.安装配置ant 安装地址:http://ant.apache.org/ 1.下载ant一路傻瓜式安装 2.配置ant环境变量:path下配置ant的bin路径 3.将jm ...
- Nginx location 匹配规则详解
语法规则 location [=|~|~*|^~] /uri/ { … } 模式 含义 location = /uri = 表示精确匹配,只有完全匹配上才能生效 location ^~ /uri ^~ ...
- C# Linq to Entity 多条件 OR查询
技术背景:框架MVC,linq to Entity 需要一定的lambda书写能力 问题:在简单的orm中完成一些简单的增删查改是通过where insert delete update 完成的,但是 ...
- Linux下的sudo及配置
sudo的常用命令 man sudoers # 参阅帮助 visudo # 编辑sudoers文件的命令 sudo -l # 查看可执行或禁止执行的命令 sudo -u user1 /bin/ls # ...