linux flock()
表头文件 #include<sys/file.h>
定义函数 int flock(int fd,int operation);
函数说明 flock()会依参数operation所指定的方式对参数fd所指的文件做各种锁定或解除锁定的动作。此函数只能锁定整个文件,无法锁定文件的某一区域。
参数 operation有下列四种情况:
LOCK_SH 建立共享锁定。多个进程可同时对同一个文件作共享锁定。
LOCK_EX 建立互斥锁定。一个文件同时只有一个互斥锁定。
LOCK_UN 解除文件锁定状态。
LOCK_NB 无法建立锁定时,此操作可不被阻断,马上返回进程。通常与LOCK_SH或LOCK_EX 做OR(|)组合。
单一文件无法同时建立共享锁定和互斥锁定,而当使用dup()或fork()时文件描述词不会继承此种锁定。
返回值 返回0表示成功,若有错误则返回-1,错误代码存于errno。
flock只要在打开文件后,需要对文件读写之前flock一下就可以了,用完之后再flock一下,前面加锁,后面解锁。其实确实是这么简单,但是前段时间用的时候发现点问题,问题描述如下:
一个进程去打开文件,输入一个整数,然后上一把写锁(LOCK_EX),再输入一个整数将解锁(LOCK_UN),另一个进程打开同样一个文件,直 接向文件中写数据,发现锁不起作用,能正常写入(我此时用的是超级用户)。google了一大圈发现flock不提供锁检查,也就是说在用flock之前 需要用户自己去检查一下是否已经上了锁,说明白点就是读写文件之前用一下flock检查一下文件有没有上锁,如果上锁了flock将会阻塞在那里(An attempt to lock the file using one of these file descriptors may be denied by a lock that the calling process has already placed via another descriptor ),除非用了LOCK_NB。一个完整的用于测试的事例代码如下所示:
//lockfile.c
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
int main()
{
int fd,i;
char path[]="/home/taoyong/test.txt";
extern int errno;
fd=open(path,O_WRONLY|O_CREAT);
if(fd!=-1)
{
printf("open file %s ./n",path);
printf("please input a number to lock the file./n");
scanf("%d",&i);
if(flock(fd,LOCK_EX)==0)
{
printf("the file was locked./n");
}
else
{
printf("the file was not locked./n");
}
printf("please input a number to unlock the file./n");
scanf("%d",&i);
if(flock(fd,LOCK_UN)==0)
{
printf("the file was unlocked./n");
}
else
{
printf("the file was not unlocked./n");
}
close(fd);
}
else
{
printf("cannot open file %s/n",path);
printf("errno:%d/n",errno);
printf("errMsg:%s",strerror(errno));
}
}
//testprocess.c
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/file.h>
int main()
{
int fd,i;
char path[]="/home/taoyong/test.txt";
char s[]="writing.../nwriting....../n";
extern int errno;
fd=open(path,O_WRONLY|O_CREAT|O_APPEND);
if(fd!=-1)
{
printf("open file %s ./n",path);
if(flock(fd,LOCK_EX|LOCK_NB)==0)
{
printf("the file was locked by the process./n");
if(-1!=write(fd,s,sizeof(s)))
{
printf("write %s to the file %s/n",s,path);
}
else
{
printf("cannot write the file %s/n",path);
printf("errno:%d/n",errno);
printf("errMsg:%s/n",strerror(errno));
}
}
else
{
printf("the file was locked by other process.Can't write.../n");
printf("errno:%d:",errno);
}
close(fd);
}
else
{
printf("cannot open file %s/n",path);
printf("errno:%d/n",errno);
printf("errMsg:%s",strerror(errno));
}
}
linux flock()的更多相关文章
- 使用linux flock文件锁实现任务锁定避免计划任务程序冲突
格式:flock [-sxun][-w #] fd#flock [-sxon][-w #] file [-c] command选项-s, --shared: 获得一个共享锁 -x, --exclusi ...
- linux使用flock文件锁解决crontab冲突问题
* * * * * flock -xn /dev/shm/redis.lock -c "/usr/local/bin/redis-server" 可以用flock命令,配合使用rs ...
- linux使用flock文件锁
使用linux flock 文件锁实现任务锁定,解决冲突 格式: flock [-sxun][-w #] fd# flock [-sxon][-w #] file [-c] command flock ...
- Linux multiple open a device
Linux multiple open a device a device = /dev/wiegand Linux在多次打开同一个设备(/dev/wiegand)的时候,打开结果都是成功,但是在用w ...
- Linux 定时任务的配置
通常我们会需要定时启动一些shell脚本,类似Windows中的Task Scheduler, 下面是在AWS EMR Cluster 主几点上配置的步骤: 1. 先创建一个shell脚本,将需要执行 ...
- Linux 驱动开发
linux驱动开发总结(一) 基础性总结 1, linux驱动一般分为3大类: * 字符设备 * 块设备 * 网络设备 2, 开发环境构建: * 交叉工具链构建 * NFS和tftp服务器安装 3, ...
- linux cron计划任务防止多个任务同时运行
使用linux flock 文件锁实现任务锁定,解决冲突格式:flock [-sxun][-w #] fd#flock [-sxon][-w #] file [-c] command选项-s, --s ...
- Nginx基础知识之————RTMP模块专题(实践文档)
on_publish 语法:on_publish url上下文:rtmp, server, application描述:这个可以设置为一个API接口(GET方式接受所有参数),会给这个API接口返回8 ...
- (原创)Rocketmq分布式消息队列的部署与监控
-------------------------------------------------------------------------------------------- 一.Rocke ...
随机推荐
- AcWing 802. 区间和
(https://www.acwing.com/problem/content/804/) 假定有一个无限长的数轴,数轴上每个坐标上的数都是0. 现在,我们首先进行 n 次操作,每次操作将某一位置x上 ...
- 深入ArrayList看fast-fail机制
fail-fast机制简介 什么是fail-fast fail-fast 机制是java集合(Collection)中的一种错误机制.它只能被用来检测错误,因为JDK并不保证fail-fast机制一定 ...
- SpringBootTest单元测试及日志
springboot系列学习笔记全部文章请移步值博主专栏**: spring boot 2.X/spring cloud Greenwich. 由于是一系列文章,所以后面的文章可能会使用到前面文章的项 ...
- win32 socket编程(二)——TCP/IP
一.大端.小端法定义 1.1小端法(Little-Endian)就是低位字节排放在内存的低地址端即该值的起始地址,高位字节排放在内存的高地址端. (主机字节顺序) 1.2 大端法(Big-Endian ...
- 调用SM30数据表维护的函数
相关文章:http://www.cnblogs.com/caizjian/p/3248499.html 1.se11进去新建一个数据表 2.se55进去生产表维护 3.sm30进去维护数据表 4.se ...
- ajaxSubmit 实现图片上传 SSM maven
文件上传依赖: <!-- 文件上传组件 --> <dependency> <groupId>commons-fileupload</groupId> & ...
- Dubbo学习源码总结系列四--集群容错机制
Dubbo提供了哪些集群容错机制?如何实现的? 提供了六种集群容错机制,包括Failover(失败自动切换,尝试其他服务器).Failfast(失败立即抛出异常).Failsafe(失 ...
- 在eclipse里搜索maven项目需要的dependency
eclipse直接就可以通过下载同步仓库索引,直接关键字查询需要的dependency. 前提是你已经在你的eclipse上配好了maven正确的环境. 1. 设置在开启eclipse时下载同步仓库索 ...
- linux详解 rsync 服务和配置文件
首先要选择服务器启动方式: l 对于负荷较重的 rsync 服务器应该选择独立运行方式 l 对于负荷较轻的 rsync 服务器应该选择 xinetd 运行方式 l 创建配 ...
- WAF防火墙学习
正则解析神器 http://rick.measham.id.au/paste/explain.pl http://regexr.com/ http://regex101.com/ http://www ...