本篇用信號量實現進程互斥,並解決著名的哲學家就餐問題。
一、進程互斥示例
前面講進程間通信時提到過進程互斥的概念,下面寫個程序模擬一下,流程如下圖:

即父進程打印字符 O,子進程打印字符 X,每次打印一個字符後 sleep 一下。要演示的效果是:在打印程序的邊界有 PV 操作,因此即使中間 sleep 期間時間片輪轉到另一進程,由於資源不可用也不會穿插輸出其他字符,也就是說 O 或 X 都會成對出現,如 OOXXOOOOXXXXXXOO……
程序如下:
#include<sys/types.h> #include<unistd.h> #include<errno.h> #include<sys/ipc.h> #include<sys/sem.h> #include<sys/wait.h> #define ERR_EXIT(m) \ do { \ perror(m); \ exit(EXIT_FAILURE); \ } while(0) union semun { int val; /* Value for SETVAL */ struct semid_ds *buf; /* Buffer for IPC_STAT, IPC_SET */ unsigned short *array; /* Array for GETALL, SETALL */ struct seminfo *__buf; /* Buffer for IPC_INFO (Linux-specific) */ }; int semid; /* pv操作之間的臨界區,導致打印的字符一定是成對出現的 */ void print(char op_char) { int pause_time; srand(getpid()); int i; for (i = 0; i < 10; i++) { sem_p(semid); printf("%c", op_char); fflush(stdout); pause_time = rand() % 3; sleep(pause_time); printf("%c", op_char); fflush(stdout); sem_v(semid); pause_time = rand() % 2; sleep(pause_time); } } int main(void) { semid = sem_create(IPC_PRIVATE); sem_setval(semid, 1); pid_t pid; pid = fork(); if (pid == -1) ERR_EXIT("fork"); if (pid > 0) { print('o'); wait(NULL); sem_d(semid); } else { print('x'); } return 0; }
sem_create 等函數可參考工具集。調用 semget 時指定 key = IPC_PRIVATE,表示創建私有信號集,但具有親緣關係的進程(如父子進程)可見。輸出如下:
simba@ubuntu:~/Documents/code/linux_programming/UNP/system_v$ ./print
ooxxooxxooxxooxxooooooxxooxxooxxooxxxxxx
可見輸出字符都成對出現。分析如下:semval = 1,假設父進程先被調度,它先 P 一下使 semval = 0;子進程在父進程睡眠期間被調度並嘗試 P,使 semval = -1 而阻塞;父進程打印完 V 一下使 semval = 0,喚醒子進程,子進程 P 返回,打印並睡眠後再 V 使 semval = 1。子進程睡眠時父進程又可能嘗試 P,如此循環往復。
二、哲學家就餐問題
哲學家就餐問題的描述可另行參考,這裡採用的解法是:僅當一個哲學家兩邊筷子都可用時才允許他拿筷子。

上圖紅色數字為哲學家編號,共 5 位哲學家,用 5 個進程表示;黑色數字為筷子編號,共 5 根筷子。可定義一個含 5 個信號量的信號集,每個信號量初始值為 1,當某哲學家能同時拿到兩根筷子(同時 P 兩個信號量成功)時即可用餐,否則阻塞等待;用餐後需同時 V 兩個信號量,讓其他進程能 P 成功。
程序如下:
#include<stdio.h> #include<stdlib.h> #include<sys/ipc.h> #include<sys/msg.h> #include<sys/types.h> #include<unistd.h> #include<errno.h> #include<sys/ipc.h> #include<sys/sem.h> #include<sys/wait.h> #define ERR_EXIT(m) \ do { \ perror(m); \ exit(EXIT_FAILURE); \ } while(0) union semun { int val; /* Value for SETVAL */ struct semid_ds *buf; /* Buffer for IPC_STAT, IPC_SET */ unsigned short *array; /* Array for GETALL, SETALL */ struct seminfo *__buf; /* Buffer for IPC_INFO (Linux-specific) */ }; int semid; #define DELAY (rand() % 5 + 1) void wait_for_2fork(int no) { int left = no; int right = (no + 1) % 5; struct sembuf buf[2] = { {left, -1, 0}, {right, -1, 0} }; semop(semid, buf, 2); } void free_2fork(int no) { int left = no; int right = (no + 1) % 5; struct sembuf buf[2] = { {left, 1, 0}, {right, 1, 0} }; semop(semid, buf, 2); } void philosopere(int no) { srand(getpid()); for (; ;) { printf("%d is thinking\n", no); sleep(DELAY); printf("%d is hungry\n", no); wait_for_2fork(no); printf("%d is eating\n", no); sleep(DELAY); free_2fork(no); } } int main(void) { semid = semget(IPC_PRIVATE, 5, IPC_CREAT | 0666); if (semid == -1) ERR_EXIT("semget"); union semun su; su.val = 1; int i; for (i = 0; i < 5; i++) { semctl(semid, i, SETVAL, su); } int no = 0; pid_t pid; for (i = 1; i < 5; i++) { pid = fork(); if (pid == -1) ERR_EXIT("fork"); if (pid == 0) { no = i; break; } } philosopere(no); return 0; }
前面說過,當需要對一個信號集中的多個信號量操作時,要麼全部執行、要麼全部不執行,是一項原子操作。某哲學家需要兩根筷子,即需對兩個信號量同時 P 成功才可用餐;信號量序號 0~4 對應筷子編號,此時 semop 操作 2 個信號量,需定義含 2 個 struct sembuf 的數組 struct sembuf buf[2];
simba@ubuntu:~/Documents/code/linux_programming/UNP/system_v$ ./dinning
0 is thinking
3 is thinking
2 is thinking
4 is thinking
1 is thinking
4 is hungry
4 is eating
0 is hungry
3 is hungry
1 is hungry
1 is eating
2 is hungry
3 is eating
4 is thinking
1 is thinking
0 is eating
4 is hungry
0 is thinking
1 is hungry
1 is eating
3 is thinking
4 is eating
0 is hungry
1 is thinking
2 is eating
0 is eating
4 is thinking
2 is thinking
1 is hungry
3 is hungry
3 is eating
0 is thinking
2 is hungry
1 is eating
4 is hungry
................
可以發現程序始終沒有卡住,即未發生死鎖;同時最多隻有兩位哲學家一起用餐,也不會出現相鄰哲學家同時用餐的情況。