zephyr/samples/subsys/portability/cmsis_rtos_v1/philosophers
Alberto Escolar Piedras 196341c18b samples: Switch integration_platforms from native_posix to native_sim
For all remaining samples which now set their integration platform
as native_posix(_64) switch them to native_sim(_64)

Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
2023-11-20 12:02:48 +01:00
..
src boards: x86: Add intel_ prefix to Elkhart Lake boards 2023-08-15 11:24:35 +00:00
CMakeLists.txt include: Prefix includes to use a scope 2022-04-08 19:03:32 +02:00
prj.conf samples: cmsis_rtos_v1: philosophers: increas CMSIS_THREAD_MAX_STACK_SIZE 2023-04-04 16:23:37 +00:00
README.rst doc: Migrate subsys/ code samples to new Sphinx extension 2023-09-21 09:28:31 +02:00
sample.yaml samples: Switch integration_platforms from native_posix to native_sim 2023-11-20 12:02:48 +01:00

.. zephyr:code-sample:: cmsis-rtos-v1
   :name: Dining Philosophers (CMSIS RTOS V1 APIs)

   Implement a solution to the Dining Philosophers problem using CMSIS RTOS V1.

Overview
********
This sample implements a solution to the `Dining Philosophers problem
<https://en.wikipedia.org/wiki/Dining_philosophers_problem>`_ (a classic
multi-thread synchronization problem) using CMSIS RTOS V1 APIs.  This particular
implementation demonstrates the usage of multiple preemptible and cooperative
threads of differing priorities, as well as mutex/semaphore and thread sleeping
which uses CMSIS RTOS V1 API implementation in Zephyr.

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.


Building and Running
********************

This project outputs to the console.  It can be built and executed
on QEMU as follows:

.. zephyr-app-commands::
   :zephyr-app: samples/philosophers
   :host-os: unix
   :board: qemu_x86
   :goals: run
   :compact:

Sample Output
=============

.. code-block:: console

    Philosopher 0 [C:-2]  THINKING [  500 ms ]
    Philosopher 1 [C:-1]  THINKING [  375 ms ]
    Philosopher 2 [P: 0]  THINKING [  575 ms ]
    Philosopher 3 [P: 1]   EATING  [  525 ms ]
    Philosopher 4 [P: 2]  THINKING [  800 ms ]
    Philosopher 5 [P: 3]   EATING  [  625 ms ]

    Demo Description
    ----------------
    An implementation of a solution to the Dining Philosophers
    problem (a classic multi-thread synchronization problem) using
    CMSIS RTOS V1 APIs. This particular implementation demonstrates the
    usage of multiple preemptible threads of differing
    priorities, as well as semaphores and thread sleeping.

Exit QEMU by pressing :kbd:`CTRL+A` :kbd:`x`.