《APUE》:死锁检测实例


《Unix环境高级编程》这本书附带了许多短小精美的小程序,我在阅读此书的时候,将书上的代码按照自己的理解重写了一遍(大部分是抄书上的),加深一下自己的理解(纯看书太困了,呵呵)。此例子在Ubuntu 10.04上测试通过。

相关链接

  • 《UNIX环境高级编程》(第二版)apue.h的错误
  • Unix环境高级编程 源代码地址

程序简介:这个例子是一个会发生死锁的程序。

  1. //《APUE》程序14-2:加锁和解锁一个文件区域  
  2. //《APUE》程序14-4:死锁检测实例  
  3. #include <stdio.h>  
  4. #include <stdlib.h>  
  5. #include <unistd.h>  
  6. #include <fcntl.h>  
  7. #include <errno.h>  
  8. #include <signal.h>  
  9. #include <sys/stat.h>   
  10.  
  11. #define read_lock(fd, offset, whence, len) \  
  12.     lock_reg((fd), F_SETLK, F_RDLCK, (offset), (whence), (len)) 
  13. #define readw_lock(fd, offset, whence, len) \  
  14.     lock_reg((fd), F_SETLKW, F_RDLCK, (offset), (whence), (len)) 
  15. #define write_lock(fd, offset, whence, len) \  
  16.     lock_reg((fd), F_SETLK, F_WRLCK, (offset), (whence), (len)) 
  17. #define writew_lock(fd, offset, whence, len) \  
  18.     lock_reg((fd), F_SETLKW, F_WRLCK, (offset), (whence), (len)) 
  19. #define un_lock(fd, offset, whence, len) \  
  20.     lock_reg((fd), F_SETLK, F_UNLCK, (offset), (whence), (len)) 
  21.  
  22. #define FILE_MODE   (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)  
  23.  
  24. sig_atomic_t sigflag; /* set nonzero by sig handler */ 
  25. sigset_t newmask, oldmask, zeromask; 
  26.  
  27. //输出错误信息并退出     
  28. void error_quit(const char *str)     
  29. {     
  30.     fprintf(stderr, "%s\n", str);     
  31.     exit(1);     
  32.  
  33. static void sig_usr(int signo)  /* one signal handler for SIGUSR1 and SIGUSR2 */ 
  34.     sigflag = 1; 
  35.  
  36. void TELL_WAIT(void
  37.     if (signal(SIGUSR1, sig_usr) == SIG_ERR) 
  38.         error_quit("signal(SIGUSR1) error"); 
  39.     if (signal(SIGUSR2, sig_usr) == SIG_ERR) 
  40.         error_quit("signal(SIGUSR2) error"); 
  41.     sigemptyset(&zeromask); 
  42.     sigemptyset(&newmask); 
  43.     sigaddset(&newmask, SIGUSR1); 
  44.     sigaddset(&newmask, SIGUSR2); 
  45.  
  46.     /* 
  47.     * Block SIGUSR1 and SIGUSR2, and save current signal mask. 
  48.     */ 
  49.     if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0) 
  50.         error_quit("SIG_BLOCK error"); 
  51.  
  52. void TELL_PARENT(pid_t pid) 
  53.     kill(pid, SIGUSR2);     /* tell parent we're done */ 
  54.  
  55. void WAIT_PARENT(void
  56.     while (sigflag == 0) 
  57.         sigsuspend(&zeromask);  /* and wait for parent */ 
  58.     sigflag = 0; 
  59.  
  60.     /* 
  61.     * Reset signal mask to original value. 
  62.     */ 
  63.     int temp = sigprocmask(SIG_SETMASK, &oldmask, NULL); 
  64.     if (temp < 0) 
  65.         error_quit("SIG_SETMASK error"); 
  66.  
  67. void TELL_CHILD(pid_t pid) 
  68.     kill(pid, SIGUSR1);         /* tell child we're done */ 
  69.  
  70. void WAIT_CHILD(void
  71.     while (sigflag == 0) 
  72.         sigsuspend(&zeromask);  /* and wait for child */ 
  73.     sigflag = 0; 
  74.  
  75.     /* 
  76.     * Reset signal mask to original value. 
  77.     */ 
  78.     int temp = sigprocmask(SIG_SETMASK, &oldmask, NULL); 
  79.     if (temp < 0) 
  80.         error_quit("SIG_SETMASK error"); 
  81.  
  82. //加锁或解锁某个文件区域  
  83. int lock_reg(int fd, int cmd, int type, off_t offset, 
  84.              int whence, off_t len) 
  85.     struct flock lock; 
  86.     lock.l_type = type; 
  87.     lock.l_start = offset; 
  88.     lock.l_whence = whence; 
  89.     lock.l_len = len; 
  90.     return fcntl(fd, cmd, &lock); 
  91.  
  92. //锁住文件中的一个字节  
  93. void lockabyte(const char *name, int fd, off_t offset) 
  94.     //在我的系统上(Ubuntu10.04),发生死锁时writew_lock并不会返回-1  
  95.     if( writew_lock(fd, offset, SEEK_SET, 1) < 0 ) 
  96.         error_quit("writew_lock error"); 
  97.     printf("%s: got the lock, byte %ld\n", name, offset); 
  98.  
  99. int main(void
  100.     int fd; 
  101.     pid_t pid; 
  102.  
  103.     fd = creat("templock", FILE_MODE); 
  104.     if( fd < 0 ) 
  105.         error_quit("create error"); 
  106.     if( write(fd, "ab", 2) != 2 ) 
  107.         error_quit("write error"); 
  108.  
  109.     TELL_WAIT(); 
  110.     pid = fork(); 
  111.     if( pid < 0 ) 
  112.         error_quit("fork error"); 
  113.     else if( pid == 0 ) 
  114.     { 
  115.         lockabyte("child", fd, 0); 
  116.         TELL_PARENT( getpid() ); 
  117.         WAIT_PARENT(); 
  118.         lockabyte("child", fd, 1); 
  119.     } 
  120.     else 
  121.     { 
  122.         lockabyte("parent", fd, 1); 
  123.         TELL_CHILD(pid); 
  124.         WAIT_CHILD(); 
  125.         lockabyte("parent", fd, 0); 
  126.     } 
  127.     return 0; 

注解:

1:在该程序中,子进程锁住字节0,父进程锁住字节1,然后,它们又都试图锁住对方已经加锁的字节,这样就造成了死锁。

2:《Unix环境高级编程》上说:检测到死锁时,内核必须选择一个进程出错返回。但在我的系统中,父子进程都被卡住,只有当你强制中断时(Ctrl+C)时,程序才会结束。这个问题以后找个时间来研究一下。

相关内容