借助fcntl函数来实现锁机制。操作文件的进程没有获得锁时,可以打开,但无法执行read、write操作。
fcntl函数:获取、设置文件访问控制属性。
int fcntl(int fd,int cmd,…/* arg */);
示例代码file_lock.c
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
void sys_err(char *str)
{
perror(str);
exit(1);
}
int main(int argc,char *argv[])
{
int fd;
struct flock f_lock;
if(argc<2){
printf("./a.out filename\n");
exit(1);
}
if((fd=open(argv[1],O_RDWR))<0)
sys_err("open");
f_lock.l_type=F_WRLCK; /*选用写锁*/
// f_lock.l_type=F_RDLCK; /*选用读锁*/
f_lock.l_whence=SEEK_SET;
f_lock.l_start=0;
f_lock.l_len=0; /*0表示整个文件加锁*/
fcntl(fd,F_SETLKW,&f_lock);
printf("get flock\n");
sleep(10);
f_lock.l_type=F_UNLCK;
fcntl(fd,F_SETLKW,&f_lock);
printf("un flock\n");
close(fd);
return 0;
}
新建一个test.txt空文件,作为测试。
1.选用写锁
gcc -pthread file_lock.c
./a.out test.txt
再新起一个终端
./aout test.txt
2.选用读锁
gcc -pthread file_lock.c
./a.out test.txt
再新起一个终端
./aout test.txt