问题描述:
有五个哲学家公用一张餐桌,分别坐在周围的五张椅子上,在餐桌上有五个碗和五只筷子,他们的生活方式是交替地进行思考和用餐。平时,一个哲学家进行思考,饥饿时便试图拿取其左右最靠近他的筷子,只有在他拿到两只筷子时才能进餐,进餐完毕,放下筷子继续思考。(计算机操作系统 第三版)
书上为代码:
Var chopstick: array[0,...,4] of semaphore; repeat wait(chopstick[i]); wait(chopstick[(i+1)%5]) . . eat: . . signal(chopstick[i]) signal(opstick[(i+1)%5]) . . think: until false;这样当五个哲学家同时饥饿时而各自拿起左边的筷子是,就会使五个信号量chopstick均为0;当他们再试图去拿右边的筷子时,都将因没有筷子可拿而无限地等待。从而导致死锁。
本文解决死锁的策略是:只有当哲学家的左右两只筷子均可以用的时候,才允许其拿起筷子进餐,否则则该哲学家一直请求左边的筷子保持阻塞状态(左边的筷子信号量为1则直接占有,信号量为0则进入阻塞状态),右边的筷子若信号量为0,则非阻塞,进入等待。
详细代码:
/* * philosopher.c * * Created on: 2012-12-17 * Author: xkey */ #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <pthread.h> #include <semaphore.h> #include <string.h> #define PEOPLE_NUM 5 #define THINKING 1 #define HUNGRY 2 #define EATING 3 sem_t chopsticks[PEOPLE_NUM]; pthread_mutex_t mutex; void *philosopher(void *arg){ int id = (int) arg; int state = THINKING; int right = (id + 1) % PEOPLE_NUM; int left = (id + PEOPLE_NUM - 1) % PEOPLE_NUM; char ptrState[32]; while(1){ switch(state){ case THINKING: usleep(300); state = HUNGRY; strcpy(ptrState,"Thinking before eat"); break; case HUNGRY: strcpy(ptrState,"Hungry"); if(sem_wait(&chopsticks[left]) == 0){//阻塞状态 if(sem_trywait(&chopsticks[right]) == 0){//非阻塞 strcpy(ptrState,"I will Eating"); state = EATING; }else{ state = THINKING; strcpy(ptrState,"I have not chopsticks"); sem_post(&chopsticks[left]);//释放请求的得到的left筷子 printf("Philosopher right chopsticks is busy,right=%d,thread id is %d\n",right,id); } }else{ printf("Philosopher left chopsticks is busy,left=%d,thread id is %d\n",left,id);//这句话由于上面被阻塞永远不会输出 } break; case EATING: printf("Philosopher fetch left and right chopsticks: (%d,%d), threadid is %d\n",left,right,id); sem_post(&chopsticks[left]); sem_post(&chopsticks[right]); printf("Philosopher release left and right chopsticks: (%d,%d), threadid is %d\n",left,right,id); usleep(500); state = THINKING; strcpy(ptrState,"Thinking after eat"); break; } pthread_mutex_lock(&mutex); printf("Philosopher is %s, thread id is %d\n",ptrState,id); pthread_mutex_unlock(&mutex); usleep(1000); } pthread_exit((void*)0); } int main(){ pthread_t tid[PEOPLE_NUM]; int i; pthread_mutex_init(&mutex,NULL); for(i = 0 ; i < PEOPLE_NUM ; i ++){ sem_init(&chopsticks[i],0,1); } for(i = 0 ; i < PEOPLE_NUM ; i ++){ pthread_create(&tid[i],NULL,philosopher,(void*)i); } for(i = 0 ; i < PEOPLE_NUM ; i ++){ pthread_join(tid[i],NULL); } return 0; }
这里有个问题,也就是解决死锁的策略
if(sem_wait(&chopsticks[left]) == 0){//阻塞状态
if(sem_trywait(&chopsticks[right]) == 0){//非阻塞
可以考虑如果第二个if使用sem_wait会怎么样,是不是还能保证解决死锁,个人认为均用sem_wait就不需要写if语句了,这样应该会造成死锁;
另:如果两个if均使用sem_trywait会怎么样?
个人觉得这两个问题是本文应该思考的地方,本身哲学家进餐问题的实现不难,关键就是解决死锁的策略。
本文解决哲学家用餐问题使用的是信号量方法,该方法详解的Bolg地址见http://blog.csdn.net/wtz1985/article/details/3826291点击打开链接
另附两篇博文都是关于解决哲学家进餐问题的:
1、使用互斥量:http://blog.csdn.net/kongzhp/article/details/7487150
2、使用信号量(本文参照的方法,但是我看他写的全部是用sem_wait的):http://www.cnblogs.com/margincc/archive/2011/06/30/2094418.html
附:由于本人对Linux多线程研究不深,如有错误,欢迎指教,谢谢。
下面贴出上面代码的执行结果:
Philosopher is Thinking before eat, thread id is 4
Philosopher fetch left and right chopsticks: (0,2), threadid is 1
Philosopher release left and right chopsticks: (0,2), threadid is 1
Philosopher is Thinking after eat, thread id is 1
Philosopher right chopsticks is busy,right=0,thread id is 4
Philosopher is I have not chopsticks, thread id is 4
Philosopher is Thinking before eat, thread id is 1
Philosopher fetch left and right chopsticks: (4,1), threadid is 0
Philosopher release left and right chopsticks: (4,1), threadid is 0
Philosopher is Thinking before eat, thread id is 3
Philosopher is Thinking before eat, thread id is 4
Philosopher is I will Eating, thread id is 1
Philosopher is Thinking after eat, thread id is 0
Philosopher is Thinking before eat, thread id is 2
Philosopher right chopsticks is busy,right=0,thread id is 4
Philosopher is I have not chopsticks, thread id is 4
Philosopher fetch left and right chopsticks: (0,2), threadid is 1
Philosopher release left and right chopsticks: (0,2), threadid is 1
Philosopher is I will Eating, thread id is 3
Philosopher is I will Eating, thread id is 2
Philosopher is Thinking before eat, thread id is 0
Philosopher is Thinking after eat, thread id is 1
Philosopher is Thinking before eat, thread id is 4
Philosopher fetch left and right chopsticks: (2,4), threadid is 3
Philosopher release left and right chopsticks: (2,4), threadid is 3
Philosopher fetch left and right chopsticks: (1,3), threadid is 2
Philosopher release left and right chopsticks: (1,3), threadid is 2
Philosopher is I will Eating, thread id is 0
Philosopher is Thinking after eat, thread id is 3
Philosopher is Thinking before eat, thread id is 1
Philosopher is I will Eating, thread id is 4
Philosopher is Thinking after eat, thread id is 2
Philosopher fetch left and right chopsticks: (4,1), threadid is 0
Philosopher is Thinking before eat, thread id is 3
Philosopher fetch left and right chopsticks: (3,0), threadid is 4
Philosopher release left and right chopsticks: (3,0), threadid is 4
Philosopher release left and right chopsticks: (4,1), threadid is 0
可以看到,一开始线程4就被阻塞,原因就是右边的筷子0被线程1占有
红色部分,线程4一直不能同时得到左右两边的筷子
紫色部分,线程2左边的筷子1是空闲的,但是右边的筷子3之前已经被线程4所请求过,但是正是由于线程4不能得到其右边的筷子0,所以放弃了已经占有的其左边筷子3,所以线程2能够执行。
蓝色体现了多线程的同步执行,除此之外,发现线程4同时得到了左右两边的筷子3和0