© 2004, d. j. foreman 1 the dining philosophers problem

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

Upload: leslie-dalton

Post on 16-Dec-2015

213 views

Category:

Documents


0 download

TRANSCRIPT

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

© 2004, D. J. Foreman 1

The Dining Philosophers Problem

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

© 2004, D. J. Foreman 2

The Dining PhilosophersPhilosopher (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 processesfork (philosopher, 1, 0); fork (philosopher, 1, 1);fork (philosopher, 1, 2);fork (philosopher, 1, 3);fork (philosopher, 1, 4);

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

© 2004, D. J. Foreman 3

One Solutionphilosopher (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);

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

© 2004, D. J. Foreman 4

AND SynchronizationFor n resources: Rn

Some Pi->Rj, maybe >1 in one critsec

order of P operations may cause deadlockUsing semaphores Si

Psimultaneous(S1, …, Sn)

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

© 2004, D. J. Foreman 5

Simultaneous SemaphoresP_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 {}}

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

© 2004, D. J. Foreman 6

Dining Philosophers Re-visitedPhilosopher (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);

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

© 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 achievedTwo 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.

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

© 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 achievedQ: Is Deadlock caused by race to set flag[i]?

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

© 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.