一、文件锁
文件锁用于多个用户共同使用或操作同一个文件。有读锁的时候可以再加读锁,不能再加写锁。有写锁的时候,不能加任何锁,加读锁时,该描述符必须是读打开,加写锁时,该描述符必须是写打开,且只在进程间有用。
3对于锁区域要注意的几点
3.1 锁可以开始或者超过文件当前结束位置,但是不可以开始或者超过文件的开始位置
3.2 如果l_len为0,意味着锁的区域为可以到达的最大文件偏移位置。这个类型,可以让我们锁住一个文件的任意开始位置,结束的区域可以到达任意的文件结尾,并且以append方式追加文件时,也会同样上锁。
3.3 如果要锁住整个文件,设置l_start 和 l_whence为文件的开始位置(l_start为0 l_whence 为 SEEK_SET ),并且l_len为0。
3.4 如果有多个读共享锁(l_type of F_RDLCK),其他的读共享锁可以接受,但是写互斥锁(type ofF_WRLCK)拒绝
3.5 如果有一个写互斥锁(type ofF_WRLCK),其他的读共享锁(l_type of F_RDLCK)拒绝,其他的写互斥锁拒绝。
3.6 如果要取得读锁,这个文件描述符必须被打开可以去读;如果要或者写锁,这个文件的描述符必须可以被打开可以去写。
4 fcntl的cmd参数
F_GETLK:决定以flockptr描述的锁,是否被其他的锁阻塞。
F_SETLK :设置锁。
F_SETLKW:对应着F_GETLK的可以阻塞的版本。w意味着wait
重写的上锁方法
上写锁//lock.c int lock_set(int fd,int type){ struct flock lock; lock.l_type=type; lock.l_start=0; lock.l_whence=SEEK_SET; lock.l_len = 0; lock.l_pid=-1; fcntl(fd,F_GETLK,&lock); if(lock.l_type!=F_UNLCK){ if(lock.l_type == F_RDLCK) printf("Read lock already set by %d!\n",lock.l_pid); else if(lock.l_type == F_WRLCK) printf("Write lock already set by %d!\n",lock.l_pid); } lock.l_type = type; //此处的F_SETLKW为F_SETLK的阻塞版本,当无法获取锁时进入睡眠等待状态 if(fcntl(fd,F_SETLKW,&lock)<0){ printf("Lock failed:type=%d!\n",lock.l_type); exit(1); } switch(lock.l_type){ case F_RDLCK: printf("read lock set by %d\n",getpid()); break; case F_WRLCK: printf("write lock set by %d\n",getpid()); break; case F_UNLCK: printf("UN lock set by %d\n",getpid()); break; default: break; } }
#include<stdio.h> #include<fcntl.h> #include<unistd.h> #include"lock.c" int main(){ int fd; fd=open("readme",O_RDWR|O_CREAT,0666); if(fd<0){ printf("Open file error\n"); exit(1); } lock_set(fd,F_WRLCK); getchar(); lock_set(fd,F_UNLCK); getchar(); return 0; }
上读锁
#include<stdio.h> #include<fcntl.h> #include<unistd.h> #include"lock.c" int main(){ int fd; fd=open("readme",O_RDWR|O_CREAT,0666); if(fd<0){ printf("Open file error\n"); exit(1); } lock_set(fd,F_RDLCK); getchar(); lock_set(fd,F_UNLCK); getchar(); return 0; }
在两个终端中测试:
两个终端可以同时加上读锁。
有一个终端加上读锁,则必须等读锁释放才能加写锁。
有一个终端加写锁必须释放才能加别的锁。