• linux文件锁


    1.struct flock介绍
    结构体描述
    锁类型: F_RDLCK(读共享锁), F_WRLCK(写互斥锁),和F_UNLCK(对一个区域解锁)
    锁开始: 锁位置(l_whence),相对于l_whence要锁或者解锁的区域开始位置(l_start)
    锁长度: 要锁的长度,字节计数(l_len)
    锁拥有者:记录锁的拥有进程ID,这个进程可以阻塞当前进程,仅F_GETLK形式返回
    struct flock
    {
            short l_type;    /*F_RDLCK, F_WRLCK, or F_UNLCK*/
            off_t l_start;    /*相对于l_whence的偏移值,字节为单位*/
            short l_whence;    /*从哪里开始:SEEK_SET, SEEK_CUR, or SEEK_END*/
            off_t l_len;    /*长度, 字节为单位; 0 意味着缩到文件结尾*/
            pid_t l_pid;    /*returned with F_GETLK*/
    };

    2.fcntl函数介绍

    fcntl系统调用可以用来对已打开的文件描述符进行各种控制操作以改变已打开文件的的各种属性
    原型:int fcntl(int fd, int cmd ,struct flock* lock);
    参数:
            fd是要锁定的文件描述符
            对于cmd参数,可以使用的有: F_GETLK, F_SETLK(较多使用)or F_SETLKW
            第三个参数(flockptr),指向一个flock结构指针
    返回值:如果出错,返回-1,错误原因保存在错误码errno中,如果成功则返回某个其他值
    注意:
            1)fcntl函数添加锁失败:errno=9, Bad file descriptor
            原因之一:打开文件方式为只读打开,却想添加写文件锁导致
            2)fcntl函数造成互斥的测试:一般是两个线程之间测试(一个线程上锁,另一个尝试去写)
            3)fcntl函数文件锁:两个线程之间同时上读锁不造成互斥,可以同时读取访问
            4)fcntl函数文件锁:两个不同线程对同一资源的文件描述符可以不一致(和资源的文件描述符是否相同无关)

    1. //根据锁类型封装函数
    2. #define read_lock(fd, offset, whence, len) \
    3. lock_reg((fd), F_SETLK, F_RDLCK, (offset), (whence), (len))
    4. #define readw_lock(fd, offset, whence, len) \
    5. lock_reg((fd), F_SETLKW, F_RDLCK, (offset), (whence), (len))
    6. #define write_lock(fd, offset, whence, len) \
    7. lock_reg((fd), F_SETLK, F_WRLCK, (offset), (whence), (len))
    8. #define writew_lock(fd, offset, whence, len) \
    9. lock_reg((fd), F_SETLKW, F_WRLCK, (offset), (whence), (len))
    10. #define un_lock(fd, offset, whence, len) \
    11. lock_reg((fd), F_SETLK, F_UNLCK, (offset), (whence), (len))
    12. int lock_reg(int fd, int cmd, int type, off_t offset, int whence, off_t len){
    13. struct flock lock;
    14. lock.l_type = type;
    15. lock.l_start = offset;
    16. lock.l_whence = whence;
    17. lock.l_len = len;
    18. return(fcntl(fd, cmd, &lock));
    19. }
    20. //核心代码
    21. static int function_write() //写文件锁
    22. {
    23. /* 写文件前设置写锁 */
    24. if(write_lock(fileno(host_fp), 0, SEEK_SET, 0) == -1)
    25. {
    26. fprintf(stderr,"%s: write_lock fail: %s, sleep 5s, try again.\n", __FUNCTION__, strerror(errno));
    27. sleep(5);
    28. write_lock(fileno(host_fp), 0, SEEK_SET, 0);
    29. }
    30. /*
    31. 写操作
    32. ...
    33. ...
    34. ...
    35. */
    36. /* 写完后要释放锁 */
    37. if(un_lock(fileno(host_fp), 0, SEEK_SET, 0) == -1)
    38. {
    39. fprintf(stderr,"%s: unlock fail: %s, sleep 1s, try again.\n", __FUNCTION__, strerror(errno));
    40. sleep(1);
    41. un_lock(fileno(host_fp), 0, SEEK_SET, 0);
    42. }
    43. }
    44. static int function_read() //读文件锁
    45. {
    46. if(read_lock(fileno(fp), 0, SEEK_SET, 0) == -1)
    47. {
    48. fprintf(stderr,"%s: read_lock fail: %s. sleep 5s, try again.\n", __FUNCTION__, strerror(errno));
    49. sleep(5);
    50. read_lock(fileno(fp), 0, SEEK_SET, 0);
    51. }
    52. /*
    53. 读文件操作
    54. ...
    55. ...
    56. ...
    57. */
    58. if(un_lock(fileno(fp), 0, SEEK_SET, 0) == -1)
    59. {
    60. fprintf(stderr,"%s: unlock fail: %s. sleep 1s, try again.\n", __FUNCTION__, strerror(errno));
    61. sleep(1);
    62. un_lock(fileno(fp), 0, SEEK_SET, 0);
    63. }
    64. }

     

  • 相关阅读:
    mall :rabbit项目源码解析
    Windows启用Hyper-V详细安装Centos7教程
    【Less-CSS】初识Less,使编写 CSS 变得简洁
    如何在前端项目中管理依赖关系?
    Hadoop3:MapReduce中Reduce阶段自定义OutputFormat逻辑
    Spring Framework 学习笔记3:整合 MyBatis+JUnit
    将顺序表非零元素依次移到表的前端
    深入理解mysql执行的底层机制
    探秘TikTok社群:短视频中的共同体验
    『C++成长记』C++入门——内联函数
  • 原文地址:https://blog.csdn.net/hao745580264_dawei/article/details/126584020