该结构是在lock.h文件中定义。
lock.h File
功能
定义一些文件的锁的选项
Description
The flock structure in the /usr/include/sys/flock.h file, which describes a lock, contains the following fields:
l_type | Describes the type of lock. If the value of the Command parameter to the fcntlsubroutine is F_SETLK orF_SETLKW, the l_type field indicates the type of lock to be created. Possible values are:
If the value of the Command parameter to the fcntl subroutine is F_GETLK, the l_typefield describes an existing lock. Possible values are:
|
l_whence | Defines the starting offset. The value of this field indicates the point from which the relative offset, the l_startfield, is measured. Possible values are:
These values are defined in the unistd.h file. |
l_start | Defines the relative offset in bytes, measured from the starting point in the l_whencefield. |
l_len | Specifies the number of consecutive bytes to be locked. |
l_sysid | Contains the ID of the node that already has a lock placed on the area defined by thefcntl subroutine. This field is returned only when the value of the Command parameter isF_GETLK. |
l_pid | Contains the ID of a process that already has a lock placed on the area defined by thefcntl subroutine. This field is returned only when the value of the Command parameter isF_GETLK. |
l_vfs
| Specifies the file system type of the node identified in the l_sysid field. |
看一下示例吧!
- int
- waldirlock(Wal *w)
- {
- int r;
- int fd;
- struct flock lk;
- char path[PATH_MAX];
- r = snprintf(path, PATH_MAX, "%s/lock", w->dir);
- if (r > PATH_MAX) {
- twarnx("path too long: %s/lock", w->dir);
- return 0;
- }
- fd = open(path, O_WRONLY|O_CREAT, 0600);
- if (fd == -1) {
- twarn("open");
- return 0;
- }
- lk.l_type = F_WRLCK;
- lk.l_whence = SEEK_SET;
- lk.l_start = 0;
- lk.l_len = 0;
- r = fcntl(fd, F_SETLK, &lk);
- if (r) {
- twarn("fcntl");
- return 0;
- }
- // intentionally leak fd, since we never want to close it
- // and we'll never need it again
- return 1;
- }
struct flock 作为fcntl函数的第三个参数,使用F_SETLK,设置了其参数。
其中,参数 fd 表示文件描述符;参数 operation 指定要进行的锁操作,该参数的取值有如下几种:
通常情况下,如果加锁请求不能被立即满足,那么系统调用 flock() 会阻塞当前进程。比如,进程想要请求一个排他锁,但此时,已经由其他进程获取了这个锁,那么该进程将会被阻塞。如果想要在没有获得这个排他锁的情况下不阻塞该进程,可以将 LOCK_NB 和 LOCK_SH 或者 LOCK_EX 联合使用,那么系统就不会阻塞该进程。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));
}
}