Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Philosophers
o think
o take forks (one at a time)
o eat
o put forks (one at a time)
Eating requires 2 forks
Pick one fork at a time
How to prevent deadlock?
What about starvation?
What about concurrency?
The solution
o A philosopher first
gets
o only then it tries to
take the 2 forks.
Slide Systems,
Operating taken from a presentation
2011, by Gadi
Danny Hendler Taubenfeld,
& Amnon IDC
Meisels 5
Dining philosophers: textbook solution code
#define N 5
#define LEFT (i-1) % N
#define RIGHT (i+1) % N
#define THINKING 0
#define HUNGRY 1
#define EATING 2
int state[N];
semaphore mutex = 1;
semaphore s[N]; // per each philosopher
void philosopher(int i) {
while(TRUE) {
think();
pick_sticks(i);
eat();
put_sticks(i);
}
}
void test(int i) {
if(state[i] == HUNGRY && state[LEFT] != EATING
&& state[RIGHT] != EATING) {
state[i] = EATING;
up(&s[i]); }
}
Eat
Block
Starvation!
Eat
Slide Systems,
Operating taken from a presentation
2011, by Gadi
Danny Hendler Taubenfeld,
& Amnon IDC
Meisels 8
Monitor-based implementation
monitor diningPhilosophers procedure test(i){
condition self[N]; if (state[LEFT] <> EATING &&
integer state[N]; state[RIGHT] <> EATING &&
procedure pick_sticks(i){ state[i] = HUNGRY)
state[i] := HUNGRY; then {
test(i); state[i] := EATING;
if state[i] <> EATING signal(self[i]);
then wait(self[i]); }
} }
procedure put_sticks(i){ for i := 0 to 4 do state[i] := THINKING;
state[i] := THINKING; end monitor
test(LEFT);
test(RIGHT);
An inefficient solution
o reduces to mutual exclusion
o not enough concurrency
o Starvation possible
Slide Systems,
Operating taken from a presentation
2011, by Gadi
Danny Hendler Taubenfeld,
& Amnon IDC
Meisels 11
Theorem: The LR solution is starvation-free
R 6 0 R
3 2
L 4 1
L
Slide Systems,
Operating taken from a presentation
2011, by Gadi
Danny Hendler Taubenfeld,
& Amnon IDC
Meisels 12
Deadlocks
Process B Deadlock
Process A holds
resource Q requests
resource Q F
A P
R S
Q B M
Operating Systems, 2011, Danny Hendler & Amnon Meisels 18
Different possible runs: an example
A B C
Request R Request S Request T
Request S Request T Request R
Release R Release S Release T
Release S Release T Release R
Round-robin
scheduling:
1. A requests R A B C
2. B requests S
3. C requests T
4. A requests S
5. B requests T R S T
6. C requests R
An alternative
scheduling:
1. A requests R A B C
2. C requests T
3. A requests S
4. C requests R
5. A releases R R S T
6. A releases S
Possible Strategies:
o Prevention
structurally negate one of the four necessary conditions
o Avoidance
allocate resources carefully, so as to avoid deadlocks
o Detection and recovery
o Do nothing (The “ostrich algorithm’’)
deadlocks are rare and hard to tackle... do nothing
Example: Unix - process table with 1000 entries and 100 processes
each requesting 20 FORK calls... Deadlock.
users prefer a rare deadlock over frequent refusal of FORK
Safe state:
Not deadlocked
There is a scheduling that satisfies all possible future
requests
l8
l7
Both have printer
l6
Printer Both have plotter
t
l5
Plotter r
Both have both
s
Unsafe state A
p q l1 l2 l3 l4
Printer
Plotter
Operating Systems, 2011, Danny Hendler & Amnon Meisels 30
Safe and Unsafe states (single resource)
Safe state:
o Not deadlocked
o There is a way to satisfy all possible future requests
1. Look for a row of R whose unmet resource needs are all smaller
than or equal to A. If no such row exists, the system will
eventually deadlock.
2. Otherwise, assume the process of the row chosen finishes
(which will eventually occur). Mark that process as terminated
and add the i’th row of C to the A vector
3. Repeat steps 1 and 2 until either all processes are marked
terminated, which means safe, or until a deadlock occurs,
which means unsafe.
T P S C T P S C
A 3 0 1 1 A 1 1 0 0
B 0 1 0 0 B 0 1 1 2
C= C 1 1 1 0 R= C 3 1 0 0
D 1 1 0 1 D 0 0 1 0
E 0 0 0 0 E 2 1 1 0
T P S C T P S C
A 3 0 1 1 A 1 1 0 0
B 0 1 0 0 B 0 1 1 2
C= C 1 1 1 0 R= C 3 1 0 0
D 0 0 0 0 D 0 0 1 0
E 0 0 0 0 E 2 1 1 0
T P S C T P S C
A 0 0 0 0 A 1 1 0 0
B 0 1 0 0 B 0 1 1 2
C= C 1 1 1 0 R= C 3 1 0 0
D 0 0 0 0 D 0 0 1 0
E 0 0 0 0 E 2 1 1 0
T P S C T P S C
A 3 0 1 1 A 1 1 0 0
B 0 1 0 0 B 0 1 1 2
C= C 1 1 1 0 R= C 3 1 0 0
D 1 1 0 1 D 0 0 1 0
E 0 0 0 0 E 2 1 1 0
T P S C T P S C
A 3 0 1 1 A 1 1 0 0
B 0 1 1 0 B 0 1 0 2
C= C 1 1 1 0 R= C 3 1 0 0
D 1 1 0 1 D 0 0 1 0
E 0 0 0 0 E 2 1 1 0
T P S C T P S C
A 3 0 1 1 A 1 1 0 0
B 0 1 1 0 B 0 1 0 2
C= C 1 1 1 0 R= C 3 1 0 0
D 1 1 0 1 D 0 0 1 0
E 0 0 1 0 E 2 1 0 0