概述
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=%sn", 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 successn");
printf("press any key to unlockn");
getchar();
lock.l_type = F_UNLCK;
if (fcntl(fd, F_SETLK, &lock) == 0)
printf("unlock successn");
else
ERR_EXIT("unlock fail");
}
else
ERR_EXIT("lock fail");
return 0;
}
注意:
fcntl(fd, F_SETLK, &lock) 与fcntl(fd, F_SETLKW, &lock) 区别是后者一直阻塞中,直到文件锁被释放
最后
以上就是任性大地为你收集整理的fcntl的全部内容,希望文章能够帮你解决fcntl所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复