Professional Documents
Culture Documents
05synchronization Cse130 Fa19 KV 10 Review
05synchronization Cse130 Fa19 KV 10 Review
Miller
Progress
of T2
Infeasible
Region
Progress of T1
Using Resource 1
Two Resources
Progress
of T2
Using R2
Infeasible
Region
Using R1
Infeasible
Region
Progress of T1
Using R1
Using R2
Two Resources
Progress
of T2
Infeasible
Region
Using R1
Progress of T1
Using R1
Using R2
Two Resources
Progress
of T2
Using R2
Infeasible
Joint
Region
Using R1
Progress
Path
Progress of T1
Using R1
Using R2
Unsafe Region
Progress
of T2 ❖ Why is this a problem?
‣ T1 has R2
‣ T2 has R1
‣ Both will want R1 & R2
Using R2
Progress
Unsafe
Path
Region
deadlock
Progress of T1
Using R1
Using R2
Progress
of T2
‣ Tasks claim exclusive control
over a resource
• Mutual Exclusion
‣ Resource cannot be taken away
• Nonpreemption
‣ Tasks hold resources while
Using R2
Infeasible
Region waiting
Joint
• Hold and Wait
Using R1
Progress
Unsafe
Path
Region
Progress of T1
Using R1
Using R2
Progress
of T2
‣ Tasks claim exclusive control
over a resource
• Mutual Exclusion
‣ Resource cannot be taken away
• Nonpreemption
‣ Tasks hold resources while
Using R2
Infeasible
Region waiting
Joint
• Hold and Wait
Using R1
Progress
Unsafe
Path
Region
Progress of T1
Using R1
Using R2
Dining Philosophers
❖ N philosophers around a table
‣ All are hungry
‣ All like to think
❖ N chopsticks available
‣ 1 between each pair of philosophers
❖ Philosophers need two chopsticks to
eat
❖ Philosophers alternate between eating
and thinking
❖ Goal: coordinate use of chopsticks
‣ Deadlock // eat
chopstick[i].up();
‣ Fairness chopstick[(i+1)%n].up();
// think
}
Dining Philosophers
❖ All philosophers grab the chopstick on
their right and wait for the chopstick
on their left to be released
Dining Philosophers
❖ All philosophers grab the chopstick on
their right and wait for the chopstick
on their left to be released
❖ How to detect deadlock?
‣ Use a graph
• Vertex per resource (chopstick)
• Directed edge per task
- “Tail” indicates held resource
- “Arrowhead” indicates
requested resource Not Task → Resource Edge
CSE 130: Principles of Computer System Design (Fall 2019) 11
© 2019 Ethan L. Miller
‣ Use a graph
• Vertex per resource (chopstick)
• Directed edge per task
- “Tail” indicates held resource
No Cycle – No Deadlock
❖ All philosophers grab the chopstick
with the lowest index
❖ How to detect deadlock? 1
0
‣ Use a graph
• Vertex per resource (chopstick) 2
• Directed edge per task
5
- “Tail” indicates held resource
4 3
Illegal Graph
❖ All philosophers grab the chopstick
with the lowest index
❖ How to detect deadlock? 1
0
‣ Use a graph
• Vertex per resource (chopstick) 2
• Directed edge per task
5
- “Tail” indicates held resource
4 3
A
B
DEADLOCK!
What is a deadlock?
❖ Formal de nition:
“A set of processes is deadlocked if each process in the set is
waiting for an event that only another process in the set can cause.”
R 1 2
Process Holds Wants
1 R S
2 T 3 S 4 T 5
3 S
4 U S,T
6 U V
5 T V
6 W S
7 V U W 7
Preventing deadlock
❖ Deadlock can be completely prevented!
❖ Ensure that at least one of the conditions for deadlock never occurs
‣ Mutual exclusion
‣ Circular wait
‣ Hold & wait
‣ No preemption
❖ Not always possible…
Livelock
❖ Sometimes, processes can still run, but not make progress
❖ Example: two processes want to use resources A and B
‣ P0 gets A, P1 gets B
‣ Each realizes that a deadlock will occur if they proceed as planned!
‣ P0 drops A, P1 drops B
‣ P0 gets B, P1 gets A
‣ Same problem as before
‣ This can go on for a very long time...
❖ Real-world example: Ethernet transmission collisions
‣ If there’s a “collision” on the wire, wait and try again
‣ Multiple processes waited the exact same amount of time...
numerical order
‣ Need not acquire them all at once!
❖ Circular wait is prevented C
Condition Prevented by