表头文件  #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));
            }
}

linux flock()的更多相关文章

  1. 使用linux flock文件锁实现任务锁定避免计划任务程序冲突

    格式:flock [-sxun][-w #] fd#flock [-sxon][-w #] file [-c] command选项-s, --shared: 获得一个共享锁 -x, --exclusi ...

  2. linux使用flock文件锁解决crontab冲突问题

    * * * * * flock -xn /dev/shm/redis.lock -c "/usr/local/bin/redis-server" 可以用flock命令,配合使用rs ...

  3. linux使用flock文件锁

    使用linux flock 文件锁实现任务锁定,解决冲突 格式: flock [-sxun][-w #] fd# flock [-sxon][-w #] file [-c] command flock ...

  4. Linux multiple open a device

    Linux multiple open a device a device = /dev/wiegand Linux在多次打开同一个设备(/dev/wiegand)的时候,打开结果都是成功,但是在用w ...

  5. Linux 定时任务的配置

    通常我们会需要定时启动一些shell脚本,类似Windows中的Task Scheduler, 下面是在AWS EMR Cluster 主几点上配置的步骤: 1. 先创建一个shell脚本,将需要执行 ...

  6. Linux 驱动开发

    linux驱动开发总结(一) 基础性总结 1, linux驱动一般分为3大类: * 字符设备 * 块设备 * 网络设备 2, 开发环境构建: * 交叉工具链构建 * NFS和tftp服务器安装 3, ...

  7. linux cron计划任务防止多个任务同时运行

    使用linux flock 文件锁实现任务锁定,解决冲突格式:flock [-sxun][-w #] fd#flock [-sxon][-w #] file [-c] command选项-s, --s ...

  8. Nginx基础知识之————RTMP模块专题(实践文档)

    on_publish 语法:on_publish url上下文:rtmp, server, application描述:这个可以设置为一个API接口(GET方式接受所有参数),会给这个API接口返回8 ...

  9. (原创)Rocketmq分布式消息队列的部署与监控

    -------------------------------------------------------------------------------------------- 一.Rocke ...

随机推荐

  1. 使用IntelliJ IDEA配置Tomcat(详细操作)

    一,下载Tomcat 1.进入官网Http://tomcat.apache.org/,选择download,下载所需要的Tomcat版本.(注意:最好下载Tomcat 7 或者Tomcat 8 因为最 ...

  2. HDU-2068 RPG的错排(组合, 错排)

    RPG的错排 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Sub ...

  3. Codeforces Round #545 (Div. 2) C. Skyscrapers (离散化)

    题目传送门 题意: 给你n*m个点,每个点有高度h [ i ][ j ] ,用[1,x][1,x]的数对该元素所处十字上的所有元素重新标号, 并保持它们的相对大小不变.n,m≤1000n,m≤1000 ...

  4. redis关闭和启动

    redis关闭 到redis节点目录下执行如下命令 redis-cli -p 端口号 shutdown redis启动 ./redis-server 参数 参数:redis.conf文件全路径 需要到 ...

  5. 问题 J: 老肖数等式

    问题 J: 老肖数等式 时间限制: 1 Sec  内存限制: 128 MB提交: 1594  解决: 741[提交] [状态] [命题人:jsu_admin] 题目描述 老肖前几年当了小学数学老师,他 ...

  6. windwos下nginx 配置https并http强制跳转https

    windwos下nginx  配置https并http强制跳转https 一.首先配置证书文件 申请证书文件,这里就不做详细过程了,直接看证书文件结果. 这是两个证书的关键文件 打开ngxin下con ...

  7. linux 根目录扩容

    之前搭建了一个CentOS(7.0, x64)的VM,一直没留意它的硬盘空间.昨天,系统突然弹出警示,说 根目录 空间不足了. 这样的话,就只能给它增加空间呗. 我自己其实已经增加过硬盘空间了,但是为 ...

  8. C++基础之static(静态)变量

    static 表示静态   作用: 1.在函数体内,静态变量的值维持不变(记忆功能) 2.是一个本地的全局函数,即只能被本模块的函数访问(隐藏功能)   static变量: static全局变量和普通 ...

  9. .net Console.ReadLine无效

    代码中出现了 Console.ReadLine无效解决办法:把应用程序的输出类型改为 控制台应用程序

  10. SSM中前台传数组。后台接受的问题

    当时写得时候,忘记考虑json的jar,做个记录. 第一步:先带入jar <dependency> <groupId>com.fasterxml.jackson.core< ...