概述

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. git 版本回滚

    1.克隆代码到本地 git clone http://qtools@dev.qtoolsbaby.cn:81/gitlab/qtools/jenkins_ceshi.git 2.git log 查看所 ...

  2. [转帖]Ansible管理windows集群

    Ansible管理windows集群 http://www.cnblogs.com/Dev0ps/p/10026908.html 写的挺好的 我关注点还是不够好呢 最近公司新项目需要安装400+win ...

  3. 安装vue-cli

    1.路径 https://nodejs.org/en/ cmd 创建项目 1.创建一个基于 webpack 模板的新项目 vue init webpack projectname(项目名) 2.项目名 ...

  4. Linux(Ubuntu)使用日记------markdown文件与pdf,doc,docx文件的相互转化(pandoc使用)

    安装: sudo apt-get install pandoc 使用: man pandoc   查看帮助文档 直接转换,命令如下: pandoc -f markdown -t docx ./test ...

  5. nginx--default_server定义规则及配置

    nginx 的 default_server 指令可以定义默认的 server 出处理一些没有成功匹配 server_name 的请求,如果没有显式定义,则会选取第一个定义的 server 作为 de ...

  6. 数据分析三剑客之numpy

    Numpy 简介 数据分析三剑客:Numpy,Pandas,Matplotlib NumPy(Numerical Python) 是 Python 语言的一个扩展程序库,支持大量的维度数组与矩阵运算, ...

  7. python3.7安装pylint

    python3.7安装pylint之"setuptools"版本错误 借鉴:错题集(已解决):pyinstaller报错ModuleNotFoundError: No module ...

  8. 理解依赖注入,laravel IoC容器

    在看laravel文档的时候,有一个服务容器(IoC)的概念.它是这样介绍的:Laravel 服务容器是一个用于管理类依赖和执行依赖注入的强大工具.依赖注入听上去很花哨,其实质是通过构造函数或者某些情 ...

  9. 阿里云Redis开发规范

    转自: https://yq.aliyun.com/articles/531067 摘要: 本文介绍了在使用阿里云Redis的开发规范,从键值设计.命令使用.客户端使用.相关工具等方面进行说明,通过本 ...

  10. Vue学习笔记五:事件修饰符

    目录 什么是事件修饰符 没有事件修饰符的问题 HTML 运行 使用事件修饰符 .stop阻止冒泡 .prevent 阻止默认事件 .capture 添加事件侦听器时使用事件捕获模式 .self 只当事 ...