《Unix環境高級編程》這本書附帶了許多短小精美的小程序,我在閱讀此書的時候,將書上的代碼按照自己的理解重寫了一遍(大部分是抄書上的),加深一下自己的理解(純看書太困了,呵呵)。此例子在Ubuntu 10.04上測試通過。
相關鏈接
- 《UNIX環境高級編程》(第二版)apue.h的錯誤 http://www.linuxidc.com/Linux/2011-04/34662.htm
- Unix環境高級編程 源代碼地址 http://www.linuxidc.com/Linux/2011-04/34826.htm
程序簡介:這個例子是一個會發生死鎖的程序。
- //《APUE》程序14-2:加鎖和解鎖一個文件區域
- //《APUE》程序14-4:死鎖檢測實例
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <errno.h>
- #include <signal.h>
- #include <sys/stat.h>
-
- #define read_lock(fd, offset, whence, len) \
- lock_reg((fd), F_SETLK, F_RDLCK, (offset), (whence), (len))
- #define readw_lock(fd, offset, whence, len) \
- lock_reg((fd), F_SETLKW, F_RDLCK, (offset), (whence), (len))
- #define write_lock(fd, offset, whence, len) \
- lock_reg((fd), F_SETLK, F_WRLCK, (offset), (whence), (len))
- #define writew_lock(fd, offset, whence, len) \
- lock_reg((fd), F_SETLKW, F_WRLCK, (offset), (whence), (len))
- #define un_lock(fd, offset, whence, len) \
- lock_reg((fd), F_SETLK, F_UNLCK, (offset), (whence), (len))
-
- #define FILE_MODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)
-
- sig_atomic_t sigflag; /* set nonzero by sig handler */
- sigset_t newmask, oldmask, zeromask;
-
- //輸出錯誤信息並退出
- void error_quit(const char *str)
- {
- fprintf(stderr, "%s\n", str);
- exit(1);
- }
-
- static void sig_usr(int signo) /* one signal handler for SIGUSR1 and SIGUSR2 */
- {
- sigflag = 1;
- }
-
- void TELL_WAIT(void)
- {
- if (signal(SIGUSR1, sig_usr) == SIG_ERR)
- error_quit("signal(SIGUSR1) error");
- if (signal(SIGUSR2, sig_usr) == SIG_ERR)
- error_quit("signal(SIGUSR2) error");
- sigemptyset(&zeromask);
- sigemptyset(&newmask);
- sigaddset(&newmask, SIGUSR1);
- sigaddset(&newmask, SIGUSR2);
-
- /*
- * Block SIGUSR1 and SIGUSR2, and save current signal mask.
- */
- if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0)
- error_quit("SIG_BLOCK error");
- }
-
- void TELL_PARENT(pid_t pid)
- {
- kill(pid, SIGUSR2); /* tell parent we're done */
- }
-
- void WAIT_PARENT(void)
- {
- while (sigflag == 0)
- sigsuspend(&zeromask); /* and wait for parent */
- sigflag = 0;
-
- /*
- * Reset signal mask to original value.
- */
- int temp = sigprocmask(SIG_SETMASK, &oldmask, NULL);
- if (temp < 0)
- error_quit("SIG_SETMASK error");
- }
-
- void TELL_CHILD(pid_t pid)
- {
- kill(pid, SIGUSR1); /* tell child we're done */
- }
-
- void WAIT_CHILD(void)
- {
- while (sigflag == 0)
- sigsuspend(&zeromask); /* and wait for child */
- sigflag = 0;
-
- /*
- * Reset signal mask to original value.
- */
- int temp = sigprocmask(SIG_SETMASK, &oldmask, NULL);
- if (temp < 0)
- error_quit("SIG_SETMASK error");
- }
-
- //加鎖或解鎖某個文件區域
- int lock_reg(int fd, int cmd, int type, off_t offset,
- int whence, off_t len)
- {
- struct flock lock;
- lock.l_type = type;
- lock.l_start = offset;
- lock.l_whence = whence;
- lock.l_len = len;
- return fcntl(fd, cmd, &lock);
- }
-
- //鎖住文件中的一個字節
- void lockabyte(const char *name, int fd, off_t offset)
- {
- //在我的系統上(Ubuntu10.04),發生死鎖時writew_lock並不會返回-1
- if( writew_lock(fd, offset, SEEK_SET, 1) < 0 )
- error_quit("writew_lock error");
- printf("%s: got the lock, byte %ld\n", name, offset);
- }
-
- int main(void)
- {
- int fd;
- pid_t pid;
-
- fd = creat("templock", FILE_MODE);
- if( fd < 0 )
- error_quit("create error");
- if( write(fd, "ab", 2) != 2 )
- error_quit("write error");
-
- TELL_WAIT();
- pid = fork();
- if( pid < 0 )
- error_quit("fork error");
- else if( pid == 0 )
- {
- lockabyte("child", fd, 0);
- TELL_PARENT( getpid() );
- WAIT_PARENT();
- lockabyte("child", fd, 1);
- }
- else
- {
- lockabyte("parent", fd, 1);
- TELL_CHILD(pid);
- WAIT_CHILD();
- lockabyte("parent", fd, 0);
- }
- return 0;
- }
注解:
1:在該程序中,子進程鎖住字節0,父進程鎖住字節1,然後,它們又都試圖鎖住對方已經加鎖的字節,這樣就造成了死鎖。
2:《Unix環境高級編程》上說:檢測到死鎖時,內核必須選擇一個進程出錯返回。但在我的系統中,父子進程都被卡住,只有當你強制中斷時(Ctrl+C)時,程序才會結束。這個問題以後找個時間來研究一下。