文件锁的竞争

//fcntl锁定文件
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
#include<fcntl.h>

const char *test_file = "/home/ndj/filelock/fcntl/test_lock.in";
int main()
{
    int file_desc;//文件描述符
    int byte_count;
    char *byte_to_write = "A";
    struct flock region1;
    struct flock region2;
    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++)
        (void)write(file_desc, byte_to_write, 1);
    //把文件10~30字节设为区域1, 并在其上设置共享锁
    region1.l_type = F_RDLCK;//共享锁
    region1.l_whence = SEEK_SET;//绝对位置
    region1.l_start = 10;
    region1.l_len = 20;
    //把文件40~50字节设置为区域2,并在其上设置独占锁
    region2.l_type = F_WRLCK;//独占锁
    region2.l_whence = SEEK_SET;
    region2.l_start = 40;
    region2.l_len = 10;
    //现在锁定文件
    printf("Process %d locking file\n", getpid());
    res = fcntl(file_desc, F_SETLK, &region1);
    if(res == -1)
        fprintf(stderr, "Failed to lock region1\n");
    res = fcntl(file_desc, F_SETLK, &region2);
    if(res == -1)
        fprintf(stderr, "Failed to lock region2\n");
    //然后等一会
    sleep(60);//等待其他进程访问
    printf("Princess %d closing file\n", getpid());
    close(file_desc);
    printf("SUCCESS=%d", EXIT_SUCCESS);
    exit(EXIT_SUCCESS);
}
//文件锁的竞争
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
#include<fcntl.h>
const char *test_file="/home/ndj/filelock/fcntl/test_lock.in";//文件对象

//const char *test_file = "/home/ndj/filelock/fcntl/test_lock.in";
int main()
{
    int file_desc;
    struct flock region_to_lock;
    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);
    }
    //指定文件区域,并尝试在他们之上执行不同的锁定操作
    region_to_lock.l_type = F_RDLCK;//共享锁
    region_to_lock.l_whence = SEEK_SET;
    region_to_lock.l_start = 10;
    region_to_lock.l_len = 5;

    printf("Process %d, trying F_RDLCK, region %d to %d\n", getpid(), (int)region_to_lock.l_start, (int)(region_to_lock.l_start+region_to_lock.l_len));
    res = fcntl(file_desc, F_SETLK, &region_to_lock);
    if(res == -1)
    {
        fprintf(stderr, "Process %d - failed to lock region\n", getpid());
    //  exit(EXIT_FAILURE);
    }
    else
    {
        printf("Process %d - obtained lock region\n", getpid());
    }

    region_to_lock.l_type = F_UNLCK;//解锁
    region_to_lock.l_whence = SEEK_SET;
    region_to_lock.l_start = 10;
    region_to_lock.l_len = 5;

    printf("Process %d, trying F_UNLCK,region %d to %d\n", getpid(), (int)region_to_lock.l_start, (int)(region_to_lock.l_start+region_to_lock.l_len));
    res = fcntl(file_desc, F_SETLK, &region_to_lock);
    if(res == -1)
    {
        fprintf(stderr, "Process %d - fail to unlock region\n", getpid());
    }
    else
    {
        printf("Process %d - unlocked region\n", getpid());
    }

    region_to_lock.l_type = F_UNLCK;//解锁
    region_to_lock.l_whence = SEEK_SET;
    region_to_lock.l_start = 0;
    region_to_lock.l_len = 50;
    printf("Process %d, trying F_UNLCK,region %d to %d\n", getpid(), (int)region_to_lock.l_start, (int)(region_to_lock.l_start+region_to_lock.l_len));
    res = fcntl(file_desc, F_SETLK, &region_to_lock);
    if(res == -1)
    {
        fprintf(stderr, "Process %d - fail to unlock region\n", getpid());
    }
    else
    {
        printf("Process %d - unlocked region\n", getpid());
    }

    region_to_lock.l_type = F_WRLCK;//独占锁
    region_to_lock.l_whence = SEEK_SET;
    region_to_lock.l_start = 16;
    region_to_lock.l_len = 5;

    printf("Process %d, trying F_WRLCK ,region %d to %d\n", getpid(), (int)region_to_lock.l_start, (int)(region_to_lock.l_start+region_to_lock.l_len));
    res = fcntl(file_desc, F_SETLK, &region_to_lock);
    if(res == -1)
    {
        fprintf(stderr, "Process %d - fail to lock region\n", getpid());
    }
    else
    {
        printf("Process %d - locked region\n", getpid());

    }

    region_to_lock.l_type = F_RDLCK;//共享锁
    region_to_lock.l_whence = SEEK_SET;
    region_to_lock.l_start = 40;
    region_to_lock.l_len = 10;

    printf("Process %d, trying F_RDLCK,region %d to %d\n", getpid(), (int)region_to_lock.l_start, (int)(region_to_lock.l_start+region_to_lock.l_len));
    res = fcntl(file_desc, F_SETLK, &region_to_lock);
    if(res == -1)
    {
        fprintf(stderr, "Process %d - fail to lock region\n", getpid());
    }
    else
    {
        printf("Process %d - locked region\n", getpid());
    }

    region_to_lock.l_type = F_WRLCK;//独占锁
    region_to_lock.l_whence = SEEK_SET;
    region_to_lock.l_start = 16;
    region_to_lock.l_len = 5;

    printf("Process %d, trying F_WELCK ,region %d to %d\n", getpid(), (int)region_to_lock.l_start, (int)(region_to_lock.l_start+region_to_lock.l_len));
    res = fcntl(file_desc, F_SETLK, &region_to_lock);
    if(res == -1)
    {
        fprintf(stderr, "Process %d - fail to lock region\n", getpid());
    }
    else
    {
        printf("Process %d - locked region\n", getpid());
    }

    printf("Process %d ending\n", getpid());
    close(file_desc);
    exit(EXIT_SUCCESS);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值