2024-08-24 00:47:19 +08:00
|
|
|
.. zephyr:code-sample:: posix-philosophers
|
|
|
|
:name: POSIX Philosophers
|
2024-01-15 00:23:40 +08:00
|
|
|
|
2024-08-24 00:47:19 +08:00
|
|
|
Implement a solution to the Dining Philosophers problem using the POSIX API.
|
2024-01-15 00:23:40 +08:00
|
|
|
|
|
|
|
Overview
|
|
|
|
********
|
|
|
|
|
2024-09-16 22:28:19 +08:00
|
|
|
This sample implements Zephyr's :zephyr:code-sample:`dining-philosophers` sample using the
|
2024-01-15 00:23:40 +08:00
|
|
|
:ref:`POSIX API <posix_support>`. The source code for this sample can be found under
|
|
|
|
:file:`samples/posix/philosophers`.
|
|
|
|
|
|
|
|
Building and Running
|
|
|
|
********************
|
|
|
|
|
2024-01-31 13:24:09 +08:00
|
|
|
This project outputs to the console. It can be built and executed on QEMU as follows:
|
2024-01-15 00:23:40 +08:00
|
|
|
|
|
|
|
.. zephyr-app-commands::
|
|
|
|
:zephyr-app: samples/posix/philosophers
|
|
|
|
:host-os: unix
|
|
|
|
:board: qemu_riscv64
|
|
|
|
:goals: run
|
|
|
|
:compact:
|
|
|
|
|
|
|
|
Sample Output
|
|
|
|
=============
|
|
|
|
|
|
|
|
.. 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 ]
|
|
|
|
|
|
|
|
Exit QEMU by pressing :kbd:`CTRL+A` :kbd:`x`.
|
|
|
|
|
|
|
|
Debugging
|
|
|
|
*********
|
|
|
|
|
|
|
|
Like the original philosophers sample, the POSIX variant also enables
|
|
|
|
:kconfig:option:`CONFIG_DEBUG_THREAD_INFO` by default.
|
|
|
|
|
|
|
|
.. zephyr-app-commands::
|
|
|
|
:zephyr-app: samples/philosophers
|
|
|
|
:host-os: unix
|
|
|
|
:board: <board_name>
|
|
|
|
:goals: debug
|
|
|
|
:compact:
|
|
|
|
|
|
|
|
Additional Information
|
|
|
|
**********************
|
|
|
|
|
2024-09-16 22:28:19 +08:00
|
|
|
For additional information, please refer to the :zephyr:code-sample:`dining-philosophers` sample.
|