Monitor Solution to Dining Philosophers monitor DiningPhilosophers { enum {THINK
ID: 3582459 • Letter: M
Question
Monitor Solution to Dining Philosophers 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 left and right neighbors test((i + 4) % 5); test((i + 1) % 5); } (continued) 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; iExplanation / Answer
a) Solution : Only one philosopher can eat at a time.
Explanation: In DiningPhilosopher problem, five philosophers are sitting in circle.
Each have a plate in front of them and a fork is placed between two plates i.e, five forks.
Each philosopher spends his/her life alternating between thinking and eating.
A philosophers can eat only when he is hungry and have both right and left forks available.
This is implemented by test method, which checks if the philosopher is hungry and his left ((i+4)%5) and right ((i+1)%5) philosophers are not eating.
b) Each philosopher can be in one of the three states THINKING, HUNGRY, or EATING.
Pickup and putdown are activities.
Initially each philosopher is in THINKING state.
When he gets into HUNGRY state, Pickup forks available to his left and right and waits until both forks are available before entering EATING state
Then putdown both forks and get back to THINKING state.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.