Professional Documents
Culture Documents
08 Deadlock
08 Deadlock
Deadlock
Deadlock Questions?
• What is a deadlock?
• What causes a deadlock?
• How do you deal with (potential) deadlocks?
A += B; A += B;
unlock(b) unlock(a);
A += 30 B += 30
unlock(a) unlock(b);
Thread Maria
Tucker gets lock b Tucker waits lock b
Thread Tucker
A += B; A += B;
unlock(b) unlock(a);
A += 30 B += 30
unlock(a) unlock(b);
Maria Hybinette, UGA
Representing Deadlock
• Two common ways of representing deadlock:
– Vertices (circles or rectangles)
• threads (or processes) in system
• resources [types] (e.g., locks, semaphores, printers)
– Edges : indicates either:
• `waiting for’ or `wants’ (head of arrow on resource ‘square’) OR
• `held by’ (head of arrow on thread – ‘circle’)
• Examples …
Deadlock Prevention:
Mutual Exclusion
Mutual exclusion
Hold and wait
No preemption
Circular wait
• No mutual exclusion
• --> Make access to resources sharable ;
lock(a); lock(b)
A += 10; B += 10;
unlock(a) Unlock(b);
lock(b); lock(a);
B += 20; A += 20;
unlock(b) unlock(a);
lock(a) lock(b)
A += 30 B += 30
Maria Hybinette, UGA unlock(a) unlock(b);
Deadlock Prevention
Hold and Wait
Mutual exclusion
Hold and wait
• Two Approaches: No preemption
Circular wait
2. Atomically acquire all resources at once
(all or none)
» Example: Single lock to protect all (other
variations - e.g., release access to one
variable earlier)
lock(AB); lock(AB)
A += 10; B += 10;
B += 20; A += 20;
A += 30 B += 30
unlock(AB) unlock(AB);
Deadlock Prevention
No Preemption
Mutual exclusion
• Two Approaches:
1. Preempt requestors resource Hold and wait
• Example: B is holding some resources and then No preemption
requests additional resources that are held by Circular wait
other threads, then B releases all its resources
(and start over)
2. Preempt holders resource
• Example: A waiting for something held by B, then take
resource away from B and give them to A (B starts
over).
• Not possible if resource cannot be saved and
restored
– Can’t take away a lock without causing
problems
• Only works for some resources (e.g., CPU and
memory)
– May cause thrashing.
Deadlock Detection
Single Instance of Each Resource
Type
• Maintain a wait-for graph (it works on RAGS as well)
– Nodes are processes.
– Simplify: removes resource nodes and collapse edges
– Pi → Pj if Pi is waiting for Pj.
• Periodically invoke an algorithm (breath or depth first) that
searches for a cycle in the graph.
Resource Allocation
Wait For
Graphs (RAGs)
Deadlock detection
• Do a depth-first-search on the resource
allocation graph (RAG)
D, E, G ?
are deadlocked
A, C, F ?
are not deadlocked because S can
be allocated to either and then the
others can take turn to complete
Printers
P2
waiting
holding
P1 P3
CD-WR
holding waiting
P4
holding
Maria Hybinette, UGA
• Next create an algorithm with multiple instances,
and its data structures.
– Matrices and Vectors each column
• are numbers available of a particular kind or type, e.g., printers.
– Allocation Matrix
– Request Matrix
– Numbers in Existence Vector
– Numbers Available Vector
Detection algorithm
A marked process means it can run to completion
Have Need
Detection algorithm
Detection algorithm
2 2 2 0
2 2 2 0
Detection algorithm
2 2 2 0
4 2 2 1
2 2 2 0
4 2 2 1
No deadlock!
everything is marked
Maria Hybinette, UGA
Deadlock avoidance
Don’t allocate resource if it leads to deadlock
t1 t2 t3 t4 instruction
Process A
Process-resource trajectories
Requests Printer
Requests CD-RW
Releases Printer
Releases CD-RW
RP RC RLP RLC
t1 t2 t3 t4 instruction
Process A
tZ
tY
tX
tW
Process-resource trajectories
instruction
Releases CD-RW
Requests Printer
Releases Printer
Process B
RLP
tZ
RLC
tY Request CD-RW
RP
tX
RC
tW
RLP
tZ
RLC
tY
RP
tX
RC
tW
RP RC RLP RLC
t1 t2 t3 t4 instruction
Process A
Process-resource trajectories
instruction
Mutual
Exclusion
Process B
RLP
tZ
RLC Both processes
tY Request the 1 CD-RW
RP
tX
RC
tW
RP RC RLP RLC
t1 t2 t3 t4 instruction
Process A
RLP
tZ
RLC
tY
RP
tX
RC
tW
RP RC RLP RLC
t1 t2 t3 t4 instruction
Process A
Process-resource trajectories
instruction
Unsafe: Forbidden
Process B
RP RC RLP RLP
t1 t2 t3 t4 instruction
Process A
RLP
tZ
RLC
tY
RP
tX
RC
tW
RP RC RLP RLP
t1 t2 t3 t4 instruction
Process A
Trajectory showing
system progress
Process-resource trajectories
instruction
Process B
RLP
tZ B makes progress,
RLC A is not running
tY
RP
tX
RC
tW
RP RC RLP RLP
t1 t2 t3 t4 instruction
Process A
RLP
tZ
RLC
tY
RP
tX
RC
tW
RP RC RLP RLP
t1 t2 t3 t4 instruction
Process A
B requests
the CD-RW
Maria Hybinette, UGA
instructinons
Process-resource trajectories
Process B
RLP
tZ
RLC
tY
RP
tX Request is granted
RC
tW
RP RC RLP RLP
t1 t2 t3 t4 instructions
Process A
RLP
tZ
RLC
tY
RP
tX
RC
tW
RP RC RLP RLP
t1 t2 t3 t4 instructinons
Process A
Process-resource trajectories
instructions
Process B
RLP
tZ
RLC
tY
RP
tX
RC
tW
RP RC RLP RLP
t1 t2 t3 t4 instructions
Process A
Request is granted;
A proceeds
Maria Hybinette, UGA
Process-resource trajectories
instructions B runs & requests
the printer...
MUST WAIT!
Process B
RLP
tZ
RLC
tY
RP
tX
RC
tW
RP RC RLP RLP
t1 t2 t3 t4 instructions
Process A
Process-resource trajectories
instructions
RLP
tZ
RLC
tY
RP
tX
RC
tW
RP RC RLP RLP
t1 t2 t3 t4 instructions
Process A
RLP
tZ
RLC
B...
tY holds CD-RW
tX RP requests printer
RC
tW
RP RC RLP RLP
t1 t2 t3 t4 instructions
Process A
Process-resource trajectories
instructinos
A...
holds printer
requests CD-RW
Process B
RLP
tZ
RLC
B...
tY holds CD-RW
tX RP requests printer
RC
tW
DEADLOCK!
RP RC RLP RLP
t1 t2 t3 t4 instructions
Process A
RLP
tZ
RLC
Should the OS
tY give A the printer,
tX RP or make it wait???
RC
tW
RP RC RLP RLP
t1 t2 t3 t4 instructions
Process A
Process-resource trajectories
instructions
Process B
RLP
tZ
RLC
tY
RP
tX
RC
tW
RP RC RLP RLP
t1 t2 t3 t4 instructions
Process A
RP RC RLP RLP
t1 t2 t3 t4 instructions
Process A
Process-resource trajectories
instructions
Process B
RLP
tZ
RLC
tY
RP
tX
RC
tW
B requests the printer,
RP RC RLP RLP
t1 t2 t3 t4
B releases CD-RW, time
B releases printer,
Process A then A runs to completion!
• A “safe” state:
– No deadlock, and
– There is some scheduling order in which every process can run to
completion even if all of them request their maximum number of units
immediately
Deadlock Avoidance
• Dijkstra’s Banker’s Algorithm
6
2
5
3
Safe states
6
2
5
3
Maria Hybinette, UGA
Unsafe/Safe state?
The difference here is A
possesses 1 more resource
10 total
5
6
2
2
5
5
3 2
Safe Unsafe!
Maximum # Needed
Avoidance modeling
Total resource vector Available resource vector
Avoidance algorithm
Avoidance algorithm
2 2 2 0
2 2 2 0
Avoidance algorithm
2 2 2 0
4 2 2 1