Dining Philosophers¶
The code for this example is located in the examples.philosophers.manage
module.
The dining philosophers problem is an example problem often used in concurrent algorithm design to illustrate synchronisation issues and techniques for resolving them.
The problem¶
Five silent philosophers sit at a round table with each a bowl of spaghetti.
A fork f
is placed between each pair of adjacent philosophers P
:
P P
O f O
f f
P O O P
f f
O
P
Each philosopher P
must alternately think and eat from his bowl O
.
Eating is not limited by the amount of spaghetti left: assume an infinite
supply.
However, a philosopher can only eat while holding both the fork f
to
the left and the fork to the right.
Each philosopher can pick up an adjacent fork, when available, and put it down,
when holding it. These are separate actions: forks must be picked up and put
down one by one.
This implementation will just work. No starvation or dead-lock.
There are two parameters:
- Average eating period, the higher the more time is spend eating.
- Average waiting period, the higher the more frequent philosophers get a chance to eat.
To run the example, type:
pulsar manage.py
Implementation¶
-
class
examples.philosophers.manage.
DiningPhilosophers
(callable=None, load_config=True, **params)[source]¶ -
-
release_forks
(philosopher)[source]¶ The
philosopher
has just eaten and is ready to release both forks.This method releases them, one by one, by sending the
put_down
action to the monitor.
-
take_action
(philosopher)[source]¶ The
philosopher
performs one of these two actions:- eat, if he has both forks and then
release_forks()
. - try to
pickup_fork()
, if he has fewer than 2 forks.
- eat, if he has both forks and then
-
worker_info
(philosopher, info=None)[source]¶ Override
worker_info()
to provide information about the philosopher.
-