概述

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, &section_1);
printf("section_1 l_type: %d\n", (&section_1)->l_type);
re = fcntl(file, F_SETLK, &section_1_1);
report_err(re);
printf("section_1_1 l_type: %d\n", (&section_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, &section_2);
report_err(re);
printf("section_2 l_type: %d\n", (&section_2)->l_type);
re = fcntl(file, F_SETLKW, &section_2_1);
report_err(re);
printf("section_2_1 l_type: %d\n", (&section_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函数文件锁概述的更多相关文章

  1. Linux fcntl函数详解

    功能描述:根据文件描述词来操作文件的特性. 文件控制函数          fcntl -- file control 头文件: #include <unistd.h> #include ...

  2. Linux fcntl函数设置阻塞与非阻塞

    转自http://www.cnblogs.com/xuyh/p/3273082.html 用命令F_GETFL和F_SETFL设置文件标志,比如阻塞与非阻塞 F_SETFL     设置给arg描述符 ...

  3. Linux 系统 文件锁 fcntl函数详解

    #include <unistd.h> #include <fcntl.h> int fcntl(int fd, int cmd); int fcntl(int fd, int ...

  4. fcntl函数加文件锁

    对文件加锁是原子性的,可以用于进程间文件操作的同步.在linux下,有三个函数可以对文件进程加锁,分别是fcntl.flock.lockf.这里只说fcntl,它的用法也是最复杂的. fcntl是fi ...

  5. Linux系统编程(3)——文件与IO之fcntl函数

    linux文件I/O用:open.read.write.lseek以及close函数实现了文件的打开.读写等基本操作.fcntl函数可以根据文件描述词来操作文件. 用法: int fcntl(int ...

  6. fcntl 函数与文件锁

    一.fcntl函数 功能:操纵文件描述符,改变已打开的文件的属性 int fcntl(int fd, int cmd, ... /* arg */ ); cmd的取值可以如下: 复制文件描述符 F_D ...

  7. [转]Linux系统调用--fcntl函数详解

    功能描述:根据文件描述词来操作文件的特性. 文件控制函数          fcntl -- file control头文件: #include <unistd.h> #include & ...

  8. fcntl函数用法——设置文件锁

    fcntl函数.锁定文件,设置文件锁.设置获取文件锁:F_GETLK .F_SETLK  .F_SETLKW文件锁结构,设置好用于fcntl函数的第三个参数.struct flock{    shor ...

  9. Linux下 fcntl 函数用法说明

    功能描述:根据文件描述词来操作文件的特性. 文件控制函数         fcntl -- file control LIBRARY         Standard C Library (libc, ...

随机推荐

  1. java学习——JDK1.8接口和实现类

    Java 8 新特性:接口的静态方法和默认方法 https://blog.csdn.net/sun_promise/article/details/51220518 接口的默认方法和静态方法 http ...

  2. spring多模块项目手动整合

    一.分别创建parent entity dao service controller web等模块项目,如图: 二.parent项目添加依赖 <!-- 集中定义依赖版本号 --> < ...

  3. call(),apply(),bind()区别?

    每个函数都包含两个非继承而来的方法,apply()和call(),这两方法的用途都是在特定的作用域中调用函数,实际上等于设置函数数体内的this对象的值. apply()和call()第一个参数都一样 ...

  4. 如何解决Redis中的key过期问题

    最近我们在Redis集群中发现了一个有趣的问题.在花费大量时间进行调试和测试后,通过更改key过期,我们可以将某些集群中的Redis内存使用量减少25%. Twitter内部运行着多个缓存服务.其中一 ...

  5. python3.6使用pygal模块不具交互性,图片不能显示数据

    1.版本 个人电脑版本:win10+python3.6 2.安装 2.1 安装过的版本 1. 先使用pip安装pygal1.7,(参考<python从入门到实践>)         pyt ...

  6. 【dp】摘花生

    [题目描述] Hello Kitty想摘点花生送给她喜欢的米老鼠.她来到一片有网格状道路的矩形花生地(如下图),从西北角进去,东南角出来.地里每个道路的交叉点上都有种着一株花生苗,上面有若干颗花生,经 ...

  7. [HNOI2012]集合选数(状压DP+构造)

    题目要求若出现x,则不能出现2x,3x 所以我们考虑构造一个矩阵 \(1\ 2\ 4 \ 8--\) \(3\ 6\ 12\ 24--\) \(9\ 18\ 36--\) \(--\) 不难发现,对于 ...

  8. 通用权限管理系统之权限菜单zTree树的展示及移动的处理方法

    在通用权限管理系统中,有很多数据结构是有父子关系的,如组织机构,部门,权限菜单等,在展示的时候,大多数是通过zTree树的形式展现的,如下: 权限菜单展示 这种数据后台输出比较容易处理,参考如下获取某 ...

  9. Apache Hadoop 2.9.2 的快照管理

    Apache Hadoop 2.9.2 的快照管理 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 快照相当于对目录做一个备份.并不会立即复制所有文件,而是指向同一个文件.当写入发生 ...

  10. Hadoop记录-queue使用率

    #!/bin/sh ip=xxx port=8088 export HADOOP_HOME=/app/hadoop/bin rmstate1=$($HADOOP_HOME/yarn rmadmin - ...