linux下C语言中的flock函数使用方法 .
表头文件 #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函数使用方法 .的更多相关文章
- linux下C语言中的flock函数用法 【转】
表头文件 #include<sys/file.h> 定义函数 int flock(int fd,int operation); 函数说明 flock()会依参数operation所指 ...
- c语言中的rewind函数,Win CE 不支持,可用fseek函数替换
FILE *read = fopen(cXmlFile,"rb"); if (read) { fseek(read, 0L, SEEK_END); int len = ftell( ...
- Linux下C语言编程实现spwd函数
Linux下C语言编程实现spwd函数 介绍 spwd函数 功能:显示当前目录路径 实现:通过编译执行该代码,可在终端中输出当前路径 代码实现 代码链接 代码托管链接:spwd.c 所需结构体.函数. ...
- linux下通过脚本实现自动重启程序的方法
无论什么程序都不可能完美无缺,理论上,任何程序都有 Core Dump 的一天,正式运营的程序,尤其是服务器程序,一旦 Core Dump ,后果不堪设想,有过服务器开发经验的朋友,一定都经历过深夜美 ...
- Linux下java进程CPU占用率高分析方法
Linux下java进程CPU占用率高分析方法 在工作当中,肯定会遇到由代码所导致的高CPU耗用以及内存溢出的情况.这种情况发生时,我们怎么去找出原因并解决. 一般解决方法是通过top命令找出消耗资源 ...
- Kali Linux下破解WIFI密码挂载usb无线网卡的方法
Kali Linux下破解WIFI密码挂载usb无线网卡的方法 时间:2014-10-12 来源:服务器之家 投稿:root 首先我要说的是,wifi密码的破解不是想象中的那么容易,目前还 ...
- linux下使用crontab定时备份MYSQL数据库的方法:
摘要 linux下使用crontab定时备份MYSQL数据库的方法: 只需按照下面3步做,一切都在你的掌控之下: 第一步:在服务器上配置备份目录代码: ------------------------ ...
- Linux下查看alert日志文件的两种方法
--linux下查看alert日志文件的两种方法: --方法1: SQL> show parameter background_dump_dest; NAME TYPE VALUE ------ ...
- Linux下用rm删除的文件的恢复方法
Linux下用rm删除的文件的恢复方法_Linux教程_Linux公社-Linux系统门户网站https://www.linuxidc.com/Linux/2008-08/14744.htm linu ...
随机推荐
- matlab学习------------普通dialog对话框,错误对话框errordlg,警告对话框warndlg
Dialog对话框 语法: h = dialog('PropertyName',PropertyValue,...) 对话框的默认属性 WindowStyle的值: {normal} | moda ...
- 用python做自己主动化測试--对server端的自己主动化測试(3)-很多其它http client实例
上一篇中仅仅是实现了一个非常easy的http client功能,request还提供了keep alive, SSL, 多文件上传,cookie 管理功能,http requests头管理等丰富的功 ...
- ssh连接失败,排错经验(转)
一.场景描述 ssh连接服务器,发现连接失败,但是对应服务器的ip能够ping通. 场景: [root@yl-web ~]# ssh root@10.1.101.35 ssh_exchange_ide ...
- linux处置服务Iptables
一:Iptables防火墙服务 iptables分为两个部分:一个部分在内核中实现,一个为用户接口命令iptables,用户通过该命令来改动防火墙的功能.所以,iptables要使用对应的功能.必需要 ...
- 【Linux命令】--(9)其他常用命令
其他常用命令+++++++++++++++++++++++++++++++lndiffdatecal grep wcpswatchatcrontab++++++++++++++++++++++++++ ...
- css3 menu 手机菜单1
首先看一下效果图; 效果1,主要是 translateY(100px) -->translateY(0px);opacity:0;—>opacity: 1; 然后递归延迟 怕麻烦也可以自己 ...
- UVA 1364 - Knights of the Round Table (获得双连接组件 + 二部图推理染色)
尤其是不要谈了些什么,我想A这个问题! FML啊.....! 题意来自 kuangbin: 亚瑟王要在圆桌上召开骑士会议.为了不引发骑士之间的冲突. 而且可以让会议的议题有令人惬意的结果,每次开会前都 ...
- IIS 7.0 Features and Vista Editions
原文 IIS 7.0 Features and Vista Editions Overview of IIS 7.0 differences Across Windows Vista Editions ...
- 看德日进,凯文·凯利与Kurzweil老师?
生命从哪里来.要到那里去.生命存在的意义是什么.这些差点儿是人类可以探究的最深层次问题.基督教给出的答案是毁灭和审判.佛学给出的答案是无常,科学的达尔文进化论给出了生命的起点和进化的过程,对于未来.达 ...
- 【DataStructure】Some useful methods for arrays
Last night it took me about two hours to learn arrays. For the sake of less time, I did not put emph ...