Question: Consider the following solution to the dining Philosopher problem (which was discussed in class but has a deadlock problem). Rewrite the code below to resolve
Consider the following solution to the dining Philosopher problem (which was discussed in class but has a deadlock problem). Rewrite the code below to resolve the deadlock problem such that even-numbered philosophers select chopsticks to their right, first, then to their left; and odd-numbered philosophers select chopsticks to their left, first, then to their right. [Note: Assume the P() and V() are two primitive functions that guard the entrance and exit of the critical section.]
Philosopher(i) {
semaphore chopsticks[5] = 1; /* assume these are binary semaphores */
while (true)
{
P(chopstick[i]); /* pick left */
P(chopstick[(i+1) % 5); /* pick right */
phil_eating();
V(chopstick[(i+1) % 5); /* return right */
V(chopstick[i]); /* return left */
phil_thinking();
}
I don't need full lines of code, just need to arrange this code in a way where there will be no deadlock!
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
