fcntl

1、功能:操纵文件描述符,改变已打开的文件的属性


int fcntl(int fd,int cmd,..../*arg*/);


2、fcntl常用操作

 复制文件描述符

       F_DUPFD(long)

 文件描述符标志

       F_GETFD(void)

      F_SETFD(long)

文件状态标志

     F_GETFL(void)

    F_SETFL(long)

文件锁

      F_GETLK

      F_SETLK,F_SETLKW


下面例子:说明的是fd=0说明文件描述符标准输入文件描述符,标准输入是阻塞的,但可以通过fcntl来设置此文件描述符为非阻塞的状态。会立刻返回一个EAGIN错误信息,表明此资源暂时不能用(因为阻塞和非阻塞只能用在socket文件描述符上, EAGAIN The  file descriptor fd refers to a file other than a socket and has been marked nonblocking (O_NONBLOCK),  and  the  read  wouldblock.)

可以设置文件描述符状态和清除文件描述符状态。

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

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


#define ERR_EXIT(m) \
	do \
	{ \
		perror(m); \
		exit(EXIT_FAILURE); \
	} while(0)

void set_flag(int fd, int flags);
void clr_flag(int fd, int flags);

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, F_SETFL, flags | O_NONBLOCK);
	if (ret == -1)
		ERR_EXIT("fcntl set flag error");
*/
	set_flag(0, O_NONBLOCK);
	/*clr_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");
}





2、文件锁结构体

 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) */
               ...
           }

下面的代码示例是对文件进行进行加锁:排它锁,即当一个进程打开文件时,对文件进行加锁,其他进程再次打开该文件时,打开失败,出现错误:lock fail: Resource temporarily unavailable


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

#include <stdlib.h>
#include <stdio.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, 0644);
	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)*/
	if (fcntl(fd, F_SETLKW, &lock) == 0)
	{
		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;
}


注意:

 fcntl(fd, F_SETLK, &lock) 与fcntl(fd, F_SETLKW, &lock) 区别是后者一直阻塞中,直到文件锁被释放

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值