【发布时间】:2018-01-21 06:06:42
【问题描述】:
这个伪代码能否以最大并行度解决哲学家进餐问题?这里mutex是一个初始化为1的二进制信号量。假设叉子的编号从0到(N-1)。从 0 到 (N-1) 共有 N 个哲学家。
void philosopher(int i) // ith philosopher
{
while (true)
{
think();
down(&mutex); // acquire lock
take_fork(i); // take left fork
take_fork((i+1)%N); // take right fork
up(&mutex); // release the lock
eat();
down(&mutex); // acquire lock
put_fork(i);
put_fork((i+1)%N);
up(&mutex); // release the lock
}
}
这应该以最大的并行度解决哲学家进餐问题,因为在一位哲学家获得两个分叉后,锁就会被释放。但会吗?会不会有活力的问题?我很困惑。
【问题讨论】:
-
当哲学家获得互斥体并发现他的右叉被占用时会发生什么?
take_fork会失败从而导致左叉未释放吗? -
这应该是不可能的,因为互斥锁已经被其他哲学家关闭了。 @DmitriChubarov
标签: concurrency semaphore dining-philosopher binary-semaphore