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函数用法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 马斯克称要裁员10% 知情人士:特斯拉中
- 下一篇: ofo拖欠50万罚款被强制执行:你的押金