linux系统编程之文件与I/O(六):fcntl 函数与文件锁
来源:互联网 发布:shell编程的特点 编辑:程序博客网 时间:2024/05/23 01:16
一、fcntl函数
功能:操纵文件描述符,改变已打开的文件的属性
int fcntl(int fd, int cmd, ... /* arg */ );
cmd的取值可以如下:
复制文件描述符
F_DUPFD (long)
设置/获取文件描述符标志
F_GETFD (void)
F_SETFD (long)
设置/获取文件状态标志
F_GETFL (void)
F_SETFL (long)
获取/设置文件锁
F_GETLK
F_SETLK,F_SETLKW
其中复制文件描述符可参见《linux系统编程之文件与I/O(五):打开文件的内核结构file和重定向》,文件描述符的标志只有一个即FD_CLOEXEC,设置/获取文件描述符标志看这里。下面先来看设置/获取文件状态标志。
F_SETFL:
On Linux this command can change only the O_APPEND, O_ASYNC, O_DIRECT, O_NOATIME, and O_NONBLOCK flags.
示例程序如下:
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
> File Name: file_fcntl.c
> Author: Simba
> Mail: dameng34@163.com
> Created Time: Sat 23 Feb 2013 02:34:02 PM CST
************************************************************************/
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
#include<fcntl.h>
#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<string.h>
#define ERR_EXIT(m) \
do { \
perror(m); \
exit(EXIT_FAILURE); \
} while(0)
void set_flag(int, int);
void clr_flag(int, int);
int main(int argc, char *argv[])
{
char buf[1024] = {0};
int ret;
/*
int flags;
flags = fcntl(0, F_GETFL, 0);
if (flags == -1)
ERR_EXIT("fcntl get flag error");
ret = fcntl(0, SETFL, flags | O_NONBLOCK); //设置为非阻塞,但不更改其他状态
if (ret == -1)
ERR_EXIT("fcntl set flag error");
*/
set_flag(0, O_NONBLOCK);
ret = read(0, buf, 1024);
if (ret == -1)
ERR_EXIT("read error");
printf("buf=%s\n", buf);
return 0;
}
void set_flag(int fd, int flags)
{
int val;
val = fcntl(fd, F_GETFL, 0);
if (val == -1)
ERR_EXIT("fcntl get flag error");
val |= flags;
if (fcntl(fd, F_SETFL, val) < 0)
ERR_EXIT("fcntl set flag error");
}
void clr_flag(int fd, int flags)
{
int val;
val = fcntl(fd, F_GETFL, 0);
if (val == -1)
ERR_EXIT("fcntl get flag error");
val &= ~flags;
if (fcntl(fd, F_SETFL, val) < 0)
ERR_EXIT("fcntl set flag error");
}
simba@ubuntu:~/Documents/code/linux_programming/APUE/File_IO$ ./file_fcntl
read error: Resource temporarily unavailable
因为将标准输入的状态更改为非阻塞,则read不会阻塞等待输入而立即返回错误,errno将被置为EAGAIN,即可以重新尝试。
二、文件锁结构体
struct flock {
...
short l_type; /* Type of lock: F_RDLCK,
F_WRLCK, F_UNLCK */
short l_whence; /* How to interpret l_start:
SEEK_SET, SEEK_CUR, SEEK_END */
off_t l_start; /* Starting offset for lock */
off_t l_len; /* Number of bytes to lock */
pid_t l_pid; /* PID of process blocking our lock
(F_GETLK only) */
...
};
文件锁的类型只有两种,一种是写锁也叫排他锁,一种是读锁也就共享锁,可以有多个进程各持有一个读锁,但只能有一个进程持有写锁,只有对文件有对应的读写权限才能施加对应的锁类型。中间三个参数 l_whence, l_start, l_len 决定了被锁定的文件范围。当fcntl 函数的cmd为F_GETLK时,flock 结构体的 l_pid 参数会返回持有写锁的进程id。进程退出或者文件描述符被关闭时,会释放所有的锁。
示例程序如下:
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
> File Name: file_flock.c
> Author: Simba
> Mail: dameng34@163.com
> Created Time: Sat 23 Feb 2013 02:34:02 PM CST
************************************************************************/
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
#include<fcntl.h>
#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<string.h>
#define ERR_EXIT(m) \
do { \
perror(m); \
exit(EXIT_FAILURE); \
} while(0)
int main(int argc, char *argv[])
{
int fd;
fd = open("test2.txt", O_CREAT | O_RDWR | O_TRUNC, 0664);
if (fd == -1)
ERR_EXIT("open error");
/* 只有对文件有相应的读写权限才能施加对应的文件锁 */
struct flock lock;
memset(&lock, 0, sizeof(lock));
lock.l_type = F_WRLCK; // 排他锁,即不允许其他进程再对其加任何类型的锁,但读锁(共享锁)允许
lock.l_whence = SEEK_SET;
lock.l_start = 0; //从文件开头开始锁定
lock.l_len = 0; // 文件全部内容锁住
if (fcntl(fd, F_SETLK, &lock) == 0)
{
/* 若为F_SETLKW,这时如果锁已经被其他进程占用,则此进程会阻塞直到其他进程释放锁*/
printf("lock success\n");
printf("press any key to unlock\n");
getchar();
lock.l_type = F_UNLCK;
if (fcntl(fd, F_SETLK, &lock) == 0)
printf("unlock success\n");
else
ERR_EXIT("unlock fail");
}
else
ERR_EXIT("lock fail");
return 0; //进程退出会对所有文件解锁
}
测试如下:
我们先在一个 终端执行程序:
simba@ubuntu:~/Documents/code/linux_programming/APUE/File_IO$ ./file_flock
lock success
press any key to unlock
现在文件已经被锁住了,而且没有按下任何按键,所以卡在这里,也还没解锁,接着在另一个终端再次执行同个程序:
simba@ubuntu:~/Documents/code/linux_programming/APUE/File_IO$ ./file_flock
lock fail: Resource temporarily unavailable
会立即返回错误,因为我们希望施加的是排他锁,而现在前面一个进程正在占用写锁还没释放,所以尝试施加锁失败,而如果fcntl 函数的cmd 设置为 F_SETLKW,即带w的版本,则此进程会一直阻塞直到前面一个进程释放了锁。
参考:《APUE》
- linux系统编程之文件与I/O(六):fcntl 函数与文件锁
- linux系统编程之文件与I/O(六):fcntl 函数与文件锁
- linux系统编程之文件与I/O(六):fcntl 函数与文件锁
- linux系统编程之文件与I/O(五):fcntl 函数与文件锁
- Linux环境编程之文件I/O(五):fcntl函数
- Linux系统编程:fcntl函数与文件锁
- Linux系统编程(1)——文件与I/O之C标准I/O函数与系统调用I/O(尹成清华毕业生)
- Linux系统编程(1)——文件与I/O之C标准I/O函数与系统调用I/O
- Linux系统编程(1)——文件与I/O之C标准I/O函数与系统调用I/O
- Linux系统编程(3)——文件与IO之fcntl函数
- Linux 文件I/O操作之fcntl函数
- Linux 文件I/O(二)之 fcntl函数讲解
- 简单文件I/O编程之fcntl
- linux系统编程之文件与I/O(一):文件的打开关闭
- linux系统编程之文件与I/O(二):文件的读取写入
- linux系统编程之文件与I/O(四):文件的属性
- linux系统编程之文件与I/O(一):文件的打开关闭
- linux系统编程之文件与I/O(二):文件的读取写入
- IOS 动态改变cell的高度
- 为什么使用extern “C”?
- windows8应用《同济新闻客户端》隐私声明
- HDU 3555 Bomb (数位DP)
- JAVA编程思想笔记——初始化和清理(1)
- linux系统编程之文件与I/O(六):fcntl 函数与文件锁
- 关于Java性能的9个谬论
- Android SDK下载与在eclipse中的安装(菜鸟来讲超慢易理解版)
- Win7系统设置Wifi热点(非原创)
- JAVA程序输出到.txt文件的换行符处理
- java.io 与对象序列化 笔记
- JAVA中private protected public 修饰符及JAVA默认修饰符(default acess)
- JAVA Socket 网络联机 客户端 服务端 笔记
- Building your own toolchain