使用fcntl锁定文件,并且测试

//lock1.c
/*
程序首先创建一个文件,并且以可读的方式打开,然后在文件中添加一些内容,接着
在文件中设置两个区域,第一个区域为10-30字节,使用共享锁;第二个区域为40-50
字节,使用独占锁,然后程序调用fcntl函数来锁定这两个区域,并在关闭文件和退出程序前等待一分钟
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>

const char *test_file = "/tmp/test_lock";

int main()
{
    int file_desc;
    int byte_count;
    char *byte_to_write = "A";
    struct flock region_1;
    struct flock region_2;
    int res;
    file_desc = open(test_file,O_RDWR | O_CREAT,0666);
    if(!file_desc)
    {
        fprintf(stderr,"Unable to open %s for read/write\n",test_file);
        exit(EXIT_FAILURE);
    }
    for(byte_count = 0;byte_count < 100;byte_count++)
    {
        write(file_desc,byte_to_write,1);
    }
    /*将文件10-30字节设为区域1,并在其上设置共享锁*/
    region_1.l_type = F_RDLCK;
    region_1.l_whence = SEEK_SET;
    region_1.l_start = 10;
    region_1.l_len = 20;
    /*将文件40-50字节设为区域2,并在其上设置独占锁*/
    region_2.l_type = F_WRLCK;
    region_2.l_whence = SEEK_SET;
    region_2.l_start = 40;
    region_2.l_len = 10;
    
    printf("Process %d locking file\n",getpid());
    res = fcntl(file_desc,F_SETLK,®ion_1);
    if(res == -1)
    {
        fprintf(stderr,"Failed to lock region 1\n");
    }
    res = fcntl(file_desc,F_SETLK,®ion_2);
    if(res == -1)
    {
        fprintf(stderr,"Failed to lock region 2\n");
    }
    
    sleep(60);
    
    printf("Process %d closing file\n",getpid());
    close(file_desc);
    exit(EXIT_SUCCESS);
    return 0;
}

//lock2.c

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>

const char *test_file = "/tmp/test_lock";
#define SIZE_TO_TRY 5

void show_lock_info(struct flock *to_show);

int main()
{
    int file_desc;
    int res;
    struct flock region_to_test;
    int start_byte;
    
    file_desc = open(test_file,O_RDWR | O_CREAT,0666);
    if(!file_desc)
    {
        fprintf(stderr,"Unable to open %s for read/write",test_file);
        exit(EXIT_FAILURE);
    }
    
    for(start_byte = 0;start_byte < 99;start_byte++)
    {
        region_to_test.l_type = F_WRLCK;
        region_to_test.l_whence = SEEK_SET;
        region_to_test.l_start = start_byte;
        region_to_test.l_len = SIZE_TO_TRY;
        region_to_test.l_pid = -1;
        printf("Testing F_WRLCK on region from %d to %d\n",start_byte,start_byte+SIZE_TO_TRY);
    
		res = fcntl(file_desc,F_GETLK,®ion_to_test);
		if(res == -1)
		{
		    fprintf(stderr,"F_GETLCK falied\n");
		    exit(EXIT_FAILURE);
		}
		
		if(region_to_test.l_pid != -1)
		{
		    printf("Lock would fail.F_GETLK returned.\n");
		    show_lock_info(®ion_to_test);
		}
		else
		{
		    printf("F_WRLCK - LOCK would succeed\n");
		}
		region_to_test.l_type = F_RDLCK;
		region_to_test.l_whence = SEEK_SET;
		region_to_test.l_start = start_byte;
		region_to_test.l_len = SIZE_TO_TRY;
		region_to_test.l_pid = -1;
		printf("Testing F_RDLCK on region from %d to %d\n",start_byte,start_byte+SIZE_TO_TRY);
		res = fcntl(file_desc,F_GETLK,®ion_to_test);
		if(res == -1)
		{
		    fprintf(stderr,"F_GETLCK falied\n");
		    exit(EXIT_FAILURE);
		}
		if(region_to_test.l_pid != -1)
		{
		    printf("Lock would fail.F_GETLK returned.\n");
		    show_lock_info(®ion_to_test);
		}
		else
		{
		    printf("F_RDLCK - LOCK would succeed\n");
		}
	}
	close(file_desc);
	exit(EXIT_SUCCESS);
}

void show_lock_info(struct flock *to_show)
{
    printf("\tl_type %d",to_show->l_type);
    printf("\tl_whence %d",to_show->l_whence);
    printf("\tl_start %d",to_show->l_start);
    printf("\tl_len %d",to_show->l_len);
    printf("\tl_pid %d",to_show->l_pid);
}

为了测试锁,首先运行lock1(后台运行),然后再运行lock2
$ ./lock1 &
[1] 6321
[anpan@anpan 数据管理]$ Process 6321 locking file
Process 6321 closing file


./lock2
Testing F_WRLCK on region from 0 to 5
F_WRLCK - LOCK would succeed
Testing F_RDLCK on region from 0 to 5
F_RDLCK - LOCK would succeed
Testing F_WRLCK on region from 1 to 6
F_WRLCK - LOCK would succeed
Testing F_RDLCK on region from 1 to 6
F_RDLCK - LOCK would succeed


………………………………



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值