文件锁的竞争

来源:互联网 发布:音频剪辑软件mac版 编辑:程序博客网 时间:2024/05/16 19:07
//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);}
原创粉丝点击