文件IO操作之文件锁fcntl()函数
来源:互联网 发布:linux 终端快捷键 编辑:程序博客网 时间:2024/05/21 17:46
linux是多用户的操作系统,在实际的应用中不可避免的会出现多个用户(或是多个进程)共同使用、操作一个文件的情况,这时,linux通常采用的方法是给文件上锁,来避免共享的资源产生竞争的状态。
文件锁 — 建议性锁:要求每个上锁文件的进程都要检查是否有锁存在,并尊重已有的锁。
—-强制性锁:由内核执行的锁,当一个文件被上锁进行写入的时候,内核将阻止其他任何文件对其 进行读写操作。采用强制性锁对性能影响很大,每次读写操作都必须检查是否有锁存在。
linux中实现上锁的函数lockf() — 施加建议性锁
fcntl() — 施加建议性锁,或是强制锁,或是对文件某一记录上锁(记录锁)
记录锁 —- 读取锁(共享锁):能够使多个进程都能在文件的同一部分建立读取锁
—- 写入锁(排斥锁):在任何时候只能有一个进程在文件的某个部分建立写入锁。
在文件的同一部分不能同时建立读取锁和写入锁。
fcntl()函数:
struct flock 结构体
struct flock{ short l_type; off_t l_start; short l_whence; off_t l_len; pid_t l_pid;}
如果要加锁整个文件,通常的方法是将l_start 设置为0,l_whence 设置为SEEK_SET,l_len设置为0.
设置文件锁部分代码lock_set.c:每次设置前都要查询文件是否已被上锁
struct flock old_lock,lock; lock.l_whence = SEEK_SET; lock.l_start = 0; lock.l_len = 0; lock.l_type = type; lock.l_pid = -1; /*file is locked?*/ fcntl(fd,F_GETLK,&lock); if(lock.l_type != F_UNLCK) { /*why cannot lock the file?*/ if(lock.l_type == F_RDLCK)//have a rd lock { printf("Read lock already set by %d\n",lock.l_pid); } else if(lock.l_type == F_WRLCK)//have a wr lock { pritnf("write lock already set by %d\n",lock.l_pid); } } /*l_type may have been modified by F_GETLCK*/ lock.l_type = type; if(fcntl(fd,F_SETLKW,&lock)<0) { printf("Lock failed:type=%d\n",lock.l_type); return 1; }
上写入锁:
#include "lock_set.c"int main(){ int fd; /*open file*/ fd = open("hello",O_RDWR|O_CREAT,0644); if(fd<0) { printf("Open file error\n") exit(1); } /*lock the file*/ lock_set(fd, F_WRLCK); getchar(); /*release the file*/ lock_set(fd, F_UNLCK); getchar(); close(fd); exit(0);}
在两个终端中运行write_lock,
终端1:
root@jwx-merit:/home/jwx/work/fileLock# ./write_lock
Wrtie lock set by 4823
Release lock by 4823
终端2:
root@jwx-merit:/home/jwx/work/fileLock# ./write_lock
write lock already set by 4823
Wrtie lock set by 4959
Release lock by 4959
由此可见,同一时刻只能有一个写入锁存在。
上读取锁;
#include "lock_set.c"int main(){ int fd; /*open file*/ fd = open("hello",O_RDWR|O_CREAT,0644); if(fd<0) { printf("Open file error\n") exit(1); } /*lock the file*/ lock_set(fd, F_RDLCK); getchar(); /*release the file*/ lock_set(fd, F_UNLCK); getchar(); close(fd); exit(0);}
在两个终端运行读取锁,
终端1:
root@jwx-merit:/home/jwx/work/fileLock# ./read_lock
Read lock set by 4965
Release lock by 4965
终端2:
root@jwx-merit:/home/jwx/work/fileLock# ./read_lock
Read lock set by 4968
Release lock by 4968
可见,读取锁可以同时被4965 和 4968 两个进程设置。
- 文件IO操作之文件锁fcntl()函数
- 8-文件IO-fcntl函数
- 文件IO---lseek、fcntl函数
- 七、Linux系统编程-文件和IO(五)fcntl函数及常用操作、文件锁
- fcntl函数对文件锁的操作
- Linux 文件I/O操作之fcntl函数
- 文件锁 fcntl()函数说明
- 文件操作二(fcntl文件锁)
- 文件I/O之fcntl函数
- Linux系统编程(3)——文件与IO之fcntl函数
- 文件锁--fcntl函数
- 文件锁fcntl()函数的使用
- 文件锁fcntl()函数的使用
- 文件锁fcntl()函数的使用
- UNIX环境编程之<二>文件共享I/O操作dup,fcntl函数
- linux C复习:文件操作|(函数fcntl)
- linux系统编程之文件与I/O(六):fcntl 函数与文件锁
- linux系统编程之文件与I/O(六):fcntl 函数与文件锁
- zynq-mpsoc系列之用petalinux生成BOOT.bin文件
- 转载--认识迅雷界面引擎
- 皮尔逊相关系数的计算(python代码版)
- 第七章 参数估计与假设检验
- 转义字符 通用字符名 字符char wchar_t
- 文件IO操作之文件锁fcntl()函数
- Spring中的事件监听机制在项目中的应用
- hdu 1059最小公倍数模板
- 1043. Is It a Binary Search Tree
- ADO接口之_ConnectionPtr
- Java实现图片上传到服务器,并把上传的图片读取出来
- MFC的COleDateTime类使用
- iOS:<Photos/Photos.h>获取相册视频以及图片
- 234. Palindrome Linked List