monitor
dp
{
enum
{thinking, hungry, eating} state[5];
condition
self[5];
void
pickup(int i) // following slides
void
putdown(int i) // following slides
void
test(int i) // following slides
void
init()
{
for
(int i = 0; i < 5; i++)
state[i] =
thinking;
}
}
void pickup(int i)
{
state[i] =
hungry;
test[i];
if (state[i] !=
eating)
self[i].wait();
}
void putdown(int i) {
state[i] =
thinking;
// test left and right neighbors
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();
}
}
No comments:
Post a Comment