高级IO-fcntl记录锁
来源:互联网 发布:微控加群软件 编辑:程序博客网 时间:2024/05/17 09:05
首先学习文章:http://www.cnblogs.com/nufangrensheng/p/3554168.html 和http://blog.csdn.net/anonymalias/article/details/9197641
然后写代码验证
#include <stdio.h>#include <fcntl.h>/* 测试记录锁相关的操作 */int main(int argc , char *argv[]){int fd ,retval ; struct flock flck;if( argc < 2 )oops( "argc:" );//open filefd = open( argv[1] , O_RDWR | O_CREAT | O_TRUNC );//fd = open( argv[1] , O_RDONLY );lock_init( &flck , F_WRLCK , SEEK_SET , 0 , 0 );printf( "before test," );pr_type( flck.l_type);/*从下面结果可以看到,不管文件属性是什么,当没有加锁时F_GETLK 都会返回F_UNLCK */printf( "after test read type,");test_lock( fd , F_WRLCK );printf( "after test write type ,");test_lock( fd , F_RDLCK );/*从下面的结果可以看到,当给文件加锁时候, 文件必须要有相应的属性 F_RDLCK对应O_RDONLY和O_RDWR ,同样。。。 *///now set a lockif( 0 == read_lock( fd ) )oops( " read lock. ");printf( "after set read type ,");retval = test_lock( fd , F_RDLCK );printf( "set lock return :%d.\n" , retval);if( 0 == write_lock( fd ) )oops( "write lock. ");printf( "after set write type ,");retval = test_lock( fd , F_WRLCK );printf( "set lock return :%d.\n" , retval);printf( "after set read type ,");retval = test_lock( fd , F_RDLCK );printf( "set lock return :%d.\n" , retval);return 0;}
#include <fcntl.h>#include <stdio.h>//#include <boolean.h>#include "oops.h"/* fcntl 记录锁函数 *///enum bool = {false ,true};void pr_type( const short l_type){switch(l_type){case F_RDLCK:printf( "flock type is F_RDLCK.\n" );break;case F_WRLCK:printf( "flock type is F_WRLCK.\n" );break;case F_UNLCK:printf( "flock type is F_UNLCK.\n" );break;default:printf( "flock type is unkown.\n" );break;}}int fcntl_fl_test( const int fd , const int type ){int val ;if( ( val = fcntl( fd , F_GETFL) ) < 0 )oops( "getfl" );if( ( val & O_ACCMODE) == type )return 1;elsereturn 0;}void lock_init( struct flock *ptr , short type , shortwhence , off_t start ,off_t len ){if( ptr == NULL )return ;ptr->l_type = type;ptr->l_whence = whence;ptr->l_start = start;ptr->l_len = len;}int read_lock( const int fd ){struct flock flck;//check if file has read access//if( !fcntl_fl_test( fd , F_RDLCK ) ){//printf( " file has not read access.\n");//return 0;//}//init and set flocklock_init( &flck , F_RDLCK , SEEK_SET , 0 , 0);if( fcntl( fd , F_SETLK , &flck ) == -1 ){printf( " fcntl return error.\n" );return 0;}elsereturn 1;}int write_lock( const int fd ){struct flock flck;//check if file has read access//if( !fcntl_fl_test( fd , F_WRLCK ) ){//printf( " file has not write access.\n");//return 0;//}//init and set flocklock_init( &flck , F_WRLCK , SEEK_SET , 0 , 0);if( fcntl( fd , F_SETLK , &flck ) == -1 ){printf( " fcntl return error.\n" );return 0;}elsereturn 1;}pid_t test_lock(const int fd ,const int type){struct flock flck;//init flocklock_init( &flck , type , SEEK_SET , 0 , 0);//test if we can create a lockif( fcntl( fd , F_GETLK , &flck ) == -1 ) return -1;pr_type( flck.l_type);if( F_UNLCK == flck.l_type )return 0;elsereturn flck.l_pid;}
然后看结果:
上面说明的兼容性规则适用于不同进程提出的锁请求,并不适用于单个进程提出的多个锁请求。如果一个进程对一个文件区间已经有了一把锁,后来该进程又企图在同一文件区间再加一把锁,那么新锁将替换老锁。例如,若一进程在某文件的16-32字节区间有一把写锁,然后又试图在16-32字节区间加一把读锁,那么该请求将成功执行(假定其他进程此时并不试图向该文件的同一区间加锁),原来的写锁被替换为读锁。
0 0
- 高级IO-fcntl记录锁
- fcntl()记录锁
- linux高级IO——记录锁
- IO fcntl
- fcntl应用于文件记录锁
- 难道fcntl记录锁失效?
- 记录锁的使用 fcntl
- 记录锁 和 fcntl函数
- 【高级IO】fcntl封装的set_fl&nbsp…
- fcntl 记录锁和struct flock
- fcntl 记录锁和struct flock
- 非阻塞IO--fcntl
- 文件IO操作之文件锁fcntl()函数
- UNIX环境高级编程——记录上锁(fcntl函数)以及死锁检测
- UNIX环境高级编程——记录上锁(fcntl函数)以及死锁检测
- Linux进程同步之记录锁(fcntl)
- Linux 文件I/O(三)之fcntl记录锁
- Linux进程同步之记录锁(fcntl)
- R语言文本分析(4)
- sar
- android开发环境的搭建
- Fragment和viewPager拒绝预加载
- 最短路径
- 高级IO-fcntl记录锁
- 赵志勇记录下的算法+程序
- 优质资源分享
- iOS开发封装带有Button的UIView控件,使用代理给button添加点击事件
- Retrofit--使用Retrofit时怎样去设置OKHttp
- HDU3397 Sequence operation(线段树的区间合并)
- STL之set和multiset详解
- 【小白成长日记】准备朝着目标前进
- 第八周项目(3)—教职工工资