Presentation is loading. Please wait.

Presentation is loading. Please wait.

© 2004, D. J. Foreman 1 The Dining Philosophers Problem.

Similar presentations


Presentation on theme: "© 2004, D. J. Foreman 1 The Dining Philosophers Problem."— Presentation transcript:

1 © 2004, D. J. Foreman 1 The Dining Philosophers Problem

2 © 2004, D. J. Foreman 2 The Dining Philosophers Philosopher (int i) { while (TRUE) {// Think (wait for spoons) then Eat P (spoon[i]); // test left spoon P (spoon[(i+1) mod 5]); // test right spoon eat(); V (spoon[(i+1) mod 5]); V (spoon[i]); } semaphore spoon[5] = (1,1,1,1,1); // create philosopher processes fork (philosopher, 1, 0); fork (philosopher, 1, 1); fork (philosopher, 1, 2); fork (philosopher, 1, 3); fork (philosopher, 1, 4);

3 © 2004, D. J. Foreman 3 One Solution philosopher (int i) { while (TRUE) {// Think (wait for spoons) then Eat j = i % 2; P (spoon [(i+j) mod 5]); P (spoon [(i+1-j) mod 5]); eat(); V (spoon [(i+1-j) mod 5]); V (spoon [(i+j) mod 5]); } semaphore spoon[5] = (1,1,1,1,1); spoon (philosopher, 1, 0); spoon (philosopher, 1, 1); spoon (philosopher, 1, 2); spoon (philosopher, 1, 3); spoon (philosopher, 1, 4);

4 © 2004, D. J. Foreman 4 AND Synchronization  For n resources: R n  Some P i ->R j, maybe >1 in one critsec  order of P operations may cause deadlock  Using semaphores S i  P simultaneous (S 1, …, S n )

5 © 2004, D. J. Foreman 5 Simultaneous Semaphores P_sim (semaphore S, int N) {L1: if ( (S[0]>=1)&& … &&(S[N-1]>=1) ) { for (i=0; i<N; i++) S[i]--; } else { Enqueue the calling thread in the queue for the first S[i] where S[i]<1; The calling thread is blocked while it is in the queue; // When the thread is removed from the queue Goto L1; // this is an algorithm, not code! } V_sim (semaphore S, int N) {for (i=0; i<N; i++) {S[i]++; Dequeue all threads in the queue for S[i]; All such threads are now ready to run (but may be blocked again in Psimultaneous ); } else {} }

6 © 2004, D. J. Foreman 6 Dining Philosophers Re-visited Philosopher (int i) {while (TRUE) { // Think Psim (spoon[i], spoon [(i+1) mod 5]); eat(); Vsim (spoon[i], spoon [(i+1) mod 5]); } semaphore fork[5] = (1,1,1,1,1); fork(philosopher, 1, 0); fork(philosopher, 1, 1); fork(philosopher, 1, 2); fork(philosopher, 1, 3); fork(philosopher, 1, 4);

7 © 2004, D. J. Foreman 7 Mutex solutions-1  First attempt: int turn = 0; // initial value for turn (the semaphore) /* process 0 *//* processes 1 */ {while(turn != 0) no-op;{while(turn != 1) no-op; /* critical section*/ /* critical section*/ turn = 1; turn = 0; }} Strict alternation between two processes via use of shared variable “turn” Mutual exclusion achieved Two problems:  Performance determined by least active (slowest) process  If one process crashes outside of the critical section, the other will wait forever for the CS.

8 © 2004, D. J. Foreman 8 Mutex solutions-2  Second attempt: /* process 0 *//* processes 1 */ {flag[0] = true;{flag[1] = true; while(flag[1]) no-op;while(flag[0]) no-op; /* critical section*/ /* critical section*/ flag[0] = false;flag[1] = false; }} Crash outside CS will not indefinitely block other process  Mutual exclusion achieved Q: Is Deadlock caused by race to set flag[i]?

9 © 2004, D. J. Foreman 9 Correct (s/w) solution  See Dekker’s Algorithm: ■ Combines using the “turn” variable and flag[] variable. ■ Avoids mutual courtesy ■ “turn” guarantees mutual exclusion. ■ “flag[]” breaks strict alternation problem – if your flag is set to 1 and other flag is 0, then enter no matter what turn is. ■ In the event that a race causes flag[0] = flag[1] = 1, then there is no deadlock to get to the CS because turn will allow one of the processes to enter. ■ flag[i] is an indication of “intent” to enter.


Download ppt "© 2004, D. J. Foreman 1 The Dining Philosophers Problem."

Similar presentations


Ads by Google