六、Linux系统编程:读写锁

5 读写锁

读写锁(ReentrantReadWriteLock)就是读线程和读线程之间不互斥。
读读不互斥,读写互斥,写写互斥

一个资源可以被多个读线程访问,也可以被一个写线程访问,但不能同时存在读写线程,读写互斥,读读共享

5.1 锁操作

int fcntl(int fd, int cmd, struct flock *lock)
  • 参数
参数含义
fd文件描述符
cmd命令。F_GETLK:获取锁;F_SETLK:设置锁;F_SETLKW:设置阻塞锁。
struct flock锁信息
  • 锁信息
参数含义
l_typeF_RDLCK:读取锁(共享锁);F_WRLCK:写入锁(排斥锁);F_UNLCK:解锁
l_whenceSEEK_SET:以文件开头为锁定的起始位置;SEEK_CUR:以目前文件读写位置为锁定的起始位置;SEEK_END:以文件结尾为锁定的起始位置
l_start相对l_whence位置的偏移量
l_len锁定区域的长度。0表示到整个文件的结束
l_pid当前占用锁的PID,只对F_GETLK命令有效
  • 返回值
返回值含义
-1失败
0SET相关命令成功
  • 给指定文件添加读锁
#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 死锁

在这里插入图片描述
在这里插入图片描述

#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();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值