表头文件  #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语言中的flock函数用法 【转】

    表头文件  #include<sys/file.h> 定义函数  int flock(int fd,int operation); 函数说明  flock()会依参数operation所指 ...

  2. c语言中的rewind函数,Win CE 不支持,可用fseek函数替换

    FILE *read = fopen(cXmlFile,"rb"); if (read) { fseek(read, 0L, SEEK_END); int len = ftell( ...

  3. Linux下C语言编程实现spwd函数

    Linux下C语言编程实现spwd函数 介绍 spwd函数 功能:显示当前目录路径 实现:通过编译执行该代码,可在终端中输出当前路径 代码实现 代码链接 代码托管链接:spwd.c 所需结构体.函数. ...

  4. linux下通过脚本实现自动重启程序的方法

    无论什么程序都不可能完美无缺,理论上,任何程序都有 Core Dump 的一天,正式运营的程序,尤其是服务器程序,一旦 Core Dump ,后果不堪设想,有过服务器开发经验的朋友,一定都经历过深夜美 ...

  5. Linux下java进程CPU占用率高分析方法

    Linux下java进程CPU占用率高分析方法 在工作当中,肯定会遇到由代码所导致的高CPU耗用以及内存溢出的情况.这种情况发生时,我们怎么去找出原因并解决. 一般解决方法是通过top命令找出消耗资源 ...

  6. Kali Linux下破解WIFI密码挂载usb无线网卡的方法

    Kali Linux下破解WIFI密码挂载usb无线网卡的方法 时间:2014-10-12    来源:服务器之家    投稿:root 首先我要说的是,wifi密码的破解不是想象中的那么容易,目前还 ...

  7. linux下使用crontab定时备份MYSQL数据库的方法:

    摘要 linux下使用crontab定时备份MYSQL数据库的方法: 只需按照下面3步做,一切都在你的掌控之下: 第一步:在服务器上配置备份目录代码: ------------------------ ...

  8. Linux下查看alert日志文件的两种方法

    --linux下查看alert日志文件的两种方法: --方法1: SQL> show parameter background_dump_dest; NAME TYPE VALUE ------ ...

  9. Linux下用rm删除的文件的恢复方法

    Linux下用rm删除的文件的恢复方法_Linux教程_Linux公社-Linux系统门户网站https://www.linuxidc.com/Linux/2008-08/14744.htm linu ...

随机推荐

  1. SQL Server 2008性能故障排查(四)——TempDB

    原文:SQL Server 2008性能故障排查(四)--TempDB 接着上一章:I/O TempDB: TempDB是一个全局数据库,存储内部和用户对象还有零食表.对象.在SQLServer操作过 ...

  2. hdu3037Saving Beans

    Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission ...

  3. Windows编译Nodejs时遇到 File "configure", line 313 SyntaxError: invalid syntax Failed to create vc project files. 时的解决方法

    第一次编译的时候电脑上未安装python,遂下载了python最新版本3.3.3,但是报了下面这个错误. 把python降到2.7.*的版本即可. 我这里测试2.7.6和2.7.3版本可以正常编译.

  4. 使用log4j日志-配置载入问题

    1.在eclipse中,把log4j.properties放在类路径下,在项目启动时就会自己主动载入. 2.在idea中.把log4j.properties放在类路径下,可是项目启动时不能直接载入(原 ...

  5. hdu3480二维斜率优化DP

    Division Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 999999/400000 K (Java/Others) Tota ...

  6. 使用Advanced Installer将.exe程序重新封装为.msi程序

    原文:使用Advanced Installer将.exe程序重新封装为.msi程序 使用Advanced Installer将.exe程序重新封装为.msi程序 首先安装Advanced instal ...

  7. crmsql句子的实体关系实体字段的信息窗口

    在crm里面怎样用sql语句查询这些信息? 查询实体信息: --查询实体信息,实体名称:account select * from MetadataSchema.Entity where name= ...

  8. 解决 TortoiseGit 诡异的 Bad file number 问题(转)

    问题描述 昨天,以及今天(2014-11-29),使用 TortoiseGit 时碰到了一个诡异的问题. 卸载,清理注册表,重装,重启,各种折腾以后,还是不能解决. 但是23.45分一过,突然灵光一闪 ...

  9. 采用xshell链路本地虚拟机Linux

    昨天想安装在自己的机器看Linux.而使用xshell通路.但是这花了很长的时间,于xshell结束所有的提示"Could not connect to '192.168.54.100' ( ...

  10. Entity Framework Code First学习系列

    Entity Framework Code First学习系列目录 Entity Framework Code First学习系列说明:开发环境为Visual Studio 2010 + Entity ...