linux 进程同步锁,使用文件锁实现进程同步

文件锁是用于解决资源的共享使用的一种机制:当多个用户需要共享一个文件时,Linux 通常采用的方法是给文件上锁,来避免共享的资源产生竞争的状态。具体来讲,是通过借助 fcntl 函数来实现锁机制。当操作文件的进程没有获得锁时,虽然可以打开文件,但无法对文件执行执行 read、write 操作。

fcntl函数:

函数原型:

int fcntl(int fd, int cmd, ... /* arg */ );

函数作用:

获取、设置文件访问控制属性。

参数介绍:

参数cmd有以下取值:

F_SETLK (struct flock *)    设置文件锁(trylock)

F_SETLKW (struct flock *)  设置文件锁(lock)W --> wait

F_GETLK (struct flock *)    获取文件锁

flock 结构体

struct flock

{

short l_type; /*F_RDLCK, F_WRLCK, or F_UNLCK*/

off_t l_start; /*相对于l_whence的偏移值,字节为单位*/

short l_whence; /*从哪里开始:SEEK_SET, SEEK_CUR, or SEEK_END*/

off_t l_len; /*长度, 字节为单位; 0 意味着缩到文件结尾*/

pid_t l_pid; /*returned with F_GETLK*/

};

应用实例:

#include

#include

#include

#include

#include

#include

#include

int lock_set(int fd, int type)

{

struct flock old_lock, lock;

memset(&old_lock, 0, sizeof(old_lock));

memset(&lock, 0, sizeof(lock));

fcntl(fd, F_GETLK, &old_lock);

if(old_lock.l_type == F_WRLCK || old_lock.l_type == F_RDLCK)

{

if (old_lock.l_type == F_RDLCK)

printf("Read lock already set by %d \n", old_lock.l_pid);

else if (old_lock.l_type == F_WRLCK)

printf("Write lock already set by %d \n", old_lock.l_pid);

}

/* 配置 lock */

lock.l_whence = SEEK_SET;

lock.l_start  = 0;

lock.l_len    = 0;

lock.l_type  = type;

lock.l_pid    = -1;

lock.l_type  = type;

if ((fcntl(fd,F_SETLKW,&lock)) < 0){

printf("Lock failed : type = %d \n",lock.l_type);

return 0;

}

switch(lock.l_type)

{

case F_RDLCK:

printf("Read lock set by %d \n", getpid());

break;

case F_WRLCK:

printf("write lock set by %d \n", getpid());

break;

case F_UNLCK:

printf("Release lock by %d \n", getpid());

break;

default:

break;

}

return 0;

}

int main()

{

int fd;

fd = open("test",O_RDWR | O_CREAT, 0644);

if(fd < 0){

printf("Open file error \n");

exit(1);

}

/* 写锁定 */

lock_set(fd, F_WRLCK);

getchar();

/* 解锁 */

lock_set(fd, F_UNLCK);

getchar();

close(fd);

return 0;

}

运行结果:

c96fc46ec605e8dd2b335e05e1854fc8.png0b1331709591d260c1c78e86d0c51c18.png

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值