美文网首页
10.文件锁

10.文件锁

作者: 陈忠俊 | 来源:发表于2020-08-21 12:50 被阅读0次
  1. pa文件,对文件加读锁
#include <stdio.h>
#include <sys/file.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

#define E_MSG(str, value) do{perror(str); return (value);} while(0)
int main(int argc, char *argv[]){
    printf("getpid: %d\n", getpid());
    struct flock lock;
    lock.l_type = F_RDLCK;
    lock.l_whence = SEEK_SET;
    lock.l_start = 0;
    lock.l_len = 6;

    //打开文件
    int fd = open(argv[1], O_RDONLY);
    if(fd == -1) E_MSG("open", -1);
    //对指定的文件加读锁
    int f = fcntl(fd, F_SETLKW, &lock);
    if(f == -1) E_MSG("fcntl", -1);
    //运行到这里,文件加读锁成功
    printf("read lock success...\n");
    getchar();
    close(fd);
    return 0;
}
  1. pb文件,对文件加写锁
#include <stdio.h>
#include <sys/file.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

#define E_MSG(str, value) do{perror(str); return (value);} while(0)
int main(int argc, char *argv[]){
    printf("getpid: %d\n", getpid());
    struct flock lock;
    lock.l_type = F_RDLCK;
    lock.l_whence = SEEK_SET;
    lock.l_start = 0;
    lock.l_len = 6;

    //打开文件, 以写的方式打开
    int fd = open(argv[1], O_RDWR);
    if(fd == -1) E_MSG("open", -1);
    //对指定的文件加写锁
    int f = fcntl(fd, F_SETLK, &lock);
    if(f == -1) E_MSG("fcntl", -1);
    //运行到这里,文件加读锁成功
    printf("read lock success...\n");
    getchar();
    close(fd);
    return 0;
}
  1. pc文件
#include <stdio.h>
#include <sys/file.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

#define E_MSG(str, value) do{perror(str); return (value);} while(0)
int main(int argc, char *argv[]){
    struct flock lock;
    //open file with read & write
    int fd = open(argv[1], O_RDWR);
    if(fd == -1) E_MSG("OPEN", -1);
    //inital struct members
    lock.l_type = F_RDLCK;
    lock.l_whence = SEEK_SET;
    lock.l_start = 0;
    lock.l_len = 6;

    //test now, if add lock true
    int cnt = fcntl(fd, F_GETLK, &lock);
    if(cnt == -1) E_MSG("fcntl", -1);
    if(lock.l_type == F_UNLCK) // add lock OK
        printf("Add lock success..\n");
    else //can't add lock
        printf("Pid == %d\n", lock.l_pid);
    close(fd);
    return 0;
}

相关文章

  • 10.文件锁

    pa文件,对文件加读锁 pb文件,对文件加写锁 pc文件

  • 10. 锁的作用

    锁的作用 保障原子性 互斥,一个锁一次只能被一个线程持有,保证了临界区代码一次只能被一个线程执行,这使得临界区代码...

  • JavaScript-DOM

    操作DOM 更新DOM 9.表单 10.操作文件 11.回调

  • 10.文件操作

    1. EOF 从一个文本文件顺序读入字符并在屏幕上显示出来: 注意:EOF不是可输出字符,因此不能在屏幕上显示。由...

  • 文件锁

  • Linux系统编程7:读写锁

    1. 接口 1.1 锁操作 参数 锁信息 返回值 给指定文件添加读锁 查看当前锁的状态 给指定文件添加写锁 给指定...

  • JAVA NIO 文件锁FileLock

    文件锁可以是shared(共享锁)或者exclusive(排他锁)。不是所有的平台都以同一种方式实现文件锁,不同的...

  • C++锁

    锁的种类 互斥锁、条件锁、自旋锁、读写锁、递归锁。 互斥锁 头文件: 类型:pthread_mutex_t, 函数...

  • 文件打包打包压缩

    10. Linux文件打包压缩 一、压缩技术解密 https://www.jianshu.com/p/7db19f...

  • Linux 文件锁

    系统调用fcntl 参数:fd:文件描述符cmd:F_GETLK, F_SETLK or F_SETLKWlock...

网友评论

      本文标题:10.文件锁

      本文链接:https://www.haomeiwen.com/subject/lyvvmhtx.html