表头文件  #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. OpenGL中shader使用

    学了接近一个月的OpenGL,最终要排上用场了...好吧,就从学到的shader(着色器)开刀吧. 先简单的介绍shader,shader事实上是显卡的功能,就是利用显卡的GPU去做图像处理的工作,而 ...

  2. 09应用输入经理旋转场景--《猿学校课程Unity3d》

    为什么极品飞车游戏等.,我们可以通过系统设置非常的方面根据自己喜欢的操作模式设置,有些人喜欢用箭头来控制不喜欢与使用"W,S,A,D"控制,这就解释程序猿不会死在程序写入内部控制, ...

  3. hdu 1159 Common Subsequence (dp乞讨LCS)

    Common Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  4. Android:创建耐磨应用 - 定义自己的布局

    创建自己的自定义布局(Creating Custom Layouts) 本文介绍如何创建自己的自定义通知和使用可穿戴UI库来创建自己的自定义布局同时你还需要知道耐磨设计标准(Wear Design P ...

  5. struts2跳转类型解析

    struts 2 跳转类型 1.dispatcher  dispatcher 为默认跳转类型.用于返回一个视图资源 xml代码 : <result name="success" ...

  6. python 3.4.0 简单的print &#39;hello world&#39;,出错--SyntaxError: invalid syntax

    问题描写叙述: win7下安装的python 3.4.0版本号, 在命令行里写入简单的输出语句: print 'hello world' 然后enter,结果返回结果为: SyntaxError: i ...

  7. ubuntu经常使用的命令摘要

    1.df命令 # df -ha 显示所有文件和分区的使用 # df -h /dev/sda1 显示sda1磁盘使用率 # df -T 显示文件系统名称属于每个分区.区的格式类型(比方ext3) 注:h ...

  8. Android采访开发——2.通用Android基础笔试题

    注意finddreams博客: http://blog.csdn.net/finddreams/article/details/44219231 正值跳槽的热季.整理一下Android面试中最常考的笔 ...

  9. 一个由proguard与fastJson引起的血案(转)

    更新微信sdk导致ComposeData中的内部类ComposeDataSender方法被混淆 根本原因,fastjson使用姿势不对. 问题描述: 一个发件人列表里,应当呈现的数据(这里命名为Com ...

  10. 流动python - 写port扫描仪和各种并发尝试(多线程/多进程/gevent/futures)

    port扫描仪的原理非常easy.没有什么比操作更socket,能够connect它认为,port打开. import socket def scan(port): s = socket.socket ...