Dining-Philosopher's Monitor解决方案:`pickup(i)`是否需要间接调用`self[i].signal()`?
Dining-Philosopher's Monitor solution: Does `pickup(i)` need to invoke `self[i].signal()` indirectly?
来自操作系统概念
5.8.2 Dining-Philosophers Solution Using Monitors
Next, we illustrate monitor concepts by presenting a deadlock-free
solution to the dining-philosophers problem. This solution imposes the
restriction that a philosopher may pick up her chopsticks only if both
of them are available. To code this solution, we need to distinguish
among three states in which we may find a philosopher. For this
purpose, we introduce the following data structure:
enum {THINKING, HUNGRY, EATING} state[5];
Philosopher i can set the variable state[i] = EATING
only if her two
neighbors are not eating: (state[(i+4) % 5] != EATING)
and
(state[(i+1) % 5] != EATING)
.
We also need to declare
condition self[5];
This allows philosopher i to delay herself when she is hungry but is
unable to obtain the chopsticks she needs.
monitor DiningPhilosophers
{
enum {THINKING, HUNGRY, EATING} state[5];
condition self[5];
void pickup(int i) {
state[i] = HUNGRY;
test(i);
if (state[i] != EATING)
self[i].wait();
}
void putdown(int i) {
state[i] = THINKING;
test((i + 4) % 5);
test((i + 1) % 5);
}
void test(int i) {
if ((state[(i + 4) % 5] != EATING) &&
(state[i] == HUNGRY) &&
(state[(i + 1) % 5] != EATING)) {
state[i] = EATING;
self[i].signal();
}
}
initialization code() {
for (int i = 0; i < 5; i++)
state[i] = THINKING;
}
}
Figure 5.18 A monitor solution to the dining-philosopher problem.
Each philosopher, before starting to eat, must invoke the operation
pickup()
. This act may result in the suspension of the philosopher
process. After the successful completion of the operation, the
philosopher may eat. Following this, the philosopher invokes the
putdown()
operation.
DiningPhilosophers.pickup(i);
...
eat
...
DiningPhilosophers.putdown(i);
pickup(i)
调用 test(i)
,当条件满足时,后者又调用 self[i].signal()
。 pickup(i)
是否需要间接调用 self[i].signal()
?
谢谢。
对 signal()
的调用在拾取期间无效,因为它向当前线程发出信号,根据定义,当前线程不能处于等待状态。
来自操作系统概念
5.8.2 Dining-Philosophers Solution Using Monitors
Next, we illustrate monitor concepts by presenting a deadlock-free solution to the dining-philosophers problem. This solution imposes the restriction that a philosopher may pick up her chopsticks only if both of them are available. To code this solution, we need to distinguish among three states in which we may find a philosopher. For this purpose, we introduce the following data structure:
enum {THINKING, HUNGRY, EATING} state[5];
Philosopher i can set the variable
state[i] = EATING
only if her two neighbors are not eating:(state[(i+4) % 5] != EATING)
and(state[(i+1) % 5] != EATING)
.We also need to declare
condition self[5];
This allows philosopher i to delay herself when she is hungry but is unable to obtain the chopsticks she needs.
monitor DiningPhilosophers { enum {THINKING, HUNGRY, EATING} state[5]; condition self[5]; void pickup(int i) { state[i] = HUNGRY; test(i); if (state[i] != EATING) self[i].wait(); } void putdown(int i) { state[i] = THINKING; test((i + 4) % 5); test((i + 1) % 5); } void test(int i) { if ((state[(i + 4) % 5] != EATING) && (state[i] == HUNGRY) && (state[(i + 1) % 5] != EATING)) { state[i] = EATING; self[i].signal(); } } initialization code() { for (int i = 0; i < 5; i++) state[i] = THINKING; } }
Figure 5.18 A monitor solution to the dining-philosopher problem.
Each philosopher, before starting to eat, must invoke the operation
pickup()
. This act may result in the suspension of the philosopher process. After the successful completion of the operation, the philosopher may eat. Following this, the philosopher invokes theputdown()
operation.DiningPhilosophers.pickup(i); ... eat ... DiningPhilosophers.putdown(i);
pickup(i)
调用 test(i)
,当条件满足时,后者又调用 self[i].signal()
。 pickup(i)
是否需要间接调用 self[i].signal()
?
谢谢。
对 signal()
的调用在拾取期间无效,因为它向当前线程发出信号,根据定义,当前线程不能处于等待状态。