file_io锁

来源:互联网 发布:波洛 福尔摩斯知乎 编辑:程序博客网 时间:2024/05/16 06:32

文件读写锁的编程:


fcntl()用来操作文件描述词的一些特性。参数fd代表欲设置的文件描述词,参数cmd代表欲操作的指令。
有以下几种情况:

F_DUPFD用来查找大于或等于参数arg的最小且仍未使用的文件描述词,并且复制参数fd的文件描述词。执行成功则返回新复制的文件描述词。请参考dup2()。F_GETFD取得close-on-exec旗标。若此旗标的FD_CLOEXEC位为0,代表在调用exec()相关函数时文件将不会关闭。
F_SETFD 设置close-on-exec 旗标。该旗标以参数arg 的FD_CLOEXEC位决定。
F_GETFL 取得文件描述词状态旗标,此旗标为open()的参数flags。
F_SETFL 设置文件描述词状态旗标,参数arg为新旗标,但只允许O_APPEND、O_NONBLOCK和O_ASYNC位的改变,其他位的改变将不受影响。
F_GETLK 取得文件锁定的状态。
F_SETLK 设置文件锁定的状态。此时flcok 结构的l_type 值必须是F_RDLCK、F_WRLCK或F_UNLCK。如果无法建立锁定,则返回-1,错误代码为EACCES 或EAGAIN。
F_SETLKW F_SETLK 作用相同,但是无法建立锁定时,此调用会一直等到锁定动作成功为止。若在等待锁定的过程中被信号中断时,会立即返回-1,错误代码为EINTR。参数lock指针为flock 结构指针,定义如下:

fcntl()用来操作文件描述词的一些特性。参数fd代表欲设置的文件描述词,参数cmd代表欲操作的指令。
有以下几种情况:
F_DUPFD用来查找大于或等于参数arg的最小且仍未使用的文件描述词,并且复制参数fd的文件描述词。执行成功则返回新复制的文件描述词。请参考dup2()。F_GETFD取得close-on-exec旗标。若此旗标的FD_CLOEXEC位为0,代表在调用exec()相关函数时文件将不会关闭。
F_SETFD 设置close-on-exec 旗标。该旗标以参数arg 的FD_CLOEXEC位决定。
F_GETFL 取得文件描述词状态旗标,此旗标为open()的参数flags。
F_SETFL 设置文件描述词状态旗标,参数arg为新旗标,但只允许O_APPEND、O_NONBLOCK和O_ASYNC位的改变,其他位的改变将不受影响。
F_GETLK 取得文件锁定的状态。
F_SETLK 设置文件锁定的状态。此时flcok 结构的l_type 值必须是F_RDLCK、F_WRLCK或F_UNLCK。如果无法建立锁定,则返回-1,错误代码为EACCES 或EAGAIN。
F_SETLKW F_SETLK 作用相同,但是无法建立锁定时,此调用会一直等到锁定动作成功为止。若在等待锁定的过程中被信号中断时,会立即返回-1,错误代码为EINTR。参数lock指针为flock 结构指针,定义如下

struct flcok{
short int l_type; /* 锁定的状态*/int l_whence;/*决定l_start位置*/off_t l_start; /*锁定区域的开头位置*/off_t l_len; /*锁定区域的大小*/pid_t l_pid; /*锁定动作的进程*/};
l_type 有三种状态:
F_RDLCK 建立一个供读取用的锁定
F_WRLCK 建立一个供写入用的锁定
F_UNLCK 删除之前建立的锁定
l_whence 也有三种方式:
SEEK_SET 以文件开头为锁定的起始位置。
SEEK_CUR 以目前文件读写位置为锁定的起始位置
SEEK_END 以文件结尾为锁定的起始位置。
返回值
成功则返回0,若有错误则返回-1,错误原因存于errno.

下面的两个例子:

#include<unistd.h>#include<stdlib.h>#include<stdio.h>#include<fcntl.h>#include<errno.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, "Unalbe 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);    }    region_1.l_type = F_RDLCK;    region_1.l_whence = SEEK_SET;    region_1.l_start = 10;    region_1.l_len = 20;    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,&region_1);    if(res == -1)        fprintf(stderr,"Failed to lock region 1\n");    res = fcntl(file_desc,F_SETLK,&region_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);}

运行效果如下:


lock5.c代码如下:

#include<unistd.h>#include<stdlib.h>#include<stdio.h>#include<fcntl.h>#include<errno.h>const char *test_file = "/tmp/test_lock";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",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)    {        printf(" Process %d - failed to lock region \n",getppid());    }    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 + region_to_lock.l_len));    res = fcntl(file_desc,F_SETLK,&region_to_lock);    if(res == -1)    {        printf(" Process %d - failed to lock region \n",getppid());    }    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 = 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 + region_to_lock.l_len));    res = fcntl(file_desc,F_SETLK,&region_to_lock);    if(res == -1)    {        printf(" Process %d - failed to lock region \n",getppid());    }    else    {        printf("process %d - obtained region \n",getpid());    }    region_to_lock.l_type = F_RDLCK;    region_to_lock.l_whence = SEEK_SET;    region_to_lock.l_start = 16;    region_to_lock.l_len = 5;    printf("Process %d, trying F_UNLCk, region %d to %d \n",getpid(),(int)(region_to_lock.l_start + region_to_lock.l_len));    res = fcntl(file_desc,F_SETLK,&region_to_lock);    if(res == -1)    {        printf(" Process %d - failed to lock region \n",getppid());    }    else    {        printf("process %d - obtained 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_UNLCk, region %d to %d \n",getpid(),(int)(region_to_lock.l_start + region_to_lock.l_len));    res = fcntl(file_desc,F_SETLK,&region_to_lock);    if(res == -1)    {        printf(" Process %d - failed to lock region \n",getppid());    }    else    {        printf("process %d - obtained 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 + region_to_lock.l_len));    res = fcntl(file_desc,F_SETLKW,&region_to_lock);    if(res == -1)    {        printf(" Process %d - failed to lock region \n",getppid());    }    else    {        printf("process %d - obtained region \n",getpid());    }    printf("Process %d ending \n",getpid());    close(file_desc);    exit(EXIT_SUCCESS);}

运行效果如下:


fcntl()用来操作文件描述词的一些特性。参数fd代表欲设置的文件描述词,参数cmd代表欲操作的指令。
0 0
原创粉丝点击