Modified version oft the Dining Philosopher Problem - here any chopstick can be used, so the five chopstick are represented báy a counting semaphore initialized with a value of 5.

Dependencies:   mbed mbed-rtos

Files at this revision

API Documentation at this revision

Comitter:
cspista
Date:
Thu Mar 17 12:34:27 2022 +0000
Commit message:
Final version

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-rtos.lib Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu Mar 17 12:34:27 2022 +0000
@@ -0,0 +1,30 @@
+#include "mbed.h"
+#include "rtos.h"
+
+Semaphore s(5);                        // a pool of 5 chopsticks
+Timer mytime;
+
+void notify(const char* name) {
+    printf("%s acquired two chopsticks %8.1f\n\r", name,mytime.read());
+}
+
+void test_thread(void const* args) {
+    while (true) {
+        Thread::wait(1000+rand()%500); //Thinking   
+        s.wait();
+        s.wait();        
+        notify((const char*)args);
+        Thread::wait(500+rand()%500);  //Eating
+        s.release();
+        s.release();        
+    }
+}
+
+int main (void) {
+    mytime.start();
+    Thread t2(test_thread, (void *)"Philosopher 2");
+    Thread t3(test_thread, (void *)"Philosopher 3");
+    Thread t4(test_thread, (void *)"Philosopher 4");
+    Thread t5(test_thread, (void *)"Philosopher 5");    
+    test_thread((void *)"Philosopher 1");
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-rtos.lib	Thu Mar 17 12:34:27 2022 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/mbed_official/code/mbed-rtos/#5713cbbdb706
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Thu Mar 17 12:34:27 2022 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/mbed_official/code/mbed/builds/65be27845400
\ No newline at end of file