Linux系统编程--fcntl()读写锁实例

嵌入式技术

1333人已加入

描述

     在多进程对同一个文件进行读写访问时,为了保证数据的完整性,有事需要对文件进行锁定。可以通过fcntl()函数对文件进行锁定和解锁。

1. fcntl

1.1.功能描述:根据文件描述词来操作文件的特性。

1.2.用法:

int fcntl(int fd, int cmd); 

int fcntl(int fd, int cmd, long arg); 

int fcntl(int fd, int cmd, struct flock *lock); 

fd:文件描述词。 

cmd:操作命令。 

arg:供命令使用的参数,是否需要arg参数跟cmd命令有关。 

lock:锁信息。

2.读写锁实例

新建两个文件,源码如下2.1、2.2所示。

2.1.给文件加读锁

#include

#include

#include

#include

#include

int main(int argc, const char * argv [ ])

{

int fd = open("test.c", O_RDONLY);

if (fd == -1)

{

perror("open failed:");

return -1;

}

struct stat sta;

fstat(fd,&sta);

struct flock lock;   

lock.l_len = sta.st_size;

lock.l_pid = getpid();

lock.l_start = 0;

lock.l_type = F_RDLCK;

lock.l_whence = SEEK_SET;

printf("进程pid: %d ",lock.l_pid);

if(fcntl(fd,F_SETLK,&lock) == -1)

{

perror("fcntl fail ");

return -1;

}

else

{

printf("add read lock success! ");

}

sleep(10);

close(fd);

return 0;

}

2.2.给文件加写锁

#include

#include

#include

#include

#include

int main(int argc, const char * argv [ ])

{

int fd = open("test.c", O_WRONLY);

if (fd == -1)

{

perror("open failed:");

return -1;

}

struct stat sta;

fstat(fd,&sta);

struct flock lock;   

lock.l_len = sta.st_size;

lock.l_pid = getpid();

lock.l_start = 0;

lock.l_type = F_WRLCK;

lock.l_whence = SEEK_SET;

printf("进程pid: %d ",lock.l_pid);

while(fcntl(fd,F_SETLK,&lock) == -1 )

{

perror("fcntl:");

sleep(1);

struct flock lock_1;

lock_1 = lock;

lock_1.l_type = F_WRLCK;  //

fcntl(fd,F_GETLK,&lock_1);//获取文件锁状态,及加锁(lock_1.l_type)能否成功

switch(lock_1.l_type)

{

case F_RDLCK:

printf("检测到读锁 pid = %d ",lock_1.l_pid);

break;

case F_WRLCK:

printf("检测到写锁 pid = %d ",lock_1.l_pid);

break;

case F_UNLCK:

printf("检测到已解锁.pid = %d ",lock_1.l_pid);

}

}

printf("写锁设置成功 ");

getchar();

close(fd);

return 0;

}

/*

注意:

1、fcntl(fd,F_GETLK,&lock_1)中的lock_1必须进行初始化,并且lock_1.l_type必须设置为相应的锁,才能确定能否加锁成功,及不成功的原因。

2、GETLK时,fcntl先检测有没有能阻止本次加锁的锁,如果有,则覆盖flock结构体(lock_1)的信息。如果没有,则置lock_1.l_type 的类型为F_UNLCK。

*/

对于写锁(F_WRLCK 独占锁),只有一个进程可以在文件的任一特定区域上享有独占锁。

对于读锁(F_RDLCK 共享锁),许多不同的进程可以同时拥有文件上同一区域上的共享锁。为了拥有共享锁,文件必须以读或者读/写的方式打开。只要任一进程拥有共享锁,那么其他进程就无法再获得独占锁。

分别编译执行:

3.先执行读锁,再执行写锁。结果如下:

liu@ubuntu:~/learn/lrn_linux$ ./readlock.out 

进程pid: 16458

add read lock success!

liu@ubuntu:~/learn/lrn_linux$ ./writelock.out 

进程pid: 16459

fcntl:: Resource temporarily unavailable

检测到读锁 pid = 16458 

fcntl:: Resource temporarily unavailable

检测到读锁 pid = 16458 

fcntl:: Resource temporarily unavailable

检测到读锁 pid = 16458 

fcntl:: Resource temporarily unavailable

检测到读锁 pid = 16458 

fcntl:: Resource temporarily unavailable

检测到读锁 pid = 16458 

fcntl:: Resource temporarily unavailable

检测到读锁 pid = 16458 

fcntl:: Resource temporarily unavailable

检测到读锁 pid = 16458 

fcntl:: Resource temporarily unavailable

检测到已解锁.pid = 16459 

写锁设置成功

可以看出,当文件被读锁占用时,无法添加写锁(独占锁)

4.先运行写锁,再运行读锁的话,结果如下:

liu@ubuntu:~/learn/lrn_linux$ ./writelock.out 

进程pid: 16349

写锁设置成功

liu@ubuntu:~/learn/lrn_linux$ ./readlock.out 

进程pid: 16350

fcntl fail : Resource temporarily unavailable

所以,加锁是成功的。




 

打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分