MONITORSmonitor DP{
status state[5];
condition self[5];
// Take chopsticks
Take chopsticks(int i){
state[i] = hungry;
test(i);
if (state[i] != Eating)
self[i].wait();
}
// Put chopsticks
put chopsticks(int i){
state[i] = Thinking
test((i+1) % 5);
test((i+4) % 5);
}
test(i){
if (state[(i + 1) % 5] != Eating
&& state[(i + 4) % 5] != Eating
&& state[i] == Hungry){
state[i] = Eating;
}
self[i].signal();
}
}
init(){
for i = 0 to 4
state[i] = Thinking;
}
}