a Suppose for each philosopher the two wait statements were
a. Suppose, for each philosopher, the two wait statements were reversed. Explain how this changes the problem? Does this impact whether or not there is deadlock?
b.suppose, for each philosopher, the two signal statements were reversed but not the wait statements. Explain how this changes the problem? Does this impact whether or not there is deadlock?
Solution
wait(chopstick[i]); // it is in waiting state(left chopstick)
wait(chopstick[(i+1) mod 5]); //it is in waiting state(right chopstick)
eat
signal(chopstick[i]); //releasing the left chopstick
signal(chopstick[(i+1) mod 5]); // releasing the right chopstick
----------------------------------------------------------------------------------------
Deadloc-->This situation will occur when thread has locked an object and waiting for other object to be released
by another thread,and ther other thread is also waiting for the first thread to release the first object,both threads
will continue waiting forever.
a)If we reverse wait statements then
1.wait(chopstick[(i+1) mod 5]);
2.wait(chopstick[i]);
first wait statement will look for corresponding notify statement here signal(chopstick[(i+1) mod 5]); is
release statement for first wait statement so when corresponding release statement is found it comes out from
wait state.
1.signal(chopstick[i]);
2.signal(chopstick[(i+1) mod 5]);
Here corresponding release statement is second one.So first wait statement is released.But second wait statement
wont released it is waiting state only.because after second wait statement there is no matching release statement for
first wait statement.As it wont go back.
-------------------------------------------------------------------------------------------------------------------
b) If we reverse the signal statements keeping wait statement as they are.
Then first wait statement searches for corresponding release statement first it encounters right chopstick signal
method which is not matched so it moves to next signal here left chopstick method found which is matched.
so first wait method (wait(chopstick[i]);)is release.
second wait method (wait(chopstick[(i+1) mod 5]);) is still in waiting state only
