概述

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. Scrapy:腾讯招聘整站数据爬取

    项目地址:https://hr.tencent.com/ 步骤一.分析网站结构和待爬取内容 以下省略一万字 步骤二.上代码(不能略了) 1.配置items.py import scrapy class ...

  2. 私有仓库 gitlab 部署笔记

    --------------------------------------------gitlab简介------------------------------------------------ ...

  3. 记录BigInteger犯过的一个错误

    2019年4月18号,面试遇到的面试题,当时做错了,纪念下. public class StrTest { public static void main(String[] args) { BigIn ...

  4. Python——代码汇总

    1.三级菜单 2.Windows启动服务 3.常用的Python实现 4.字典的基本操作

  5. navicat导入sql文件错误

    场景:100多M的sql文件导入到本地数据库报错,本地环境,phpstudy,报错一1294 - Invalid ON UPDATE clause for 'create_time' column,报 ...

  6. [2019.04.16] 由Python写成的自动解压脚本

    很久很久以前(二十七天吧……大概)被要求写一个脚本来检索并解压磁盘上所有的以特定格式命名的tar文件,于是乎学习和摸鱼就一起开始了. 这次要写的脚本,针对的是这样的文件结构: 文件结构如上图所示 可以 ...

  7. P2522 [HAOI2011]Problem b (莫比乌斯反演)

    题目 P2522 [HAOI2011]Problem b 解析: 具体推导过程同P3455 [POI2007]ZAP-Queries 不同的是,这个题求的是\(\sum_{i=a}^b\sum_{j= ...

  8. Vue(三)指令

    v-text:更新元素的text内容 <template> <div class="about"> <p v-text="msg" ...

  9. linux系统中日常运维常用命令汇总一

    一.查看日志和机器相关信息常用命令 1.cat cat 命令连接文件并打印到标准输出设备上,cat经常用来显示文件的内容,类似于下的type命令注意:当文件较大时,文本在屏幕上迅速闪过(滚屏),用户往 ...

  10. golang-Beego-orm创建的坑

    Orm使用sqlites不识别问题 Idc string `description:"机房"` 这个description sqlites的数据库不识别.解决方法 去掉descri ...