哲学家就餐问题的一个解法
2012-05-26 21:35
232 查看
哲学家就餐问题的简单描述:
五个哲学家围坐在一张圆桌周围,每个哲学家面前都有一盘通心粉。由于通心粉很滑,所以需要两把叉子才能夹住。相邻的两个盘子之间放有一把叉子。
#define N 5
#define LEFT (i+N-1)%N
#define RIGHT (i+1)%N
#define THINKING 0
#define HUNGRY 1
#define EATING 2
typedef int semaphore;
int state
semaphore mutex = 1;
semaphore s
;
void philosopher(int i)
{
while(TRUE){
}
}
void take_forks(int i)
{
down(&mutex);
state[i] = HUNGRY;
test(i);
up(&mutex);
down(&s[i]);
}
void put_forks(i)
{
down(&mutex);
state[i] = THINKING;
test(LEFT);
test(RIGHT);
up(&mutex);
}
void test(i)
{
if(state[i] == HUNGRY && state[LEFT] !=EATING && state[RIGHT] != EATING){
state[i] = EATING;
up(&s[i]);
}
}
五个哲学家围坐在一张圆桌周围,每个哲学家面前都有一盘通心粉。由于通心粉很滑,所以需要两把叉子才能夹住。相邻的两个盘子之间放有一把叉子。
#define N 5
#define LEFT (i+N-1)%N
#define RIGHT (i+1)%N
#define THINKING 0
#define HUNGRY 1
#define EATING 2
typedef int semaphore;
int state
semaphore mutex = 1;
semaphore s
;
void philosopher(int i)
{
while(TRUE){
}
}
void take_forks(int i)
{
down(&mutex);
state[i] = HUNGRY;
test(i);
up(&mutex);
down(&s[i]);
}
void put_forks(i)
{
down(&mutex);
state[i] = THINKING;
test(LEFT);
test(RIGHT);
up(&mutex);
}
void test(i)
{
if(state[i] == HUNGRY && state[LEFT] !=EATING && state[RIGHT] != EATING){
state[i] = EATING;
up(&s[i]);
}
}
相关文章推荐
- 操作系统哲学家就餐问题——定义一个互斥信号量mutex
- Chandy/Misra解法 解决哲学家就餐问题 java实现
- 死锁与哲学家就餐问题
- 一个输入问题的三种解法
- 哲学家就餐问题的分析与解决方案
- 哲学家就餐问题
- 多线程之哲学家就餐问题
- 【经典操作系统问题】哲学家就餐问题分析
- 换零钱问题的非递归解法 SICP 1.2.2中的一个问题
- 一个简单文本处理问题的多种解法
- 经典IPC问题-哲学家就餐分析与解决
- 一个排列组合问题的解法
- 哲学家就餐问题
- 哲学家就餐问题
- 大力的先入先出的库存物品,按批次出货的问题的一个解法。
- 哲学家就餐问题
- 多线程模拟哲学家就餐问题
- [转]算法讨论:哲学家就餐问题
- 哲学家就餐问题及其一种java多线程实现
- 哲学家就餐问题Java版解决案