Dining Philosophers (1) Philosophers eat/think Eating needs 2 forks Pick one fork at a time How to...
-
Upload
christal-gray -
Category
Documents
-
view
217 -
download
0
Transcript of Dining Philosophers (1) Philosophers eat/think Eating needs 2 forks Pick one fork at a time How to...
![Page 1: Dining Philosophers (1) Philosophers eat/think Eating needs 2 forks Pick one fork at a time How to prevent deadlock.](https://reader036.fdocuments.net/reader036/viewer/2022062519/5697c0211a28abf838cd2fb7/html5/thumbnails/1.jpg)
Dining Philosophers (1)
Philosophers eat/think Eating needs 2 forks Pick one fork at a time How to prevent deadlock
![Page 2: Dining Philosophers (1) Philosophers eat/think Eating needs 2 forks Pick one fork at a time How to prevent deadlock.](https://reader036.fdocuments.net/reader036/viewer/2022062519/5697c0211a28abf838cd2fb7/html5/thumbnails/2.jpg)
Dining Philosophers
A nonsolution to the dining philosophers problem
![Page 3: Dining Philosophers (1) Philosophers eat/think Eating needs 2 forks Pick one fork at a time How to prevent deadlock.](https://reader036.fdocuments.net/reader036/viewer/2022062519/5697c0211a28abf838cd2fb7/html5/thumbnails/3.jpg)
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?
![Page 4: Dining Philosophers (1) Philosophers eat/think Eating needs 2 forks Pick one fork at a time How to prevent deadlock.](https://reader036.fdocuments.net/reader036/viewer/2022062519/5697c0211a28abf838cd2fb7/html5/thumbnails/4.jpg)
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?
![Page 5: Dining Philosophers (1) Philosophers eat/think Eating needs 2 forks Pick one fork at a time How to prevent deadlock.](https://reader036.fdocuments.net/reader036/viewer/2022062519/5697c0211a28abf838cd2fb7/html5/thumbnails/5.jpg)
Dining Philosophers
Solution to dining philosophers problem (part 1)
![Page 6: Dining Philosophers (1) Philosophers eat/think Eating needs 2 forks Pick one fork at a time How to prevent deadlock.](https://reader036.fdocuments.net/reader036/viewer/2022062519/5697c0211a28abf838cd2fb7/html5/thumbnails/6.jpg)
Dining Philosophers
Solution to dining philosophers problem (part 2)
![Page 7: Dining Philosophers (1) Philosophers eat/think Eating needs 2 forks Pick one fork at a time How to prevent deadlock.](https://reader036.fdocuments.net/reader036/viewer/2022062519/5697c0211a28abf838cd2fb7/html5/thumbnails/7.jpg)
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) ;
![Page 8: Dining Philosophers (1) Philosophers eat/think Eating needs 2 forks Pick one fork at a time How to prevent deadlock.](https://reader036.fdocuments.net/reader036/viewer/2022062519/5697c0211a28abf838cd2fb7/html5/thumbnails/8.jpg)
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
![Page 9: Dining Philosophers (1) Philosophers eat/think Eating needs 2 forks Pick one fork at a time How to prevent deadlock.](https://reader036.fdocuments.net/reader036/viewer/2022062519/5697c0211a28abf838cd2fb7/html5/thumbnails/9.jpg)
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]) ;
![Page 10: Dining Philosophers (1) Philosophers eat/think Eating needs 2 forks Pick one fork at a time How to prevent deadlock.](https://reader036.fdocuments.net/reader036/viewer/2022062519/5697c0211a28abf838cd2fb7/html5/thumbnails/10.jpg)
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
![Page 11: Dining Philosophers (1) Philosophers eat/think Eating needs 2 forks Pick one fork at a time How to prevent deadlock.](https://reader036.fdocuments.net/reader036/viewer/2022062519/5697c0211a28abf838cd2fb7/html5/thumbnails/11.jpg)
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) ;
![Page 12: Dining Philosophers (1) Philosophers eat/think Eating needs 2 forks Pick one fork at a time How to prevent deadlock.](https://reader036.fdocuments.net/reader036/viewer/2022062519/5697c0211a28abf838cd2fb7/html5/thumbnails/12.jpg)
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) ;
![Page 13: Dining Philosophers (1) Philosophers eat/think Eating needs 2 forks Pick one fork at a time How to prevent deadlock.](https://reader036.fdocuments.net/reader036/viewer/2022062519/5697c0211a28abf838cd2fb7/html5/thumbnails/13.jpg)
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
![Page 14: Dining Philosophers (1) Philosophers eat/think Eating needs 2 forks Pick one fork at a time How to prevent deadlock.](https://reader036.fdocuments.net/reader036/viewer/2022062519/5697c0211a28abf838cd2fb7/html5/thumbnails/14.jpg)
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() !!!
![Page 15: Dining Philosophers (1) Philosophers eat/think Eating needs 2 forks Pick one fork at a time How to prevent deadlock.](https://reader036.fdocuments.net/reader036/viewer/2022062519/5697c0211a28abf838cd2fb7/html5/thumbnails/15.jpg)
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() !!!
![Page 16: Dining Philosophers (1) Philosophers eat/think Eating needs 2 forks Pick one fork at a time How to prevent deadlock.](https://reader036.fdocuments.net/reader036/viewer/2022062519/5697c0211a28abf838cd2fb7/html5/thumbnails/16.jpg)
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) ;
![Page 17: Dining Philosophers (1) Philosophers eat/think Eating needs 2 forks Pick one fork at a time How to prevent deadlock.](https://reader036.fdocuments.net/reader036/viewer/2022062519/5697c0211a28abf838cd2fb7/html5/thumbnails/17.jpg)
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) ;
![Page 18: Dining Philosophers (1) Philosophers eat/think Eating needs 2 forks Pick one fork at a time How to prevent deadlock.](https://reader036.fdocuments.net/reader036/viewer/2022062519/5697c0211a28abf838cd2fb7/html5/thumbnails/18.jpg)
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) ;
![Page 19: Dining Philosophers (1) Philosophers eat/think Eating needs 2 forks Pick one fork at a time How to prevent deadlock.](https://reader036.fdocuments.net/reader036/viewer/2022062519/5697c0211a28abf838cd2fb7/html5/thumbnails/19.jpg)
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
![Page 20: Dining Philosophers (1) Philosophers eat/think Eating needs 2 forks Pick one fork at a time How to prevent deadlock.](https://reader036.fdocuments.net/reader036/viewer/2022062519/5697c0211a28abf838cd2fb7/html5/thumbnails/20.jpg)
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