dining philosophers (1) philosophers eat/think eating needs 2 forks pick one fork at a time how to...
TRANSCRIPT
Dining Philosophers (1)
Philosophers eat/think Eating needs 2 forks Pick one fork at a time How to prevent deadlock
Dining Philosophers
A nonsolution to the dining philosophers problem
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?
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?
Dining Philosophers
Solution to dining philosophers problem (part 1)
Dining Philosophers
Solution to dining philosophers problem (part 2)
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) ;
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
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]) ;
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
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) ;
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) ;
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
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() !!!
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() !!!
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) ;
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) ;
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) ;
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]
P3Ready Queue
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) ;
P3Ready Queue