文件锁"/>
【Linux】Linux文件锁
文件锁
前言
/proc是一个特殊的文件系统。
该目录下文件用来表示与启动、内核相关的特殊信息。
/proc/cpuinfo——CPU详细信息
/proc/meminfo——内存相关信息
/proc/version——版本信息
/proc/sys/fs/file-max——系统中能同时打开的文件总数
可修改该文件
进程的相关信息——/proc/32689/ 表示指定进程(进程号为32689)的相关信息
/proc/devices——已分配的字符设备、块设备的设备号
文件锁
- 用于并发对文件I/O进行操作
用法
#include <unistd.h>
#include <fcntl.h>
int fcntl(int fd, int cmd, ... /* arg */ );
参数
- cmd——取值F_GETLK,F_SETLK和F_SETLKW,分别表示获取锁、设置锁、和同步设置锁。
struct flock {
short l_type; /*F_RDLCK, F_WRLCK, or F_UNLCK */
off_t l_start; /*offset in bytes, relative to l_whence */
short l_whence; /*SEEK_SET, SEEK_CUR, or SEEK_END */
off_t l_len; /*length, in bytes; 0 means lock to EOF */
pid_t l_pid; /*returned with F_GETLK */
};
l_type: 第一个成员是加锁的类型:只读锁,读写锁,或是解锁。
l_start和l_whence: 用来指明加锁部分的开始位置。
l_len: 是加锁的长度。
l_pid: 是加锁进程的进程id。
示例:
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>#define FILE_NAME "flock_demo.txt"int flock_set(int fd, int type) {printf("pid=%d come in.\n",getpid());struct flock fflock;memset(&fflock, 0, sizeof(fflock));fcntl(fd,F_GETLK,&fflock);if (fflock.l_type != F_UNLCK) {if (fflock.l_type == F_RDLCK) {//有锁,判断是读锁还是写锁printf("flock has been set to read lock by %d\n",fflock.l_pid);} else if (fflock.l_type == F_WRLCK) {printf("flock has been set to write lock by %d\n", fflock.l_pid);}}//锁定文件fflock.l_type = type;fflock.l_whence = SEEK_SET;fflock.l_start = 0;fflock.l_len = 0;fflock.l_pid = -1;//阻塞式的if (fcntl(fd,F_SETLKW,&fflock) < 0) {printf("set lock failed!\n");return -1;}switch (fflock.l_type) {case F_RDLCK:printf("read lock is set by %d\n", getpid());break;case F_WRLCK:printf("write lock is set by %d\n", getpid());break;case F_UNLCK:printf("lock is released by %d\n", getpid());break;default:break;}printf("Process pid = %d out.\n",getpid());return 0;}int main(void) {int fd = 0;fd = open(FILE_NAME, O_RDWR | O_CREAT, 0666);if (fd < 0) {printf("open file %s failed!\n",FILE_NAME);exit(-1);}//flock_set(fd, F_RDLCK); //读锁flock_set(fd, F_WRLCK); //写锁getchar();flock_set(fd, F_UNLCK); //解锁getchar();close(fd);return 0;
}
- 写锁是排他性的,文件上了写锁,就会阻止其他程序的写锁与读锁。
- 读锁可以多个程序对同一文件上读锁,除此之外其他情况也会失败(阻止其他程序的读锁与写锁)。
更多推荐
【Linux】Linux文件锁
发布评论