Dining Philosopher Problem - in the easy way. We have five philosopher sitting around a table and they are either thinking or eating. There is rice on the table, but there are only five chopsticks - and one need two of them for eating. In this easy treatment ANY two of the five chpsticks can be used for eating - if we can acquire them. The five chopsticks are represented by a 5-slots semaphore (a semaphore counting from 0 to 5). The philosophers are represented as threads.

Dependencies:   mbed-rtos mbed

Committer:
icserny
Date:
Wed Feb 10 12:46:34 2016 +0000
Revision:
0:abf1becf9617
First version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
icserny 0:abf1becf9617 1 /** 10_rtos_dpp_easy
icserny 0:abf1becf9617 2 * Dining Philosopher Problem - in the easy way
icserny 0:abf1becf9617 3 * We have five philosopher sitting around a table
icserny 0:abf1becf9617 4 * and they are either thinking or eating.
icserny 0:abf1becf9617 5 * There is rice on the table, but there are only
icserny 0:abf1becf9617 6 * five chopsticks - and one need two of them for eating.
icserny 0:abf1becf9617 7 *
icserny 0:abf1becf9617 8 * In this easy treatment ANY two of the five chpsticks
icserny 0:abf1becf9617 9 * can be used for eating - if we can acquire them.
icserny 0:abf1becf9617 10 * The five chopsticks are represneted by a 5-slots
icserny 0:abf1becf9617 11 * semaphore (a semaphore counting from 0 to 5).
icserny 0:abf1becf9617 12 * The philosophers are represented as threads.
icserny 0:abf1becf9617 13 *
icserny 0:abf1becf9617 14 * Hardware requirements:
icserny 0:abf1becf9617 15 * - FRDM-KL25Z board
icserny 0:abf1becf9617 16 */
icserny 0:abf1becf9617 17
icserny 0:abf1becf9617 18 #include "mbed.h"
icserny 0:abf1becf9617 19 #include "rtos.h"
icserny 0:abf1becf9617 20
icserny 0:abf1becf9617 21 Semaphore s(5); //a pool of 5 chopsticks
icserny 0:abf1becf9617 22 Mutex stdio_mutex; //Mutex is required only for Cortex-M0 as stdio access is unprotected in C microlib
icserny 0:abf1becf9617 23 Timer mytime;
icserny 0:abf1becf9617 24
icserny 0:abf1becf9617 25 void notify(const char* name) {
icserny 0:abf1becf9617 26 stdio_mutex.lock();
icserny 0:abf1becf9617 27 printf("%s acquired two chopsticks %8.1f\n\r", name,mytime.read());
icserny 0:abf1becf9617 28 stdio_mutex.unlock();
icserny 0:abf1becf9617 29 }
icserny 0:abf1becf9617 30
icserny 0:abf1becf9617 31 void test_thread(void const* args) {
icserny 0:abf1becf9617 32 while (true) {
icserny 0:abf1becf9617 33 Thread::wait(1000+rand()%500); //Thinking
icserny 0:abf1becf9617 34 s.wait();
icserny 0:abf1becf9617 35 s.wait();
icserny 0:abf1becf9617 36 notify((const char*)args);
icserny 0:abf1becf9617 37 Thread::wait(500+rand()%500); //Eating
icserny 0:abf1becf9617 38 s.release();
icserny 0:abf1becf9617 39 s.release();
icserny 0:abf1becf9617 40 }
icserny 0:abf1becf9617 41 }
icserny 0:abf1becf9617 42
icserny 0:abf1becf9617 43 int main (void)
icserny 0:abf1becf9617 44 {
icserny 0:abf1becf9617 45 mytime.start();
icserny 0:abf1becf9617 46 Thread t2(test_thread, (void *)"Philosopher 2");
icserny 0:abf1becf9617 47 Thread t3(test_thread, (void *)"Philosopher 3");
icserny 0:abf1becf9617 48 Thread t4(test_thread, (void *)"Philosopher 4");
icserny 0:abf1becf9617 49 Thread t5(test_thread, (void *)"Philosopher 5");
icserny 0:abf1becf9617 50 test_thread((void *)"Philosopher 1");
icserny 0:abf1becf9617 51 }