linux下C语言中的flock函数用法 .

来源:互联网 发布:mac vmware 安装win7 编辑:程序博客网 时间:2024/04/30 15:18

该结构是在lock.h文件中定义。

lock.h File

 

功能

定义一些文件的锁的选项

Description

The flock structure in the /usr/include/sys/flock.h file, which describes a lock, contains the following fields:

 

l_typeDescribes the type of lock. If the value of the Command parameter to the fcntlsubroutine is F_SETLK orF_SETLKW, the l_type field indicates the type of lock to be created. Possible values are:
F_RDLCK
A read lock is requested.
F_WRLCK
A write lock is requested.
F_UNLCK
Unlock. An existing lock is to be removed.

If the value of the Command parameter to the fcntl subroutine is F_GETLK, the l_typefield describes an existing lock. Possible values are:

F_RDLCK
A conflicting read lock exists.
F_WRLCK
A conflicting write lock exists.
F_UNLCK
No conflicting lock exists.
l_whenceDefines the starting offset. The value of this field indicates the point from which the relative offset, the l_startfield, is measured. Possible values are:
SEEK_SET
The relative offset is measured from the start of the file.
SEEK_CUR
The relative offset is measured from the current position.
SEEK_END
The relative offset is measured from the end of the file.

These values are defined in the unistd.h file.

l_startDefines the relative offset in bytes, measured from the starting point in the l_whencefield.l_lenSpecifies the number of consecutive bytes to be locked.l_sysidContains the ID of the node that already has a lock placed on the area defined by thefcntl subroutine. This field is returned only when the value of the Command parameter isF_GETLK.l_pidContains the ID of a process that already has a lock placed on the area defined by thefcntl subroutine. This field is returned only when the value of the Command parameter isF_GETLK.

l_vfs

Specifies the file system type of the node identified in the l_sysid field.

 

 

看一下示例吧!

[cpp] view plaincopy
  1. int  
  2. waldirlock(Wal *w)  
  3. {  
  4.     int r;  
  5.     int fd;  
  6.     struct flock lk;  
  7.     char path[PATH_MAX];  
  8.   
  9.     r = snprintf(path, PATH_MAX, "%s/lock", w->dir);  
  10.     if (r > PATH_MAX) {  
  11.         twarnx("path too long: %s/lock", w->dir);  
  12.         return 0;  
  13.     }  
  14.   
  15.     fd = open(path, O_WRONLY|O_CREAT, 0600);  
  16.     if (fd == -1) {  
  17.         twarn("open");  
  18.         return 0;  
  19.     }  
  20.   
  21.     lk.l_type = F_WRLCK;  
  22.     lk.l_whence = SEEK_SET;  
  23.     lk.l_start = 0;  
  24.     lk.l_len = 0;  
  25.     r = fcntl(fd, F_SETLK, &lk);  
  26.     if (r) {  
  27.         twarn("fcntl");  
  28.         return 0;  
  29.     }  
  30.   
  31.     // intentionally leak fd, since we never want to close it  
  32.     // and we'll never need it again  
  33.     return 1;  
  34. }  

 

struct flock 作为fcntl函数的第三个参数,使用F_SETLK,设置了其参数。


flock() 的函数原型如下所示:
     int flock(int fd, int operation);

其中,参数 fd 表示文件描述符;参数 operation 指定要进行的锁操作,该参数的取值有如下几种:
     LOCK_SH:表示要创建一个共享锁,在任意时间内,一个文件的共享锁可以被多个进程拥有;
     LOCK_EX:表示创建一个排他锁,在任意时间内,一个文件的排他锁只能被一个进程拥有;
     LOCK_UN:表示删除该进程创建的锁;
     LOCK_MAND:它主要是用于共享模式强制锁,它可以与 LOCK_READ 或者 LOCK_WRITE 联合起来使用,从而表示是否允许并发的读操作或者并发的写操作;
    
通常情况下,如果加锁请求不能被立即满足,那么系统调用 flock() 会阻塞当前进程。比如,进程想要请求一个排他锁,但此时,已经由其他进程获取了这个锁,那么该进程将会被阻塞。如果想要在没有获得这个排他锁的情况下不阻塞该进程,可以将 LOCK_NB 和 LOCK_SH 或者 LOCK_EX 联合使用,那么系统就不会阻塞该进程。flock() 所加的锁会对整个文件起作用。

注意:
     1. 对于文件的 close() 操作会使文件锁失效;
     2. 同理,进程结束后文件锁失效;
     3. flock() 的 LOCK_EX 是“劝告锁”,系统内核不会强制检查锁的状态,需要在代码中进行文件操作的地方显式检查才能生效。

 1030人阅读 评论(0) 收藏 举报

  表头文件  #include<sys/file.h>

  定义函数  int flock(int fd,int operation);

  函数说明  flock()会依参数operation所指定的方式对参数fd所指的文件做各种锁定或解除锁定的动作。此函数只能锁定整个文件,无法锁定文件的某一区域。

  参数  operation有下列四种情况:

  LOCK_SH 建立共享锁定。多个进程可同时对同一个文件作共享锁定。

  LOCK_EX 建立互斥锁定。一个文件同时只有一个互斥锁定。

  LOCK_UN 解除文件锁定状态。

  LOCK_NB 无法建立锁定时,此操作可不被阻断,马上返回进程。通常与LOCK_SH或LOCK_EX 做OR(|)组合。

  单一文件无法同时建立共享锁定和互斥锁定,而当使用dup()或fork()时文件描述词不会继承此种锁定。

  返回值  返回0表示成功,若有错误则返回-1,错误代码存于errno。

 

flock只要在打开文件后,需要对文件读写之前flock一下就可以了,用完之后再flock一下,前面加锁,后面解锁。其实确实是这么简单,但是前段时间用的时候发现点问题,问题描述如下:

一个进程去打开文件,输入一个整数,然后上一把写锁(LOCK_EX),再输入一个整数将解锁(LOCK_UN),另一个进程打开同样一个文件,直接向文件中写数据,发现锁不起作用,能正常写入(我此时用的是超级用户)。google了一大圈发现flock不提供锁检查,也就是说在用flock之前需要用户自己去检查一下是否已经上了锁,说明白点就是读写文件之前用一下flock检查一下文件有没有上锁,如果上锁了flock将会阻塞在那里(An attempt to lock the file using one of these file descriptors may be denied by a lock that the calling process has already placed via another descriptor ),除非用了LOCK_NB。一个完整的用于测试的事例代码如下所示:

//lockfile.c

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>

int main()
{
    int fd,i;
    char path[]="/home/taoyong/test.txt";
    extern int errno;
    fd=open(path,O_WRONLY|O_CREAT);
    if(fd!=-1)
        {
        printf("open file %s ./n",path);
        printf("please input a number to lock the file./n");
        scanf("%d",&i);
        if(flock(fd,LOCK_EX)==0)
            {
            printf("the file was locked./n");
            }
        else
            {
            printf("the file was not locked./n");
            }
        printf("please input a number to unlock the file./n");
        scanf("%d",&i);
        if(flock(fd,LOCK_UN)==0)
            {
            printf("the file was unlocked./n");
            }
        else
            {
            printf("the file was not unlocked./n");
            }
        close(fd);

        }
    else
        {
        printf("cannot open file %s/n",path);
        printf("errno:%d/n",errno);
        printf("errMsg:%s",strerror(errno));
        }
}

 

//testprocess.c

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/file.h>

int main()
{
    int fd,i;
    char path[]="/home/taoyong/test.txt";
    char s[]="writing.../nwriting....../n";
    extern int errno;
    fd=open(path,O_WRONLY|O_CREAT|O_APPEND);
    if(fd!=-1)
            {
        printf("open file %s ./n",path);

            if(flock(fd,LOCK_EX|LOCK_NB)==0)
            {
            printf("the file was locked by the process./n");    
                if(-1!=write(fd,s,sizeof(s)))
                    {
                printf("write %s to the file %s/n",s,path);
                        }
                else
                       {
                printf("cannot write the file %s/n",path);
                printf("errno:%d/n",errno);
                printf("errMsg:%s/n",strerror(errno));
                    }        
                
            }
        else
            {
            printf("the file was locked by other process.Can't write.../n");
                printf("errno:%d:",errno);
            }
        
        close(fd);


            }
        else
           {
        printf("cannot open file %s/n",path);
        printf("errno:%d/n",errno);
        printf("errMsg:%s",strerror(errno));
            }
}

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 发票纳税人识别号写错了怎么办 电子发票忘填邮箱怎么办 公司名跟发票抬头不一样怎么办 买空调不给发票怎么办 卖苹果手机没有发票怎么办 刷机字库刷坏了怎么办 维棠不能播放视频怎么办 m3u8卡顿的厉害怎么办 下载了PDF不能用怎么办 pdf格式在电脑打不开怎么办 电脑上arm格式打不开怎么办 极米桌面无响应怎么办 jpg格式的文件打印模糊怎么办 苹果迅雷mp4格式看不了怎么办 太阳镜镜片磨花了怎么办 ∪盘插上电脑读不出怎么办 网站被路由器屏蔽了怎么办 电脑无法进入路由器设置页面怎么办 笔记本电脑无线网卡坏了怎么办 笔记本内置无线网卡坏了怎么办 笔记本无线网卡坏了怎么办 wif连上不能上网怎么办 手机wifi有个感叹号怎么办 分手不删qq微信怎么办 优盘提示被锁了怎么办 电视无线网卡插上没反应怎么办 光猫没有wan口怎么办 usb无线网卡坏了怎么办 wifi接收器电脑不识别怎么办 无线路由接收器识别不了怎么办 小米3一直闪红灯怎么办 手机拨号键删了怎么办 联想笔记本电脑无线网卡坏了怎么办 联想z360内置无线网卡坏怎么办 usb无线网卡驱动安装失败怎么办 无线网被禁用了怎么办 未连接无线通信已关闭怎么办 手机设置路由器打不开了怎么办 手机打不开登录路由器的网址怎么办 按了光猫复位后怎么办 按了猫的复位键怎么办