Dining Philosophers (1) - PowerPoint PPT Presentation

About This Presentation
Title:

Dining Philosophers (1)

Description:

Philosophers eat/think Eating needs 2 forks Pick one fork at a time How to prevent deadlock Dining Philosophers (1) Dining Philosophers A nonsolution to the dining ... – PowerPoint PPT presentation

Number of Views:249
Avg rating:3.0/5.0
Slides: 21
Provided by: philld
Category:

less

Transcript and Presenter's Notes

Title: Dining Philosophers (1)


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 ForksN 1,1,1,1,1 void
take_fork(int i) wait (Forksi) void
put_fork(int i) signal(Forksi)
Problems?
4
Approach 2 Semaphore ForksN 1,1,1,1,1
Semaphore mutex 1 while (1) think()
wait(mutex) take_fork(i)
take_fork((i1) N) eat()
put_fork(i) put_fork((i1) 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

State Array S Array
T T T T
T
Mutex 1
NULL
Mutex.queue
P0 take_forks(0) down(mutex)
test(0)
8

E T T T
T
State Array S Array
Mutex 0
NULL
2 1 1 1
1
Mutex.queue
P0 take_forks(0) down(mutex)
test(0)
9

E T T T
T
State Array S Array
Mutex 0
NULL
1 0 0 0
0
Mutex.queue
P0 take_forks(0) down(mutex)
test(0) up(mutex) down(S0)
10

E T T T
T
State Array S Array
Mutex 1
NULL
Mutex.queue
P0 take_forks(0) down(mutex)
test(0) up(mutex) down(S0) //
falls through to //critical
section Preempted
11

E T T T
T
State Array S Array
Mutex 1
NULL
Mutex.queue
P0 take_forks(0) down(mutex)
test(0) up(mutex) down(S0) //
falls through to //critical
section Preempted
P2 take_forks(2) down(mutex)
test(2)
12

E T E T
T
State Array S Array
Mutex 0
NULL
1 1 2 1
1
Mutex.queue
P0 take_forks(0) down(mutex)
test(0) down(S0) Preempted
P2 take_forks(2) down(mutex)
test(2)
13

E T E T
T
State Array S Array
Mutex 1
NULL
1 1 2 1
1
Mutex.queue
P0 take_forks(0) down(mutex)
test(0) down(S0) Preempted
P2 take_forks(2) down(mutex)
test(2) up(mutex) down(S2)
//Falls through to critical section
14

E T E T
T
State Array S Array
Mutex 1
NULL
1 1 1 1
1
Mutex.queue
P2 take_forks(2) down(mutex)
test(2) up(mutex) down(S2)
eat() !!!
P0 take_forks(0) down(mutex)
test(0) down(S0) Preempted
P3 take_forks(3) down(mutex) test(3)
up(mutex) down(S3)
15

E T E H
T
State Array S Array
Mutex 1
NULL
1 1 1 0
1
Mutex.queue
S3.queue
P2 take_forks(2) down(mutex)
test(2) up(mutex) down(S2)
eat() !!!
P0 take_forks(0) down(mutex)
test(0) down(S0)
P3 take_forks(3) down(mutex) test(3)
up(mutex) down(S3)
P3
16

E T E H
T
State Array S Array
Mutex 1
NULL
1 1 1 0
1
Mutex.queue
S3.queue
P2 take_forks(2) down(mutex)
test(2) up(mutex) down(S2)
eat() !!!
P0 take_forks(0) down(mutex)
test(0) down(S0)
P3 take_forks(3) down(mutex) test(3)
up(mutex) down(S3)
P3
P2 put_forks(2)
down(mutex) State2 Thinking
test(1)
17

E T T H
T
State Array S Array
Mutex 0
NULL
1 1 1 0
1
Mutex.queue
S3.queue
P2 take_forks(2) down(mutex)
test(2) up(mutex) down(S2)
eat() !!!
P0 take_forks(0) down(mutex)
test(0) down(S0)
P3 take_forks(3) down(mutex) test(3)
up(mutex) down(S3)
P3
P2 put_forks(2)
down(mutex) State2 Thinking
test(1)
18

E T T H
T
State Array S Array
Mutex 0
NULL
1 1 1 0
1
Mutex.queue
S3.queue
P2 take_forks(2) down(mutex)
test(2) up(mutex) down(S2)
eat() !!!
P0 take_forks(0) down(mutex)
test(0) down(S0)
P3 take_forks(3) down(mutex) test(3)
up(mutex) down(S3)
P3
P2 put_forks(2)
down(mutex) State2 Thinking
test(1) test(3)

19

E T T E
T
State Array S Array
Mutex 0
NULL
1 1 1 0
1
Mutex.queue
P2 take_forks(2) down(mutex)
test(2) up(mutex) down(S2)
eat() !!!
P0 take_forks(0) down(mutex)
test(0) down(S0)
P3 take_forks(3) down(mutex) test(3)
up(mutex) down(S3)
P2 put_forks(2)
down(mutex) State2 Thinking
test(1) test(3)
//performs a signal on S3
20

E T T E
T
State Array S Array
Mutex 1
NULL
1 1 1 0
1
Mutex.queue
P2 take_forks(2) down(mutex)
test(2) up(mutex) down(S2)
eat() !!!
P0 take_forks(0) down(mutex)
test(0) down(S0)
P3 take_forks(3) down(mutex) test(3)
up(mutex) down(S3)
P2 put_forks(2)
down(mutex) State2 Thinking
test(1) test(3)
//performs a signal on S3
up(mutex)
Write a Comment
User Comments (0)
About PowerShow.com