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命令,如果在一个程序里面,肯定会是解锁状态。。自己对自己是解锁状态的。。切记

  • 相关阅读:
    POJ 1659 Frogs' Neighborhood
    zoj 2913 Bus Pass(BFS)
    ZOJ 1008 Gnome Tetravex(DFS)
    POJ 1562 Oil Deposits (DFS)
    zoj 2165 Red and Black (DFs)poj 1979
    hdu 3954 Level up
    sgu 249 Matrix
    hdu 4417 Super Mario
    SPOJ (BNUOJ) LCM Sum
    hdu 2665 Kth number 划分树
  • 原文地址:https://www.cnblogs.com/riskyer/p/3230783.html
Copyright © 2011-2022 走看看