优质服务商推荐更多服务商>

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

4224

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

    Linux系统编程__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 m     ai   n(int argc, const char * argv [ ])

{

int fd = open("     te   st.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_     pi   d = getpid();

lock.l_start = 0;

lock.l_type = F_RDLCK;

lock.l_whence = SEEK_SET;

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

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

{

perror("fcntl fail ");

return -1;

}

else

{

printf("add read lock success!\n");

}

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\n",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 \n",lock_1.l_pid);

break;

case F_WRLCK:

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

break;

case F_UNLCK:

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

}

}

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

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

所以,加锁是成功的。




特别声明:本文仅供交流学习 , 版权归属原作者,并不代表蚂蚜网赞同其观点和对其真实性负责。若文章无意侵犯到您的知识产权,损害了您的利益,烦请与我们联系vmaya_gz@126.com,我们将在24小时内进行修改或删除。

相关推荐: