場景:
原版的故事里有五個哲學家(不過我們寫的程序可以有N個哲學家),這些哲學家們只做兩件事--思考和吃飯,他們思考的時候不需要任何共享資源,但是吃飯的時候就必須使用餐具,而餐桌上的餐具是有限的,原版的故事里,餐具是叉子,吃飯的時候要用兩把叉子把面條從碗里撈出來。很顯然把叉子換成筷子會更合理,所以:一個哲學家需要兩根筷子才能吃飯。
現在引入問題的關鍵:這些哲學家很窮,只買得起五根筷子。他們坐成一圈,兩個人的中間放一根筷子。哲學家吃飯的時候必須同時得到左手邊和右手邊的筷子。如果他身邊的任何一位正在使用筷子,那他只有等着。
假設哲學家的編號是A、B、C、D、E,筷子編號是1、2、3、4、5,哲學家和筷子圍成一圈如下圖所示:
- #include <stdio.h>
- #include <stdlib.h>
- #include <memory.h>
- #include <pthread.h>
- #include <errno.h>
- #include <math.h>
- //筷子作為mutex
- pthread_mutex_t chopstick[6] ;
- void *eat_think(void *arg)
- {
- char phi = *(char *)arg;
- int left,right; //左右筷子的編號
- switch (phi){
- case 'A':
- left = 5;
- right = 1;
- break;
- case 'B':
- left = 1;
- right = 2;
- break;
- case 'C':
- left = 2;
- right = 3;
- break;
- case 'D':
- left = 3;
- right = 4;
- break;
- case 'E':
- left = 4;
- right = 5;
- break;
- }
- int i;
- for(;;){
- usleep(3); //思考
- pthread_mutex_lock(&chopstick[left]); //拿起左手的筷子
- printf("Philosopher %c fetches chopstick %d\n", phi, left);
- if (pthread_mutex_trylock(&chopstick[right]) == EBUSY){ //拿起右手的筷子
- pthread_mutex_unlock(&chopstick[left]); //如果右邊筷子被拿走放下左手的筷子
- continue;
- }
- // pthread_mutex_lock(&chopstick[right]); //拿起右手的筷子,如果想觀察死鎖,把上一句if注釋掉,再把這一句的注釋去掉
- printf("Philosopher %c fetches chopstick %d\n", phi, right);
- printf("Philosopher %c is eating.\n",phi);
- usleep(3); //吃飯
- pthread_mutex_unlock(&chopstick[left]); //放下左手的筷子
- printf("Philosopher %c release chopstick %d\n", phi, left);
- pthread_mutex_unlock(&chopstick[right]); //放下左手的筷子
- printf("Philosopher %c release chopstick %d\n", phi, right);
- }
- }
- int main(){
- pthread_t A,B,C,D,E; //5個哲學家
- int i;
- for (i = 0; i < 5; i++)
- pthread_mutex_init(&chopstick[i],NULL);
- pthread_create(&A,NULL, eat_think, "A");
- pthread_create(&B,NULL, eat_think, "B");
- pthread_create(&C,NULL, eat_think, "C");
- pthread_create(&D,NULL, eat_think, "D");
- pthread_create(&E,NULL, eat_think, "E");
- pthread_join(A,NULL);
- pthread_join(B,NULL);
- pthread_join(C,NULL);
- pthread_join(D,NULL);
- pthread_join(E,NULL);
- return 0;
- }
注意:
1. gcc編譯時,加上-lphread選項,例:gcc eating.c -lpthread
2.pthread_create的第四個參數是void *類型,我一開始傳一個char類型,即'A',就會發生段錯誤。但改成char *,即“A”就沒問題了。
3.usleep是毫秒級的阻塞