首页 技术 正文
技术 2022年11月19日
0 收藏 316 点赞 3,153 浏览 2092 个字

文件中的某个部分被锁定了,但其他的程序可以访问这个文件的其他部分,称为文件段锁定或文件区域锁定。经常使用文件区域锁定是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锁定着两个区域,并在关闭文件和退出程序前等待一分钟。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,088
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,565
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,413
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,186
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,822
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,905