Talk:Dining philosophers: Difference between revisions

From Rosetta Code
Content added Content deleted
No edit summary
(no output needed, anyway here an excerpt)
Line 9: Line 9:


There should be examples of output ; for the novice members. --[[User:Arkapravo|Arkapravo]]19:43 , 8 April 2009 (GMT)
There should be examples of output ; for the novice members. --[[User:Arkapravo|Arkapravo]]19:43 , 8 April 2009 (GMT)
: Output does not add too much information: you do not achieve the task by looking at the output (that theoretically is not predictable); an example of run of the python code on my system gave:

<pre>Russel is hungry.
Russel starts eating
Aristotle is hungry.
Marx is hungry.
Marx swaps forks
Kant is hungry.
Kant starts eating
Budda is hungry.
Russel finishes eating and leaves to think.
Aristotle swaps forks
Marx starts eating
Kant finishes eating and leaves to think.
Budda swaps forks
Aristotle starts eating
Marx finishes eating and leaves to think.
Budda starts eating
Russel is hungry.
Russel swaps forks
Aristotle finishes eating and leaves to think.
Russel starts eating
Budda finishes eating and leaves to think.
Marx is hungry.
Marx swaps forks
Russel finishes eating and leaves to think.
Marx starts eating
Aristotle is hungry.
Aristotle starts eating
Kant is hungry.
Marx finishes eating and leaves to think.
Budda is hungry.
Budda starts eating
Russel is hungry.
Russel swaps forks
Budda finishes eating and leaves to think.
Aristotle finishes eating and leaves to think.
Kant starts eating
Russel starts eating
Budda is hungry.
Marx is hungry.
Marx swaps forks
Aristotle is hungry.
Kant finishes eating and leaves to think.</pre>

: And so on. --[[User:ShinTakezou|ShinTakezou]] 23:14, 11 April 2009 (UTC)

Revision as of 23:14, 11 April 2009

Phyton solution "If a philosopher acquires one fork but can't acquire the second, he releases the first fork before waiting to acquire the other (which then becomes the first fork acquired)."

Without some additional actions this solution is exposed to livelock. That is far less probable than deadlock, yet it may happen.

Livelock: all philosopher grab one fork. Then they see that another one is out of reach. They drop their forks one by one, and then start to grab other forks, in the same order. Under a scheduling policy that keeps philosophers to act in this order, this circle will repeat itself forever. Even if the circle gets actually broken, it is actually a sort of busy waiting, which is not good.

One possible solution to this could be to introduce some policy that would prevent the same fork from being dropped when it was once acquired through waiting, like in the dirty/clean forks solution --Dmitry-kazakov 13:07, 8 November 2008 (UTC)


There should be examples of output ; for the novice members. --Arkapravo19:43 , 8 April 2009 (GMT)

Output does not add too much information: you do not achieve the task by looking at the output (that theoretically is not predictable); an example of run of the python code on my system gave:
Russel is hungry.
Russel starts eating 
Aristotle is hungry.
Marx is hungry.
Marx swaps forks
Kant is hungry.
Kant starts eating 
Budda is hungry.
Russel finishes eating and leaves to think.
Aristotle swaps forks
Marx starts eating 
Kant finishes eating and leaves to think.
Budda swaps forks
Aristotle starts eating 
Marx finishes eating and leaves to think.
Budda starts eating 
Russel is hungry.
Russel swaps forks
Aristotle finishes eating and leaves to think.
Russel starts eating 
Budda finishes eating and leaves to think.
Marx is hungry.
Marx swaps forks
Russel finishes eating and leaves to think.
Marx starts eating
Aristotle is hungry.
Aristotle starts eating 
Kant is hungry.
Marx finishes eating and leaves to think.
Budda is hungry.
Budda starts eating 
Russel is hungry.
Russel swaps forks
Budda finishes eating and leaves to think.
Aristotle finishes eating and leaves to think.
Kant starts eating 
Russel starts eating 
Budda is hungry.
Marx is hungry.
Marx swaps forks
Aristotle is hungry.
Kant finishes eating and leaves to think.
And so on. --ShinTakezou 23:14, 11 April 2009 (UTC)