zoukankan      html  css  js  c++  java
  • fcntl记录锁实例

    fcntl 函数是一个相当常用的对文件进行加锁操作的函数。
    文件锁包括强制锁、建议锁、记录锁, 不过一般系统和内核都是用的强制锁。

    以下为记录锁的例子:-----------------------------------------------
    记录锁分为读锁、写锁。。
    读锁是共享锁,可以有多把,多个进程可以同时对一个文件读。
    写锁是互斥锁,同一时间只能有一个进程写文件。


    fcntl的函数原型:int  fcntl(int fd, int cmd, struct flock *lock);

    参数说明:fd是文件描述符;cmd 相当于命令符,F_SETLK和F_GETLK 是其常用值;flock的一个锁相关信息的结构体
     
    struct flock
     
    {
        short l_type;
        off_t l_start;
        short l_whence;
        off_t l_len;
        pid_t l_pid;
    };
     
    l_type有三种取值:F_RDLCK(读锁),F_WRLCK(写锁),F_UNLCK(解锁);(也即0.1.2三个值);
     
    l_whence也有三种取值:SEEK_SET(文件开头),SEEK_END(文件结尾),SEEK_CUR(当前位置);
     
    l_len 是加锁区域的长度;
     
    l_pid 是加锁进程的pid号;


    技巧:为加锁整个文件,通常的方法是将l_start说明为0,l_whence说明为SEEK_SET,l_len说明为0。

    ================================================================================================================
    set.cpp 为读锁时,在 get.cpp 调用fcntl获取读锁的信息是获取不到的,因为读锁可能有很多把,在get.cpp 中调用fcntl直接返回了F_UNLCK. 而写锁只有一把,所以在set.cpp 中设置写锁时,get.cpp中能正确返回写锁的信息。

    set.cpp--------------------------------------------------------------------------------------------------------
    实例:
    #include <unistd.h>
    #include <errno.h>
    #include <string.h>
    #include <fcntl.h>
    #include <iostream>
    using namespace std;

    int main()
    {
        struct flock flo;
        memset(&flo, 0, sizeof(struct flock));
        flo.l_type = F_WRLCK;
        flo.l_whence = SEEK_SET;
        flo.l_start = 0;
        flo.l_len = 0;

        int fd;
        if(0 == (fd=open("/home/qinwg/qinwg/function/1.txt", O_RDWR)))
        {
            cout<<"can not open file,error!"<<endl;
        }
        if(-1 == fcntl(fd, F_SETLK, &flo))
        {
            cout<<strerror(errno)<<endl;
            close(fd);
            return 1;
        }

        while(1)
        {}
        //fcntl(fd, F_GETLK, &flo);
        //if(flo.l_type == F_WRLCK)
         //   cout<<"yes"<<endl;

        return 0;
    }

    get.cpp---------------------------------------------------------------------------------------------
    #include <unistd.h>
    #include <errno.h>
    #include <string.h>
    #include <fcntl.h>
    #include <iostream>
    using namespace std;

    int main()
    {
        struct flock flo;
        memset(&flo, 0, sizeof(struct flock));
        int fd;
        if(0 == (fd=open("/home/qinwg/qinwg/function/1.txt", O_RDWR)))
        {
            cout<<"can not open file,error!"<<endl;
        }

        //struct flock test;
        fcntl(fd, F_GETLK, &flo);
        if(flo.l_type == F_WRLCK)
            cout<<"WRLOCK"<<endl;

        close(fd);

        return 0;
    }

    注意:一定要在两个程序中测试F_GETLK命令,如果在一个程序里面,肯定会是解锁状态。。自己对自己是解锁状态的。。切记

  • 相关阅读:
    做事要趁早
    软件企业利润率知多少
    项目管理经验谈之意外事件处理
    App中调用iPhone的home + 电源键截屏功能
    WCF学习(一)
    10月博客学习一览
    设计模式之工厂模式读后感
    请教各位大鸟(关于附件上传)
    刚来的兄弟,一起扬帆起航吧
    巧用asp导出csv格式excel报表
  • 原文地址:https://www.cnblogs.com/riskyer/p/3230783.html
Copyright © 2011-2022 走看看