1 / 20

Dining Philosophers (1)

Dining Philosophers (1). Philosophers eat/think Eating needs 2 forks Pick one fork at a time How to prevent deadlock . Dining Philosophers. A non solution to the dining philosophers problem. semaphore Forks[N] = {1,1,1,1,1} ; void take_fork(int i) { wait (Forks[i]) ; }

gyda
Download Presentation

Dining Philosophers (1)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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. 0 0 0 0 0 State Array S Array T T T T T Mutex = 1 NULL Mutex.queue P0: take_forks(0) down(mutex) ; test(0) ;

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

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

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

  11. 0 0 0 0 0 State Array S Array E T T T T Mutex = 1 NULL Mutex.queue 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. 0 0 1 0 0 State Array S Array E T E T T Mutex = 0 NULL 1 1 2 1 1 Mutex.queue P0: take_forks(0) down(mutex) ; test(0) ; down(S[0]) ; Preempted P2: take_forks(2) ; down(mutex) ; test(2) ;

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

  14. 0 0 0 0 0 State Array S Array E T E T T Mutex = 1 NULL 1 1 1 1 1 Mutex.queue P2: take_forks(2) ; down(mutex) ; test(2) ; up(mutex) ; down(S[2]) ; eat() !!! 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]) ;

  15. 0 0 0 -1 0 State Array S Array E T E H T Mutex = 1 NULL 1 1 1 0 1 Mutex.queue S[3].queue P2: take_forks(2) ; down(mutex) ; test(2) ; up(mutex) ; down(S[2]) ; eat() !!! 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

  16. 0 0 0 -1 0 State Array S Array E T E H T Mutex = 1 NULL 1 1 1 0 1 Mutex.queue S[3].queue P2: take_forks(2) ; down(mutex) ; test(2) ; up(mutex) ; down(S[2]) ; eat() !!! 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 P2: put_forks(2) ; down(mutex) ; State[2] = Thinking ; test(1) ;

  17. 0 0 0 -1 0 State Array S Array E T T H T Mutex = 0 NULL 1 1 1 0 1 Mutex.queue S[3].queue P2: take_forks(2) ; down(mutex) ; test(2) ; up(mutex) ; down(S[2]) ; eat() !!! 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 P2: put_forks(2) ; down(mutex) ; State[2] = Thinking ; test(1) ;

  18. 0 0 0 -1 0 State Array S Array E T T H T Mutex = 0 NULL 1 1 1 0 1 Mutex.queue S[3].queue P2: take_forks(2) ; down(mutex) ; test(2) ; up(mutex) ; down(S[2]) ; eat() !!! 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 P2: put_forks(2) ; down(mutex) ; State[2] = Thinking ; test(1) ; test(3) ;

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

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

More Related