Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dining Philosophers (1) Philosophers eat/think Eating needs 2 forks Pick one fork at a time How to prevent deadlock.

Similar presentations


Presentation on theme: "Dining Philosophers (1) Philosophers eat/think Eating needs 2 forks Pick one fork at a time How to prevent deadlock."— Presentation transcript:

1 Dining Philosophers (1) Philosophers eat/think Eating needs 2 forks Pick one fork at a time How to prevent deadlock

2 Dining Philosophers A nonsolution to the dining philosophers problem

3 semaphore Forks[N] = {1,1,1,1,1} ; void take_fork(int i) { wait (Forks[i]) ; } void put_fork(int i) { signal(Forks[i]) ; } Problems?

4 Approach 2: Semaphore Forks[N] = {1,1,1,1,1} ; Semaphore mutex = 1 ; while (1) { think(); wait(mutex) ; take_fork(i) ; take_fork((i+1) % N) ; eat() ; put_fork(i) ; put_fork((i+1) % N) ; signal(mutex) ; } Will this work?

5 Dining Philosophers Solution to dining philosophers problem (part 1)

6 Dining Philosophers Solution to dining philosophers problem (part 2)

7 NULL Mutex.queue T T T T T 0 0 0 0 0 State Array S Array Mutex = 1 P0: take_forks(0) down(mutex) ; test(0) ;

8 NULL Mutex.queue E T T T T 2 1 1 1 1 State Array S Array Mutex = 0 P0: take_forks(0) down(mutex) ; test(0) ; 1 0 0 0 0

9 NULL Mutex.queue E T T T T 1 0 0 0 0 State Array S Array Mutex = 0 P0: take_forks(0) down(mutex) ; test(0) ; up(mutex) ; down(S[0]) ;

10 NULL Mutex.queue E T T T T 0 0 0 0 0 State Array S Array Mutex = 1 P0: take_forks(0) down(mutex) ; test(0) ; up(mutex) ; down(S[0]) ; // falls through to //critical section Preempted

11 NULL Mutex.queue E T T T T 0 0 0 0 0 State Array S Array Mutex = 1 P0: take_forks(0) down(mutex) ; test(0) ; up(mutex) ; down(S[0]) ; // falls through to //critical section Preempted P2: take_forks(2) ; down(mutex) ; test(2) ;

12 NULL Mutex.queue E T E T T 1 1 2 1 1 State Array S Array Mutex = 0 P0: take_forks(0) down(mutex) ; test(0) ; down(S[0]) ; Preempted 0 0 1 0 0 P2: take_forks(2) ; down(mutex) ; test(2) ;

13 NULL Mutex.queue E T E T T 1 1 2 1 1 State Array S Array Mutex = 1 P0: take_forks(0) down(mutex) ; test(0) ; down(S[0]) ; Preempted 0 0 0 0 0 P2: take_forks(2) ; down(mutex) ; test(2) ; up(mutex) ; down(S[2]) ; //Falls through to critical section

14 NULL Mutex.queue E T E T T 1 1 1 1 1 State Array S Array Mutex = 1 P0: take_forks(0) down(mutex) ; test(0) ; down(S[0]) ; Preempted P3: take_forks(3) ; down(mutex) ; test(3) ; up(mutex) ; down(S[3]) ; 0 0 0 0 0 P2: take_forks(2) ; down(mutex) ; test(2) ; up(mutex) ; down(S[2]) ; eat() !!!

15 NULL Mutex.queue E T E H T 1 1 1 0 1 State Array S Array Mutex = 1 P0: take_forks(0) down(mutex) ; test(0) ; down(S[0]) ; P3: take_forks(3) ; down(mutex) ; test(3) ; up(mutex) ; down(S[3]) ; P3 S[3].queue 0 0 0 -1 0 P2: take_forks(2) ; down(mutex) ; test(2) ; up(mutex) ; down(S[2]) ; eat() !!!

16 NULL Mutex.queue E T E H T 1 1 1 0 1 State Array S Array Mutex = 1 P0: take_forks(0) down(mutex) ; test(0) ; down(S[0]) ; P3: take_forks(3) ; down(mutex) ; test(3) ; up(mutex) ; down(S[3]) ; P3 S[3].queue 0 0 0 -1 0 P2: take_forks(2) ; down(mutex) ; test(2) ; up(mutex) ; down(S[2]) ; eat() !!! P2: put_forks(2) ; down(mutex) ; State[2] = Thinking ; test(1) ;

17 NULL Mutex.queue E T T H T 1 1 1 0 1 State Array S Array Mutex = 0 P0: take_forks(0) down(mutex) ; test(0) ; down(S[0]) ; P3: take_forks(3) ; down(mutex) ; test(3) ; up(mutex) ; down(S[3]) ; P3 S[3].queue 0 0 0 -1 0 P2: take_forks(2) ; down(mutex) ; test(2) ; up(mutex) ; down(S[2]) ; eat() !!! P2: put_forks(2) ; down(mutex) ; State[2] = Thinking ; test(1) ;

18 NULL Mutex.queue E T T H T 1 1 1 0 1 State Array S Array Mutex = 0 P0: take_forks(0) down(mutex) ; test(0) ; down(S[0]) ; P3: take_forks(3) ; down(mutex) ; test(3) ; up(mutex) ; down(S[3]) ; P3 S[3].queue 0 0 0 -1 0 P2: take_forks(2) ; down(mutex) ; test(2) ; up(mutex) ; down(S[2]) ; eat() !!! P2: put_forks(2) ; down(mutex) ; State[2] = Thinking ; test(1) ; test(3) ;

19 NULL Mutex.queue E T T E T 1 1 1 0 1 State Array S Array Mutex = 0 P0: take_forks(0) down(mutex) ; test(0) ; down(S[0]) ; P3: take_forks(3) ; down(mutex) ; test(3) ; up(mutex) ; down(S[3]) ; 0 0 0 0 0 P2: take_forks(2) ; down(mutex) ; test(2) ; up(mutex) ; down(S[2]) ; eat() !!! P2: put_forks(2) ; down(mutex) ; State[2] = Thinking ; test(1) ; test(3) ; //performs a signal on S[3] P3 Ready Queue

20 NULL Mutex.queue E T T E T 1 1 1 0 1 State Array S Array Mutex = 1 P0: take_forks(0) down(mutex) ; test(0) ; down(S[0]) ; P3: take_forks(3) ; down(mutex) ; test(3) ; up(mutex) ; down(S[3]) ; 0 0 0 0 0 P2: take_forks(2) ; down(mutex) ; test(2) ; up(mutex) ; down(S[2]) ; eat() !!! P2: put_forks(2) ; down(mutex) ; State[2] = Thinking ; test(1) ; test(3) ; //performs a signal on S[3] up(mutex) ; P3 Ready Queue


Download ppt "Dining Philosophers (1) Philosophers eat/think Eating needs 2 forks Pick one fork at a time How to prevent deadlock."

Similar presentations


Ads by Google