表头文件  #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下一个C语言flock功能使用 .的更多相关文章

  1. linux下一个C语言要求CPU采用

    部分   从灾难中 本来我想写一个小程序来测试CPU其他工具利用它可以检验类数据的性能.以后参考IPbench中间cpu_target_lukem插件实现我们的功能.原理非常简单:就是我们给程序设置了 ...

  2. Linux 下 expect 脚本语言中交互处理常用命令

    Linux 下 expect 脚本语言中交互处理常用命令 1. #!/usr/bin/expect 告诉操作系统脚本里的代码使用那一个 shell 来执行.这里的 expect 其实和 Linux 下 ...

  3. linux下的C语言开发 进程创建 延伸的几个例子

    在Linux下面,创建进程是一件十分有意思的事情.我们都知道,进程是操作系统下面享有资源的基本单位.那么,在linux下面应该怎么创建进程呢?其实非常简单,一个fork函数就可以搞定了.但是,我们需要 ...

  4. 在 Linux 下学习 C 语言有什么好处?

    作者:宅学部落链接:https://www.zhihu.com/question/23893390/answer/832610610来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请 ...

  5. Linux 下一个很棒的命令行工具

    导读 Taskwarrior 是 Ubuntu/Linux 下一个简单而直接的基于命令行的 TODO 工具.这个开源软件是我曾用过的最简单的基于命令行的工具之一.Taskwarrior 可以帮助你更好 ...

  6. 在Linux下开始C语言的学习

    为什么要在linux下学习C语言? linux下可以体验到最纯粹的C语言编程,可以抛出其他IDE的影响 环境配置简单,一条命令就足够.甚至对于大多数linux发行版本,都已经不需要配置C语言的环境 查 ...

  7. Linux下一个简单的日志系统的设计及其C代码实现

    1.概述 在大型软件系统中,为了监测软件运行状况及排查软件故障,一般都会要求软件程序在运行的过程中产生日志文件.在日志文件中存放程序流程中的一些重要信息, 包括:变量名称及其值.消息结构定义.函数返回 ...

  8. 《Linux下cp XXX1 XXX2的功能》的实现

    <Linux下cp XXX1 XXX2的功能>的实现 一.题目要求 编写MyCP.java 实现类似Linux下cp XXX1 XXX2的功能,要求MyCP支持两个参数: java MyC ...

  9. 编写MyCP.java 实现类似Linux下cp XXX1 XXX2的功能

    题目:编写MyCP.java 实现类似Linux下cp XXX1 XXX2的功能 要求:MyCP支持两个参数: java MyCP -tx XXX1.txt XXX2.bin 用来把文本文件(内容为十 ...

随机推荐

  1. WM_ERASEBKGND官方解释(翻译),以及Delphi里所有的使用情况(就是绘制窗口控件背景色,并阻止进一步传递消息)

    #define WM_ERASEBKGND                   0x0014 Parameters wParam A handle to the device context. //  ...

  2. [计算机基础]关于实体( Entity )和模型( Model )

    实体与模型的浅析 在日常开发过程中经常看到Entity,Model,DataModel,它们之间到底有什么异同?下面是我个人的一些理解. 一.Entity,Model,它们是什么? 维基百科描述: 实 ...

  3. 二、Cocos2dx概念介绍(游戏开发中不同的坐标系,cocos2dx锚点)

    注:ccp是cocos2dx中的一个宏定义,#define ccp(__X__,__Y__)CCPointMake((float)__X__, (float)__Y__),在此文章中表示坐标信息 1. ...

  4. (2)入门指南——(7)添加jquery代码(Adding our jQuery code)

    Our custom code will go in the second, currently empty, JavaScript file which we included from the H ...

  5. POJ 3176:Cow Bowling

    Cow Bowling Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13464   Accepted: 8897 Desc ...

  6. spring中bean的设计模式

    默认的是单例的. 如果不想单例需要如下配置: <bean id="user" class="..." singleton="false" ...

  7. A Game of Thrones(19) - Jon

    The courtyard rang to the song of swords. Under black wool, boiled leather, and mail, sweat trickled ...

  8. Web Api 2(Cors)Ajax跨域访问

    支持Ajax跨域访问ASP.NET Web Api 2(Cors)的简单示例教程演示   随着深入使用ASP.NET Web Api,我们可能会在项目中考虑将前端的业务分得更细.比如前端项目使用Ang ...

  9. HTML5之画布的拖拽/拖放

    <!DOCTYPE HTML> <html> <head> <script type="text/javascript"> func ...

  10. poj 1221 UNIMODAL PALINDROMIC DECOMPOSITIONS (母函数)

    /* 给出一个数n,把它拆分成若干个数的和,要求最大的数在中间并向两边非递增.问拆法有多少种. 母函数.枚举中间的那一个数.由于左右对称.所以仅仅须要求左边部分的方案就可以. 注意,左右两部分的取数必 ...