README.rst
1.. zephyr:code-sample:: cmsis-rtos-v1
2 :name: Dining Philosophers (CMSIS RTOS V1 APIs)
3
4 Implement a solution to the Dining Philosophers problem using CMSIS RTOS V1.
5
6Overview
7********
8This sample implements a solution to the `Dining Philosophers problem
9<https://en.wikipedia.org/wiki/Dining_philosophers_problem>`_ (a classic
10multi-thread synchronization problem) using CMSIS RTOS V1 APIs. This particular
11implementation demonstrates the usage of multiple preemptible and cooperative
12threads of differing priorities, as well as mutex/semaphore and thread sleeping
13which uses CMSIS RTOS V1 API implementation in Zephyr.
14
15The philosopher always tries to get the lowest fork first (f1 then f2). When
16done, he will give back the forks in the reverse order (f2 then f1). If he
17gets two forks, he is EATING. Otherwise, he is THINKING. Transitional states
18are shown as well, such as STARVING when the philosopher is hungry but the
19forks are not available, and HOLDING ONE FORK when a philosopher is waiting
20for the second fork to be available.
21
22Each Philosopher will randomly alternate between the EATING and THINKING state.
23
24
25Building and Running
26********************
27
28This project outputs to the console. It can be built and executed
29on QEMU as follows:
30
31.. zephyr-app-commands::
32 :zephyr-app: samples/philosophers
33 :host-os: unix
34 :board: qemu_x86
35 :goals: run
36 :compact:
37
38Sample Output
39=============
40
41.. code-block:: console
42
43 Philosopher 0 [C:-2] THINKING [ 500 ms ]
44 Philosopher 1 [C:-1] THINKING [ 375 ms ]
45 Philosopher 2 [P: 0] THINKING [ 575 ms ]
46 Philosopher 3 [P: 1] EATING [ 525 ms ]
47 Philosopher 4 [P: 2] THINKING [ 800 ms ]
48 Philosopher 5 [P: 3] EATING [ 625 ms ]
49
50 Demo Description
51 ----------------
52 An implementation of a solution to the Dining Philosophers
53 problem (a classic multi-thread synchronization problem) using
54 CMSIS RTOS V1 APIs. This particular implementation demonstrates the
55 usage of multiple preemptible threads of differing
56 priorities, as well as semaphores and thread sleeping.
57
58Exit QEMU by pressing :kbd:`CTRL+A` :kbd:`x`.
59