fcntl的区域锁定

来源:互联网 发布:apm源码用什么编译 编辑:程序博客网 时间:2024/05/16 14:12

文件中的某个部分被锁定了,但其他的程序可以访问这个文件的其他部分,称为文件段锁定或文件区域锁定。经常使用文件区域锁定是fcntl函数。

#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>

int fcntl(int fd, int cmd, struct flock *lock);

fcntl提供了三个文件锁定的选项:

F_GETLK

F_SETLK

F_SETLKW

当这些命令选项使用时,fcntl的第三个参数必须是一个指向flock结构的指针。flock的结构包括下列成员:

short l_type

short l_whence

short l_start

off_t l_len

off_t l_pid

l_type的取值定义在fcntl.h中。

F_RDLCK 建立一个供读取用的锁定
F_WRLCK 建立一个供写入用的锁定
F_UNLCK 删除之前建立的锁定


l_whence、l_start和l_len成员定义了文件中的一个区域,即一个连续的字符集合。l_whence的取值必须是SEEK_SET 、SEEK_CUR、SEEK_END中的一个。它们分别对应文件头、当前位置和文件尾。l_whence定义了l_start的相对偏移值,其中l_start是该区域的第一个字节。l_whence通常设为SEEK_SET,这是l_start就从文件的开始计算。l_len参数定义了该区域的字节数。


文件中每个字节在任意时刻只能拥有一种类型的锁:共享锁,独占锁,解锁。

1、F_GETLK

它用于获取fd打开文件的锁信息,它不会尝试去锁定文件。如果调用成功就会返回一个非-1的值,如果文件已被锁定并阻止程序成功后的执行,fcntl就会用相关信息覆盖flock的结构,如果可以成功执行,flock的结构保持不变;如果调用无法获得信息,返回-1.

2、 F_SETLK

加锁成功返回非-1的值,失败则返回-1.

3、F_SETLKW

于上面的F_SETLK的功能类似,但在无法获取锁时,这个调用等待直到可以位置。


下面是lock3.c源文件,进行加锁。


#include <unistd.h>#include <stdlib.h>#include <stdio.h>#include <fcntl.h>const char *test_file = "/tmp/test_lock";int main() {    int file_desc;    int byte_count;    char *byte_to_write = "A";    struct flock region_1;    struct flock region_2;    int res;        /* open a file descriptor */    file_desc = open(test_file, O_RDWR | O_CREAT, 0666);    if (!file_desc) {        fprintf(stderr, "Unable to open %s for read/write\n", test_file);        exit(EXIT_FAILURE);    }        /* put some data in the file */    for(byte_count = 0; byte_count < 100; byte_count++) {        (void)write(file_desc, byte_to_write, 1);    }        /* setup region 1, a shared lock, from bytes 10 -> 30 */    region_1.l_type = F_RDLCK;    region_1.l_whence = SEEK_SET;    region_1.l_start = 10;    region_1.l_len = 20;             /* setup region 2, an exclusive lock, from bytes 40 -> 50 */    region_2.l_type = F_WRLCK;    region_2.l_whence = SEEK_SET;    region_2.l_start = 40;    region_2.l_len = 10;        /* now lock the file */    printf("Process %d locking file\n", getpid());    res = fcntl(file_desc, F_SETLK, ®ion_1);    if (res == -1) fprintf(stderr, "Failed to lock region 1\n");    res = fcntl(file_desc, F_SETLK, ®ion_2);    if (res == -1) fprintf(stderr, "Failed to lock region 2\n");            /* and wait for a while */    sleep(60);    printf("Process %d closing file\n", getpid());        close(file_desc);    exit(EXIT_SUCCESS);}

程序首先创建一个文件,并以可读可写的方式打开它,然后在文件中添加一些数据。接着在文件中设置两个区域:第一个区域是10-30个字节,使用共享锁;第二个区域是40-50字节,使用独占锁。然后使用fcntl锁定着两个区域,并在关闭文件和退出程序前等待一分钟。