表头文件  #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语言中的rand()函数用法

    rand() rand()函数作用:用来产生随机数,但是,rand()的内部实现是用线性同余法实现的,是伪随机数,由于周期较长,因此在一定范围内可以看成是随机的. rand()函数返回:返回一个范围在 ...

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

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

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

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

  5. Linux下编译C代码,出现tan函数报错的情况

    undefined reference to `tan' 但是已经包含了头文件 <math.h>了,可还是报错,说是找不到tan 这个问题的原因不是很清楚, 但是网上给出的方案,就是编译的 ...

  6. c++ 网络编程(五) LINUX下 socket编程 多种I/O函数 -以及readv和writev函数用法

    原文作者:aircraft 原文链接:https://www.cnblogs.com/DOMLX/p/9614056.html 一.多种I/O函数 前言:之前我们讲的数据传输一般Linux上用writ ...

  7. linux下信号量可设值的函数操作

    #include <sys/types.h>#include <sys/ipc.h>#include <sys/sem.h>#include <errno.h ...

  8. c语言中的malloc函数

    少壮不努力,大一的时候c语言学得不扎实,最近学数据结构的时候看到c语言中malloc函数都不知道了,这里记录一下,避免以后再忘. malloc的全称是memory allocation,中文叫动态内存 ...

  9. C语言中static关键字的用法

    C记得还是大一时学的,现在觉得好久没用了,又捧起来看看.今天刚看到有关static关键字,仔细地看了一遍<C和指针>这本书中的解释,现在觉得清楚多了. 首先,我们将static关键字,修饰 ...

随机推荐

  1. Your content must have a ListView whose id attribute is 'android.R.id.list'

    修改ListView的ID为 ' @android:id/list ' <ListView android:id="@android:id/list" android:lay ...

  2. Configure,make,make install详解

    转:http://my.oschina.net/qihh/blog/66113?fromerr=6ej3CfGJ   无论对于一个初学者还是一个资深的Linux程序员,编写Makefile文件都是一件 ...

  3. JDE客户端get时报错“ERROR:fetch from table F0101 failed”

    客户端开发时发现总报错误“ERROR:fetch from table F0101 failed” 原因是用户ID在地址名册中找不到地址号.修改用户地址号即可.如下图所示

  4. apache2.4域名配置参数

    apache2.4和 2.2版本的配置有区别 2.4的配置如下 <VirtualHost *:80>DocumentRoot /www/web/projectServerName www. ...

  5. linux-网卡故障

    Linux 网络问题解决思路 1.查看 /etc/sysconfig/network-script/ 查看eth0和eth1的配置是否正确 2.查看 /etc/modual.conf 的配置模块是否正 ...

  6. 客户端访问AIDLService(远程绑定Service)

    import android.os.Bundle;import android.os.IBinder;import android.os.RemoteException;import android. ...

  7. 在 Linux 中怎样将 MySQL 迁移到 MariaDB 上

    自从甲骨文收购 MySQL 后,由于甲骨文对 MySQL 的开发和维护更多倾向于闭门的立场,很多 MySQL 的开发者和用户放弃了 MySQL.在社区驱动下,促使更多人移到 MySQL 的另一个叫 M ...

  8. Android 主页面顶部栏的通知Notification ,可以自定义通知消息栏的风格,并且点击通知栏进人本程序。

    常用的程序通知,显示到主页面的顶部栏. package com.lixu.tongzhi; import android.app.Activity; import android.app.Notifi ...

  9. Android 禁用以及捕捉home键

    最近要做个小项目,其中有需要禁止home键的需求,一开始以为不可以,感觉得root一下才行,后来查了一下,发现还是不少朋友都实现了这个功能,现在也引用一下,供大家参考一下: 1. 在activity中 ...

  10. Ajax返回类型JSON,XML

    Ajax的三种返回类型 **一.TEXT *二.JSON 数据显示页面代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transiti ...