• 文件区域使用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

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

    文章结束给大家分享下程序员的一些笑话语录: 看到有人回帖“不顶不是中国人”,他的本意是想让帖子沉了。

    --------------------------------- 原创文章 By
    文件和区域
    ---------------------------------

  • 相关阅读:
    所谓的日常 #8
    所谓的日常 #7
    所谓的日常 #6
    所谓的日常 #5
    所谓的日常 #4
    所谓的日常 #3
    DFS序+线段树 hihoCoder 1381 Little Y's Tree(树的连通块的直径和)
    Codeforces Round #366 (Div. 2)
    2016 Multi-University Training Contests
    DP套DP HDOJ 4899 Hero meet devil(国王的子民的DNA)
  • 原文地址:https://www.cnblogs.com/xinyuyuanm/p/3104921.html
Copyright © 2020-2023  润新知