Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: TextLCD mbed-rtos mbed
Fork of rtos_semaphore by
main.cpp
00001 // Hello World! for the TextLCD 00002 00003 #include "mbed.h" 00004 #include "Display.h" 00005 #include "rtos.h" 00006 00007 #define N 5 00008 #define LEFT (i+N-1)%N 00009 #define RIGHT (i+1)%N 00010 #define THINKING 0 00011 #define HUNGRY 0 00012 #define EATING 2 00013 int state[N]; 00014 Semaphore mutex(1); 00015 Semaphore* s[N]; 00016 00017 void take_forks(int i); 00018 void philosopher_thread(void const* arg); 00019 void put_forks(int i); 00020 void test(int i); 00021 00022 void eat(){ 00023 srand(time(NULL)); 00024 int r = rand()%3+2; 00025 Thread::wait(r*1000); 00026 } 00027 void think(){ 00028 srand(time(NULL)); 00029 int r = rand()%3+3; 00030 Thread::wait(r*1000); 00031 } 00032 00033 void philosopher_thread(void const* arg){ 00034 int i=*((int *)arg); 00035 while(1){ 00036 think(); 00037 take_forks(i); 00038 eat(); 00039 put_forks(i); 00040 } 00041 } 00042 00043 void take_forks(int i){ 00044 00045 } 00046 void put_forks(int i){ 00047 00048 } 00049 void test(int i){ 00050 00051 } 00052 int main() { 00053 00054 Display lcd; 00055 for(int i=0;i<N;i++){ 00056 s[i]= new Semaphore(0); 00057 } 00058 00059 00060 int a=1; 00061 int b=2; 00062 int c=3; 00063 int d=4; 00064 int e=5; 00065 00066 Thread t1(philosopher_thread, (void *)&a); 00067 Thread t2(philosopher_thread, (void *)&b); 00068 Thread t3(philosopher_thread, (void *)&c); 00069 Thread t4(philosopher_thread, (void *)&d); 00070 Thread t5(philosopher_thread, (void *)&e); 00071 00072 00073 00074 int i=0; 00075 while(1){ 00076 lcd.LCDEat(i); 00077 if(i>0){ 00078 lcd.LCDDoneEating(i-1); 00079 } 00080 else{lcd.LCDDoneEating(4);} 00081 i++; 00082 if(i==5){i=0;} 00083 wait(1); 00084 } 00085 00086 }
Generated on Fri Jul 15 2022 09:35:04 by
1.7.2
