Dining Philosophers are Changing

It was a home question, and I realized that it was wrong. I really want to understand the solution to this issue. The initial state of the code:

The solution presented in Fig. 2-46 is deadlock-free and allows the maximum parallelism for an arbitrary number of philosophers. It uses an array, state, to keep track of whether a philosopher is eating, thinking, or hungry (trying to acquire forks). A philosopher may only move into eating state if neither neighbor is eating. Philosopher i neighbors are defined by the macros LEFT and RIGHT. In other words, if i is 2, LEFT is 1 and RIGHT is 3. The program uses an array of semaphores, one per philosopher, so hungry philosophers can block if the needed forks are busy.

enter image description here

Question: Consider the procedure put_forks in Fig. 2-46. Suppose that the variable state[i] was set to THINKING after the two calls to test, rather than before. How would this change affect the solution?

+3
source share
1 answer

If the state change in put_forks is postponed after the tests, this philosopher will still be in the EATING state during the tests, and none of the adjacent philosophers will be able to change the state to EATING. This means that any philosopher who is ever blocked because he is starving when a neighboring philosopher is traveling - at the end of the take_forks method - will remain locked forever.

+4
source

All Articles