fcntl函数详解

表头文件 #include
#include

定义函数 int fcntl(int fd , int cmd);
int fcntl(int fd, int cmd, long arg);
int fcntl(int fd, int cmd, struct flock * lock);

fcntl()用来操作文件描述词的一些特性。参数fd代表欲设置的文件描述词,参数cmd代表欲操作的指令。
有以下几种情况:
F_DUPFD用来查找大于或等于参数arg的最小且仍未使用的文件描述词,并且复制参数fd的文件描述词。执行成功则返回新复制的文件描述词。请参考dup2()。F_GETFD取得close-on-exec旗标。若此旗标的FD_CLOEXEC位为0,代表在调用exec()相关函数时文件将不会关闭。
F_SETFD 设置close-on-exec 旗标。该旗标以参数arg 的FD_CLOEXEC位决定。
F_GETFL 取得文件描述词状态旗标,此旗标为open()的参数flags。
F_SETFL 设置文件描述词状态旗标,参数arg为新旗标,但只允许O_APPEND、O_NONBLOCK和O_ASYNC位的改变,其他位的改变将不受影响。
F_GETLK 取得文件锁定的状态。
F_SETLK 设置文件锁定的状态。此时flcok 结构的l_type 值必须是F_RDLCK、F_WRLCK或F_UNLCK。如果无法建立锁定,则返回-1,错误代码为EACCES 或EAGAIN。
F_SETLKW F_SETLK 作用相同,但是无法建立锁定时,此调用会一直等到锁定动作成功为止。若在等待锁定的过程中被信号中断时,会立即返回-1,错误代码为EINTR。参数lock指针为flock 结构指针,定义如下
struct flcok
{
short int l_type; 
short int l_whence;
off_t l_start; 
off_t l_len; 
pid_t l_pid; 
};
l_type 有三种状态:
F_RDLCK 建立一个供读取用的锁定
F_WRLCK 建立一个供写入用的锁定
F_UNLCK 删除之前建立的锁定
l_whence 也有三种方式:
SEEK_SET 以文件开头为锁定的起始位置。
SEEK_CUR 以目前文件读写位置为锁定的起始位置
SEEK_END 以文件结尾为锁定的起始位置。
 
返回值  成功则返回0,若有错误则返回-1,错误原因存于errno.

 

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>

#include <fcntl.h>

int     lock_reg(int, int, int, off_t, int, off_t); 
#define read_lock(fd, offset, whence, len) \
	lock_reg((fd), F_SETLK, F_RDLCK, (offset), (whence), (len))
#define readw_lock(fd, offset, whence, len) \
	lock_reg((fd), F_SETLKW, F_RDLCK, (offset), (whence), (len))
#define write_lock(fd, offset, whence, len) \
	lock_reg((fd), F_SETLK, F_WRLCK, (offset), (whence), (len))
#define writew_lock(fd, offset, whence, len) \
	lock_reg((fd), F_SETLKW, F_WRLCK, (offset), (whence), (len))
#define un_lock(fd, offset, whence, len) \
	lock_reg((fd), F_SETLK, F_UNLCK, (offset), (whence), (len))

pid_t   lock_test(int, int, off_t, int, off_t); 

#define is_read_lockable(fd, offset, whence, len) \
	(lock_test((fd), F_RDLCK, (offset), (whence), (len)) == 0)
#define is_write_lockable(fd, offset, whence, len) \
	(lock_test((fd), F_WRLCK, (offset), (whence), (len)) == 0)


int lock_reg(int fd, int cmd, int type, off_t offset, int whence, off_t len)
{
	struct flock    lock;

	lock.l_type = type;     /* F_RDLCK, F_WRLCK, F_UNLCK */
	lock.l_start = offset;  /* byte offset, relative to l_whence */
	lock.l_whence = whence; /* SEEK_SET, SEEK_CUR, SEEK_END */
	lock.l_len = len;       /* #bytes (0 means to EOF) */

	return(fcntl(fd, cmd, &lock));
}

/* Note: lock_test always success with the obtained lock process */
pid_t lock_test(int fd, int type, off_t offset, int whence, off_t len)
{
	struct flock    lock;

	lock.l_type = type;     /* F_RDLCK or F_WRLCK */
	lock.l_start = offset;  /* byte offset, relative to l_whence */
	lock.l_whence = whence; /* SEEK_SET, SEEK_CUR, SEEK_END */
	lock.l_len = len;       /* #bytes (0 means to EOF) */

	if (fcntl(fd, F_GETLK, &lock) < 0)
		perror("fcntl error");

	/* printf("F_RDLCK=%d, F_WRLCK=%d, F_UNLCK=%d\n", F_RDLCK, F_WRLCK, F_UNLCK); */
	printf("    l_type=%d, l_start=%lu, l_where=%d, l_len=%lu, l_pid=%d\n"
		, lock.l_type, lock.l_start, lock.l_whence, lock.l_len, lock.l_type);

	if (lock.l_type == F_UNLCK)
		return(0);      /* false, region isn't locked by another proc */
	return(lock.l_pid); /* true, return pid of lock owner */
}

/* ========================================================  */

void Usage(const char* program)
{
	fprintf(stderr, "Usage: %s read/read2/write/rd-wr/wr-rd/fork/dup file time\n", program);
	exit(-1);
}

int main(int argc, char *argv[])
{
	if(argc < 3){
		Usage(argv[0]);
	}
	int interval = 10;
	if(argc > 3) {
		interval = atoi(argv[3]);
	}
	printf("PID=%d\n", getpid());

	char* path = argv[2];
	if( strcmp(argv[1], "read") == 0 ) {
		int fd = fileno(fopen(path,"r"));
		if(fd == -1) {
			perror("open");
			exit(0);
		}
		printf("read lock try\n");
		/*  read lock entire file */
		if( read_lock(fd, 0, SEEK_SET, 0) < 0){
			perror("read_lock: ");
			printf("Write Lock by pid=%d\n", lock_test(fd, F_WRLCK, 0, SEEK_SET, 0 ));
			exit(0); 
		} 
		printf("read lock success.\n");
		printf("Read Lock by pid=%d\n", lock_test(fd, F_WRLCK, 0, SEEK_SET, 0 ));

		sleep(interval);

		exit(0);
	}
	else if( strcmp(argv[1], "read2") == 0 ){
		int fd = open(path, O_RDONLY);
		if(fd == -1) {
			perror("open");
			exit(0);
		}
		printf("read lock try\n");
		/*  read lock entire file */
		if( read_lock(fd, 0, SEEK_SET, 0) < 0){
			perror("read_lock: ");
			printf("Write Lock by pid=%d\n", lock_test(fd, F_WRLCK, 0, SEEK_SET, 0 ));
			exit(0); 
		} 
		printf("read lock success.\n");
		printf("Read Lock by pid=%d\n", lock_test(fd, F_WRLCK, 0, SEEK_SET, 0 ));

		if( read_lock(fd, 0, SEEK_SET, 1) < 0){
			perror("read_lock: ");
			printf("Write Lock by pid=%d\n", lock_test(fd, F_WRLCK, 0, SEEK_SET, 0 ));
			exit(0); 
		}
		printf("read lock 2 success.\n");
		sleep(interval);

		exit(0);
	}
	else if( strcmp(argv[1], "write") == 0) {
		int fd = open(path, O_WRONLY );
		printf("file: %s, fd=%d\n", path, fd);
		if(fd == -1) {
			perror("open");
			exit(0);
		}
		printf("write lock try\n");
		/*  write lock entire file */
		if( write_lock(fd, 0, SEEK_SET, 0) < 0){
			perror("write_lock: ");
			printf("Write Lock by pid=%d\n", lock_test(fd, F_WRLCK, 0, SEEK_SET, 0 ));
			printf("Read Lock by pid=%d\n", lock_test(fd, F_RDLCK, 0, SEEK_SET, 0 ));
			exit(0); 
		}
		printf("write lock success.\n"); 
		printf("Write Lock by pid=%d\n", lock_test(fd, F_WRLCK, 0, SEEK_SET, 0 ));

		int ret = write(fd, path, strlen(path) );
		if(ret != strlen(path)){
			perror("write");
		} 
		sleep(interval);

		close(fd);
		exit(0);
	}
	else if( strcmp(argv[1], "rd-wr") == 0){
		int fd = open(path, O_RDWR);   /* O_RDWR  */
		if(fd == -1) {
			perror("open");
			exit(0);
		}
		/*  read lock entire file */
		if( read_lock(fd, 0, SEEK_SET, 0) < 0){
			perror("read_lock: ");
			printf("Write Lock by pid=%d\n", lock_test(fd, F_WRLCK, 0, SEEK_SET, 0 ));
			exit(0); 
		} 
		printf("read lock success.\n");
		printf("Lock by pid=%d\n", lock_test(fd, F_WRLCK, 0, SEEK_SET, 0 ));

		/* if( un_lock(fd, 0, SEEK_SET, 0) < 0){
		perror("un-lock:");
		}                                        */

		if( write_lock(fd, 0, SEEK_SET, 1) < 0){
			perror("write_lock: ");
			printf("Write Lock by pid=%d\n", lock_test(fd, F_WRLCK, 0, SEEK_SET, 0 ));
			exit(0); 
		}
		printf("write lock success.\n");
		printf("Lock by pid=%d\n", lock_test(fd, F_WRLCK, 0, SEEK_SET, 0 ));
		sleep(interval);

		exit(0);

	} 
	else if( strcmp(argv[1], "wr-rd") == 0){
		int fd = open(path, O_RDWR);   /* O_RDWR */
		if(fd == -1) {
			perror("open");
			exit(0);
		}
		/*  write lock entire file */
		if( write_lock(fd, 0, SEEK_SET, 0) < 0){
			perror("write_lock: ");
			printf("Write Lock by pid=%d\n", lock_test(fd, F_WRLCK, 0, SEEK_SET, 0 ));
			exit(0); 
		}
		printf("write lock success.\n");
		printf("Lock by pid=%d\n", lock_test(fd, F_WRLCK, 0, SEEK_SET, 0 ));

		/* if( un_lock(fd, 0, SEEK_SET, 0) < 0){
		perror("un-lock:");
		}                                        */

		if( read_lock(fd, 0, SEEK_SET, 0) < 0){
			perror("read_lock: ");
			printf("Write Lock by pid=%d\n", lock_test(fd, F_WRLCK, 0, SEEK_SET, 0 ));
			exit(0); 
		} 
		printf("read lock success.\n");
		printf("Lock by pid=%d\n", lock_test(fd, F_WRLCK, 0, SEEK_SET, 0 ));

		sleep(interval);

		exit(0);
	}
	else if( strcmp(argv[1], "fork") == 0){
		int fd = open(path, O_RDWR);   /* O_RDWR */
		if(fd == -1) {
			perror("open");
			exit(0);
		}
		/*  write lock entire file */
		if( write_lock(fd, 0, SEEK_SET, 0) < 0){
			perror("write_lock: ");
			printf("Write Lock by pid=%d\n", lock_test(fd, F_WRLCK, 0, SEEK_SET, 0 ));
			exit(0); 
		}
		printf("write lock success.\n");
		printf("Lock by pid=%d\n", lock_test(fd, F_WRLCK, 0, SEEK_SET, 0 ));

		pid_t pid;
		if ((pid = fork()) < 0) {
			perror("un-lock:");
			exit(1);
		}
		else if(pid == 0){
			sleep(1);
			printf("New PID=%d\n", getpid());
			if( read_lock(fd, 0, SEEK_SET, 0) < 0){
				perror("read_lock: ");
				printf("Write Lock by pid=%d\n", lock_test(fd, F_WRLCK, 0, SEEK_SET, 0 ));
				exit(0); 
			} 
			printf("read lock success.\n");
			printf("Lock by pid=%d\n", lock_test(fd, F_WRLCK, 0, SEEK_SET, 0 ));

			sleep(interval);

			exit(0);
		}
		else{
			exit(1);
		}
	}
	else if( strcmp(argv[1], "dup") == 0) {
		int fd = open(path, O_RDWR);   /* O_RDWR */
		if(fd == -1) {
			perror("open");
			exit(0);
		}
		/*  write lock entire file */
		if( write_lock(fd, 0, SEEK_SET, 0) < 0){
			perror("write_lock: ");
			printf("Write Lock by pid=%d\n", lock_test(fd, F_WRLCK, 0, SEEK_SET, 0 ));
			exit(0); 
		}
		printf("write lock success.\n");
		printf("Lock by pid=%d\n", lock_test(fd, F_WRLCK, 0, SEEK_SET, 0 ));

		int fd2 = 0;
		if( (fd2=dup(fd)) < 0){
			perror("dup:");
			exit(1);
		}
		close(fd);

		if( read_lock(fd2, 0, SEEK_SET, 0) < 0){
			perror("read_lock: ");
			printf("Write Lock by pid=%d\n", lock_test(fd2, F_WRLCK, 0, SEEK_SET, 0 ));
			exit(0); 
		} 
		printf("read lock success.\n");
		printf("Lock by pid=%d\n", lock_test(fd2, F_WRLCK, 0, SEEK_SET, 0 ));

		sleep(interval);

		exit(0);
	} else {
		printf("Unknown action!\n");
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值