作業系統›Ch6 行程同步(Process Synchronization)第 16 題/共 32 題
16. Monitor、Dining Philosophers
#OS-06-016中MonitorDining Philosophers
題組題幹(本題:第24題,共 3 小題)點擊展開
Consider the following monitor solution to the 5 dining philosophers problem where there are 5 chopsticks between 5 philosophers who sit on a round table. A philosopher needs to get both chopsticks on the right and left sides to eat. Suppose the index is incremented counterclockwise. With the monitor defined below, each philosopher invokes the operations pickup() and putdown() in the following sequence:
DiningPhilosophers.pickup(i);
EAT
DiningPhilosophers.putdown(i);
monitor DiningPhilosophers
{
enum { THINKING, HUNGRY, EATING } state[5];
condition self[5];
void pickup(int i) {
state[i] = HUNGRY;
test(i);
if (state[i] != HUNGRY) 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;
}
}
One statement in the above program is wrong. Name which statement.
📄 交大110
▤完整推導請見《WH 資工筆記 · 作業系統》Ch6 行程同步(Process Synchronization)