fcntl()函数:
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
int fcntl(int filedes, int cmd,.../* int arg * / ) ;
返回:若成功则依赖于cmd(见下),若出错为- 1
常见用法:
1.复制一个文件描述符;
2.获取或者设置文件状态标志;
3.获得、设置文件记录锁;
struct flock 结构体定义:
struct flock {
short l_type; /* 记录锁类型,F_RDLCK,F_WRLCK或F_UNLCK */
off_t l_start; /* 起始位置偏移量,单位字节,是相对于l_whence而言 */
short l_whence; /* SEEK_SET,SEEK_CUR,或SEEK_END */
off_t l_len; /* 长度,单位字节,值为0时表示一直到文件尾 */
pid_t l_pid; /* 使用F_GETLK时返回此值 */
}
获得/设置记录锁(cmd = F_GETLK , F_SETLK或F_SETLKW)
F_GETLK是获得上锁的状态,F_SETLK是非阻塞上锁,F_SETLKW是阻塞上锁。
实例代码
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include <errno.h>
#include <string.h>
int main(){
/*fcntl函数可以复制文件符
int fd1, opt, fd2;
opt = 1;
fd1 = open("a.txt", O_WRONLY);
fd2 = fcntl(fd1, F_DUPFD, opt); // fd2等价于fd1(复制文件符),且f2 >= opt
write(fd1, "ccc", 3);
write(fd2, "ddd", 3);
close(fd1);
close(fd2);*/
/*用fcntl函数给打开的文件添加属性
int fd, flag;
char *p = "hello";
char *q = "world";
//打开文件
fd = open("a.txt", O_WRONLY);
if(fd == -1){
perror("open");
exit(1);
}
//从头开始写入,覆盖旧的内容
if(write(fd, p, strlen(p)) == -1){
perror("write");
exit(1);
}
//用fcntl函数获取文件标志
flag = fcntl(fd, F_GETFL, 0);
if(flag == -1){
perror("F_GETFL");
exit(1);
}
//添加O_APPEND,追加写属性
flag |= O_APPEND;
//将新的状态字赋予文件
if(fcntl(fd, F_SETFL, flag) == -1){
perror("F_SETFL");
exit(1);
}
//从文件末尾写入,不会覆盖旧的内容
if(write(fd, q, strlen(q)) == -1){
perror("write again");
exit(1);
}
close(fd);
*/
int fd;
fd = open("a.txt", O_RDWR);
struct flock lck;
lck.l_type = F_WRLCK;//设置写锁
lck.l_start = 0;//起始位置偏移量,相对于whence而言
lck.l_whence = SEEK_SET;//从头开始偏移
lck.l_len = 0;//值为0时表示一直到文件尾都被锁住
printf("start wrlock.....\n");
fcntl(fd, F_SETLKW, &lck);//F_SETLKW是表示尝试上锁会阻塞
printf("wrlock successfully\n");
getchar();
lck.l_type = F_UNLCK;//解锁
fcntl(fd, F_SETLKW, &lck);
printf("w unlock\n");
close(fd);
}
//O_ACCMODE是宏定义的0x11,用来获取读写权限