[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, ...
随机推荐
- Python开发【第十六篇】:AJAX全套(转)
作者:武沛齐 出处:http://www.cnblogs.com/wupeiqi/ 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接. 概述 对于 ...
- MySQL中怎么对varchar类型排序问题(转)
在mysql默认order by 只对数字与日期类型可以排序,但对于varchar字符型类型排序好像没有用了,下面我来给各位同学介绍varchar类型排序问题如何解决. 今天在对国家电话号码表进行 ...
- 在ConoHa上Centos7环境下源码安装部署LNMP
本文记录了从源码,在Centos 7上手动部署LNMP环境的过程,为了方便以后对nginx和mariadb进行升级,这里采用yum的方式进行安装. 1.建立运行网站和数据库的用户和组 groupadd ...
- OrCAD原理图中怎么导出FPGA的引脚分配
流程 (1)选择tool下的export FPGA: (2)选择厂商,选择器件型号.选择生成文件类型. 以上.
- 基于H5的混合开发介绍(一)WebView
转自: https://www.cnblogs.com/sanchang/p/9261461.html 一 WebView到底是什么 1 WebView是一种控件,它基于webkit引擎,因此具 ...
- [leetcode] 5.Longest Palindromic Substring-1
开始觉得挺简单的 写完发现这个时间超限了: class Solution: def longestPalindrome(self, s: str) -> str: # longest palin ...
- shell---命令解析器---简单操作快捷键
命令解析器---解析输入命令并执行操作 命令或目录补齐快捷键:TAB键 遍历史 输入: 小键盘:上 或 CTRL+P 小键盘:下 或 CTRL+N 光标移动: 小键盘:左 或 CTRL+B 小键盘:右 ...
- Vue(三)指令
v-text:更新元素的text内容 <template> <div class="about"> <p v-text="msg" ...
- mysq建表参数设置
建表的完整性约束: not null 与 default unique primary auto_increment foreign key 外键的变种 三种关系 一.介绍 约束条件与数据类型的宽度 ...
- IntelliJ IDEA 2017新工具
IntelliJ IDEA 2017 教程之概述 图文详解如何安装 Intellij IDEA 2017 深入剖析 IntelliJ IDEA 2017 的目录结构 图文详解如何配置 IntelliJ ...