2017-04-05 03:27:31 +08:00
|
|
|
.. _dining-philosophers-sample:
|
|
|
|
|
2017-01-05 21:54:33 +08:00
|
|
|
Dining Philosophers
|
|
|
|
###################
|
|
|
|
|
|
|
|
Overview
|
2017-01-21 07:58:05 +08:00
|
|
|
********
|
2017-01-05 21:54:33 +08:00
|
|
|
|
|
|
|
An implementation of a solution to the Dining Philosophers problem (a classic
|
|
|
|
multi-thread synchronization problem). This particular implementation
|
|
|
|
demonstrates the usage of multiple preemptible and cooperative threads of
|
|
|
|
differing priorities, as well as dynamic mutexes and thread sleeping.
|
|
|
|
|
|
|
|
The philosopher always tries to get the lowest fork first (f1 then f2). When
|
|
|
|
done, he will give back the forks in the reverse order (f2 then f1). If he
|
|
|
|
gets two forks, he is EATING. Otherwise, he is THINKING. Transitional states
|
|
|
|
are shown as well, such as STARVING when the philosopher is hungry but the
|
|
|
|
forks are not available, and HOLDING ONE FORK when a philosopher is waiting
|
|
|
|
for the second fork to be available.
|
|
|
|
|
|
|
|
Each Philosopher will randomly alternate between the EATING and THINKING state.
|
|
|
|
|
2017-01-15 08:33:23 +08:00
|
|
|
It is possible to run the demo in coop-only or preempt-only mode. To achieve
|
|
|
|
this, set these values for CONFIG_NUM_COOP_PRIORITIES and
|
|
|
|
CONFIG_NUM_PREEMPT_PRIORITIES in prj.conf:
|
|
|
|
|
|
|
|
preempt-only:
|
|
|
|
|
|
|
|
CONFIG_NUM_PREEMPT_PRIORITIES 6
|
|
|
|
CONFIG_NUM_COOP_PRIORITIES 0
|
|
|
|
|
|
|
|
coop-only:
|
|
|
|
|
|
|
|
CONFIG_NUM_PREEMPT_PRIORITIES 0
|
|
|
|
CONFIG_NUM_COOP_PRIORITIES 6
|
|
|
|
|
|
|
|
In these cases, the philosopher threads will run with priorities 0 to 5
|
|
|
|
(preempt-only) and -7 to -2 (coop-only).
|
|
|
|
|
2017-01-05 21:54:33 +08:00
|
|
|
Building and Running
|
2017-01-21 07:58:05 +08:00
|
|
|
********************
|
2017-01-05 21:54:33 +08:00
|
|
|
|
|
|
|
This project outputs to the console. It can be built and executed
|
|
|
|
on QEMU as follows:
|
|
|
|
|
2017-11-12 17:11:39 +08:00
|
|
|
.. zephyr-app-commands::
|
|
|
|
:zephyr-app: samples/philosophers
|
|
|
|
:board: qemu_x86
|
|
|
|
:goals: run
|
|
|
|
:compact:
|
2017-01-05 21:54:33 +08:00
|
|
|
|
|
|
|
Sample Output
|
2017-01-21 07:58:05 +08:00
|
|
|
=============
|
2017-01-05 21:54:33 +08:00
|
|
|
|
|
|
|
.. code-block:: console
|
|
|
|
|
|
|
|
Philosopher 0 [P: 3] HOLDING ONE FORK
|
|
|
|
Philosopher 1 [P: 2] HOLDING ONE FORK
|
|
|
|
Philosopher 2 [P: 1] EATING [ 1900 ms ]
|
|
|
|
Philosopher 3 [P: 0] THINKING [ 2500 ms ]
|
|
|
|
Philosopher 4 [C:-1] THINKING [ 2200 ms ]
|
|
|
|
Philosopher 5 [C:-2] THINKING [ 1700 ms ]
|
|
|
|
|
|
|
|
|