Question: Compile and run dine2.cpp with ^C . What is the maximum number of philosopers who can eat simultaneously? Add a delay statement like SDL_Delay (

 

Compile and run dine2.cpp with ^C. What is the maximum number of philosopers who can eat simultaneously? Add a delay statement like SDL_Delay ( rand() % 2000 ); right after the take_chops( l ) statement in the philosoper() function. Run the program for a longer time. What do you observe? You should have observed deadlock in running dine2. To avoid deadlock, you can use an array state to record the state of each philosopher, and one can pick up the chopsticks only if her neighbours are not eating.

Implement this mechanism and call program dine3.cpp. Repeat the above experiment to see whether dedlock occurs and what the maximum number of philosophers can dine simultaneously.

 /* dine2.cpp : mutexes lock chopsticks Compile: g++ -o dine2 dine2.cpp -lSDL Execute: ./dine2 */ #include  #include  #include  #include  #include  #include  #include  SDL_sem *chopLock[5]; //locks for chopsticks bool quit = false; int nEating = 0; // number of philosopers eating void think( int i ) { SDL_Delay ( rand() % 2000); } void eat( int i ) { printf(" Philosopher %d eating! ", i ); SDL_Delay ( rand() % 2000); } void take_chops( int i ) { printf(" Taking chopstick %d", i ); } void put_chops( int i ) { } int philosopher( void *data ) { int i, l, r; i = atoi ( (char *) data ); l = i; //left r = (i+1) % 5; while ( !quit ) { think( i ); printf(" Philosoper %d ", i ); SDL_SemWait ( chopLock[l] ); take_chops ( l ); // SDL_Delay ( rand() % 2000 ); //could lead to deadlock SDL_SemWait ( chopLock[r] ); take_chops ( r ); nEating++; eat ( i ); nEating--; put_chops ( r ); SDL_SemPost ( chopLock[r] ); put_chops ( l ); SDL_SemPost ( chopLock[l] ); } } void checkCount ( int sig ) { if ( sig == SIGINT ) printf(" %d philospers eating ", nEating ); else if ( sig == SIGQUIT ) { quit = true; printf(" Quitting, please wait.... "); for ( int i = 0; i < 5; i++ ) { // break any deadlock printf(" Unlocking %d ", i ); SDL_SemPost ( chopLock[i] ); printf(" Unlocking %d done", i ); } } } int main () { struct sigaction act, actq; act.sa_handler = checkCount; sigemptyset ( &act.sa_mask ); sigaction ( SIGINT, &act, 0 ); actq.sa_handler = checkCount; sigaction ( SIGQUIT, &actq, 0 ); SDL_Thread *p[5]; //thread identifiers const char *names[] = { "0", "1", "2", "3", "4" }; for ( int i = 0; i < 5; i++ ) chopLock[i] = SDL_CreateSemaphore( 1 ); for ( int i = 0; i < 5; i++ ) p[i] = SDL_CreateThread ( philosopher, (char *) names[i] ); for ( int i = 0; i < 5; i++ ) SDL_WaitThread ( p[i], NULL ); for ( int i = 0; i < 5; i++ ) SDL_DestroySemaphore ( chopLock[i] ); return 0; } 

Your progam may have code segments similar to the following:

#define LEFT (i - 1) % 5 #define RIGHT (i + 1) % 5 #define HUNGRY 0 #define EATING 1 #define THINKING 2 SDL_sem *s[5]; // one semaphore per philosopher bool quit = false; int nEating = 0; // number of philosopers eating SDL_mutex *mutex; int state[5]; //array keeping track of everyone's state void test ( int i ) { if ( state[i] == HUNGRY && state[LEFT] != EATING && state[RIGHT] != EATING ) { state[i] = EATING; SDL_SemPost ( s[i] ); } } void putdown ( int i ) { SDL_LockMutex ( mutex ); state[i] = THINKING; test ( LEFT ); test ( RIGHT ); SDL_UnlockMutex ( mutex ); } ....... 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!