5 读写锁
读写锁(ReentrantReadWriteLock)就是读线程和读线程之间不互斥。
读读不互斥,读写互斥,写写互斥
一个资源可以被多个读线程访问,也可以被一个写线程访问,但不能同时存在读写线程,读写互斥,读读共享
5.1 锁操作
int fcntl(int fd, int cmd, struct flock *lock)
参数 |
含义 |
fd |
文件描述符 |
cmd |
命令。F_GETLK :获取锁;F_SETLK :设置锁;F_SETLKW :设置阻塞锁。 |
struct flock |
锁信息 |
参数 |
含义 |
l_type |
F_RDLCK :读取锁(共享锁);F_WRLCK :写入锁(排斥锁);F_UNLCK :解锁 |
l_whence |
SEEK_SET :以文件开头为锁定的起始位置;SEEK_CUR :以目前文件读写位置为锁定的起始位置;SEEK_END :以文件结尾为锁定的起始位置 |
l_start |
相对l_whence 位置的偏移量 |
l_len |
锁定区域的长度。0 表示到整个文件的结束 |
l_pid |
当前占用锁的PID ,只对F_GETLK 命令有效 |
#include <stdio.h>
#include <fcntl.h>
int main(int argc, char* argv[]){
if(2!=argc){
printf("usage:%s <pathname>\n",argv[0]);
return 1;
}
int fd = open(argv[1],O_RDONLY);
if(-1 == fd){
perror("open error");
return 1;
}
struct flock lock;
lock.l_type = F_RDLCK;
lock.l_whence = SEEK_SET;
lock.l_start = 0;
lock.l_len = 0;
if(-1 == fcntl(fd,F_SETLK,&lock)){
perror("fcntl error");
return 1;
}
pause();
close(fd);
}
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
int main(int argc, char* argv[]){
if(2!=argc){
printf("usage:%s <pathname>\n",argv[0]);
return 1;
}
int fd = open(argv[1],O_RDWR);
if(-1 == fd){
perror("open error");
return 1;
}
struct flock lock;
bzero(&lock,sizeof(lock));
if(-1 == fcntl(fd,F_GETLK,&lock)){
perror("fcntl error");
return 1;
}
printf("file:%s,lock type:%d,start:%d,len:%d,by %d\n",argv[1],lock.l_type,lock.l_start,lock.l_len,lock.l_pid);
}
#include <stdio.h>
#include <fcntl.h>
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>
int fd;
void handler(int sig){
struct flock lock;
lock.l_type = F_UNLCK;
lock.l_whence = SEEK_SET;
lock.l_start = 0;
lock.l_len = 0;
if(-1 == fcntl(fd,F_SETLKW,&lock)){
perror("fcntl error");
exit(1);
}
}
int main(int argc, char* argv[]){
signal(SIGUSR1,handler);
int c,start = 0,len = 0;
while((c = getopt(argc,argv,"s:l:"))!=-1){
switch(c){
case 's':
start = atoi(optarg);
break;
case 'l':
len = atoi(optarg);
break;
}
}
if(optind != argc -1){
printf("usage:%s [-s <start>] [-l <len>] <pathname>\n",argv[0]);
return 1;
}
fd = open(argv[optind],O_WRONLY);
if(-1 == fd){
perror("open error");
return 1;
}
struct flock lock;
lock.l_type = F_WRLCK;
lock.l_whence = SEEK_SET;
lock.l_start = start;
lock.l_len = len;
if(-1 == fcntl(fd,F_SETLKW,&lock)){
perror("fcntl error");
return 1;
}
pause();
for(;;);
close(fd);
}
// 本程序不能解锁,因为只有加锁进程才能执行解锁操作
#include <stdio.h>
#include <fcntl.h>
int main(int argc, char* argv[]){
if(2!=argc){
printf("usage:%s <pathname>\n",argv[0]);
return 1;
}
int fd = open(argv[1],O_WRONLY);
if(-1 == fd){
perror("open error");
return 1;
}
struct flock lock;
lock.l_type = F_UNLCK;
lock.l_whence = SEEK_SET;
lock.l_start = 0;
lock.l_len = 0;
if(-1 == fcntl(fd,F_SETLKW,&lock)){
perror("fcntl error");
return 1;
}
pause();
close(fd);
}
5.2 死锁
![在这里插入图片描述](https://img-blog.csdnimg.cn/f161cd48d5324e668ae6932f1462d586.png)
![在这里插入图片描述](https://img-blog.csdnimg.cn/afcee55ac7d54117881fd042be0f5aaa.png)
#include <stdio.h>
#include <fcntl.h>
#include <signal.h>
#include <stdlib.h>
void lock(const char* pathname){
int fd = open(pathname,O_WRONLY);
if(-1 == fd){
perror("open error");
exit(1);
}
struct flock lock;
lock.l_type = F_WRLCK;
lock.l_whence = SEEK_SET;
lock.l_start = 0;
lock.l_len = 0;
if(-1 == fcntl(fd,F_SETLKW,&lock)){
perror("fcntl error");
exit(1);
}
//close(fd);
}
int main(int argc, char* argv[]){
if(3!=argc){
printf("usage:%s <pathname1> <pathname2>\n",argv[0]);
return 1;
}
printf("PID:%d lock file %s\n",getpid(),argv[1]);
lock(argv[1]);
printf("sleep 1s\n");
sleep(1);
printf("PID:%d lock file %s\n",getpid(),argv[2]);
lock(argv[2]);
pause();
}