给文件锁设定timeout

Linux 文件锁是建议锁,也有人把它叫做记录锁,是通过系统调用fcntl(2)来实现的。
这种锁在锁定文件时有两种模式,分别是阻塞(block)和非阻塞模式。
在编码时比较常用的是有一种的非阻塞模式,也就是发现文件已经被其他进程
锁定时,立即返回不予等待。而阻塞模式则正好与它相反,也就是一直等待直到
其他进程释放文件锁为止。
注:关于详细内容请参看《Unix环境高级编程》

不过,有的时候也会用到阻塞模式的文件锁,而且会要求不能被一直阻塞,等待
了一定时间后应返回。也就是说,想給阻塞版本的文件锁加上一个超时时间(timeout)。

通过man手册,fcntl(2)里面没有关于在阻塞模式时,设置超时时间的任何描述。
但从man手册里我们发现,文件锁在阻塞时会被信号(signal)中断。所以我们就像
可以利用设置信号软中断来实现一个自己版本的等待超时呢。

 1  #include  < stdio.h >
 2  #include  < stdlib.h >
 3  #include  < unistd.h >
 4  #include  < fcntl.h >
 5  #include  < sys / types.h >
 6  #include  < sys / stat.h >
 7  #include  < signal.h >
 8 
 9  #define  readw_lock(fd) /
10      lock_reg((fd), F_SETLKW, F_RDLCK)
11  #define  writew_lock(fd) /
12      lock_reg((fd), F_SETLKW, F_WRLCK)
13  #define  unlock(fd) /
14      lock_reg((fd), F_SETLK, F_UNLCK)
15 
16  typedef  int  ( * LW_FN)( char   * fname);
17 
18  int
19  lock_reg( int  fd,  int  cmd,  int  type)
20  {
21       struct  flock  lock ;
22       lock .l_type  =  type;
23       lock .l_start  =   0 ;
24       lock .l_whence  =  SEEK_SET;
25       lock .l_len  =   0 ;
26 
27       return  fcntl(fd, cmd,  & lock );
28  }
29 
30  void  hander( int  signo)
31  {
32       //  do nothing
33       return ;
34  }
35 
36  int  lockw( char   * fname, LW_FN fn,  int  timeout)
37  {
38       int  ret  =   0 ;
39       int  fd;
40       struct  sigaction act, oact;
41 
42       if  ((fd  =  open(fname,  O_CREAT  |  O_RDWR,  0666 ))  ==   - 1 ) {
43          printf( " open failed!/n " );
44           return   - 1 ;
45      }
46 
47       //  set timer to wakeup fcntl
48      act.sa_handler  =  hander;
49      sigemptyset( & act.sa_mask);
50      act.sa_flags  =   0 //  here, must be zero for wakeup fcntl
51      sigaction(SIGALRM,  & act,  & oact);
52 
53       int  sec  =  alarm(timeout);
54 
55       if  (writew_lock(fd)  ==   0 ) {
56          alarm(sec);
57          // recovery signal handler.
58         sigaction(SIGALRM, &oact, NULL);
59        
60        
printf("locked OK!/n");
61        
62
           //  here, add code about file.
63  #ifdef _TEST
64          getchar();
65          ret  =   0 ;
66  #else
67          ret  =  fn(fname);
68  #endif
69 
70          printf( " unlocked!/n " );
71          unlock(fd);
72      }
73       else  {
74          alarm(sec);
75           //  recovery signal handler.
76          sigaction(SIGALRM,  & oact, NULL);
77           //  lock failed, because of timeout.
78          printf( " write lock failed/n " );
79          ret  =   - 1 ;
80      }
81 
82       return  ret;
83  }
84 
85  //  test code
86  int  func( char   * fname)
87  {
88      printf( " check file:%s /n " , fname);
89      getchar();
90       return   0 ;
91  }
92 
93  int  main()
94  {
95       return  lockw( " file.lock " , func,  5 );
96  }
97 
98 


该程序的原理是,利用了alarm(2)设置的定时器,在一定时间过后会产生SIGALRM信号,会使当前正在
执行的系统调用中断,导致该系统调用(fcntl)返回失败。

上述代码有以下的说明:
 1. 信号处理函数hander是一个空函数,里面什么也不做。它的存在就是为了接收SIGALRM信号
 2. sigactionsa_flags成员一定要设置成0,否则不会是系统调用中断
 3. 为了防止把以前设置的定时器破坏,不管是加锁成功还是失败都立即恢复以前的定时器。
 4. 因为为了接收SIGALRM信号,我们设置了它的信号处理函数。那在加锁失败和成功后也要恢复以前的设定。

注: 虽然上面的代码能实现文件锁超时等待的问题,但又引入了另一个问题,就是该代码会破坏以前设定的定时器,即使是后面也恢复了以前的定时器设置,也会有一些 副作用。比如:当为了等待其他进程释放文件锁,等待的时间(也就是timeout时间)超过了以前设定的定时器触发时间,那这段期间内的以前设定的定时器 就无效了。

posted on 2007-11-26 14:52 lymons 阅读(614) 评论(0)  编辑 收藏 引用 所属分类: C

你可能感兴趣的:(给文件锁设定timeout)