Chapter 5 - Concurrency Deadlock - For Class
Chapter 5 - Concurrency Deadlock - For Class
CHAPTER 5
CONCURRENCY:
DEADLOCK
2
DEADLOCK
RESOURCE-ALLOCATION GRAPH
• Process
• Pi requests instance of Rj Pi
Rj
• Pi is holding an instance of Rj Pi
Rj
7
DEADLOCK
P1 P2 P3
Wait-for graph
P2 P3
P1
Wait-for graph
RESOURCE-ALLOCATION GRAPH
AND WAIT-FOR GRAPH
• Mutual exclusion
• Hold-and-wait
CONDITIONS FOR DEADLOCK 16
THE APPROACHES ON DEALING WITH DEADLOCK
17
• Deadlock Prevention
• Design such a system where there is no chance of having a deadlock.
• Deadlock Avoidance
• Making the appropriate dynamic choices based on the current state
of resource allocation – safe state and unsafe state.
• Deadlock Detection & Recovery
• Detect the presence of deadlock and take action to recover
• Deadlock Ignorance (Ostrich algorithm used by Windows and
Linux)
• It is the most popular method and it acts as if no deadlock and the
user will restart.
• Handling deadlock is expensive - lot of codes need to be altered
which will decrease the performance of the OS.
18
DEADLOCK AVOIDANCE
DEADLOCK AVOIDANCE:
RESOURCE ALLOCATION DENIAL
• Referred to as the banker’s algorithm
• State of the system is the current allocation of resources to process
• Safe state is where there is at least one sequence that does not
result in deadlock
• Unsafe state is a state that is not safe for resource allocation (as it
brings to deadlock)
20
DEADLOCK DETECTION
DEADLOCK DETECTION
• Detection algorithm
• Recovery scheme
DEADLOCK DETECTION 35
Qik ≤ Wk for 1 ≤ k ≤ m
If true:
W k = Wk + Aik for 1 ≤ k ≤ m
If false:
Terminate
DEADLOCK DETECTION 36
DEADLOCK DETECTION – STEP 1 37
0 0 0 0 0
W = W + A(P3)
DEADLOCK DETECTION – STEP 4 40
Q(P1) <= W? No
Terminate algo.
Q(P2) <= W? No
DETECTION ALGORITHM
• Sequence <P0, P2, P3, P1, P4> will result in Finish[i] = true for all i.
43
W = Available = (0 0 0)
EXAMPLE OF DETECTION ALGORITHM 44
W = Available = (0 0 0)
Q(P0) <= W Mark P0, W = (0 0 0) + (0 1 0) = (0 1 0)
EXAMPLE OF DETECTION ALGORITHM 45
W = Available = (0 0 0)
Q(P0) <= W Mark P0, W = (0 0 0) + (0 1 0) = (0 1 0)
Q(P2) <= W Mark P2, W = (0 1 0) + (3 0 3) = (3 1 3)
EXAMPLE OF DETECTION ALGORITHM 46
W = Available = (0 0 0)
Q(P0) <= W Mark P0, W = (0 0 0) + (0 1 0) = (0 1 0)
Q(P2) <= W Mark P2, W = (0 1 0) + (3 0 3) = (3 1 3)
Q(P3) <= W Mark P3, W = (3 1 3) + (2 1 1) = (5 2 4)
EXAMPLE OF DETECTION ALGORITHM 47
W = Available = (0 0 0)
Q(P0) <= W Mark P0, W = (0 0 0) + (0 1 0) = (0 1 0)
Q(P2) <= W Mark P2, W = (0 1 0) + (3 0 3) = (3 1 3)
Q(P3) <= W Mark P3, W = (3 1 3) + (2 1 1) = (5 2 4)
Q(P1) <= W Mark P1, W = (5 2 4) + (2 0 0) = (7 2 4)
EXAMPLE OF DETECTION ALGORITHM 48
W = Available = (0 0 0)
Q(P0) <= W Mark P0, W = (0 0 0) + (0 1 0) = (0 1 0)
Q(P2) <= W Mark P2, W = (0 1 0) + (3 0 3) = (3 1 3)
Q(P3) <= W Mark P3, W = (3 1 3) + (2 1 1) = (5 2 4)
Q(P1) <= W Mark P1, W = (5 2 4) + (2 0 0) = (7 2 4)
Q(P4) <= W Mark P4, W = (7 2 4) + (0 0 2) = (7 2 6)
=> No deadlock
EXAMPLE (CONT.) 49
W = Available = (0 0 0)
Mark P0, W = (0 0 0) + (0 1 0) = (0 1 0)
Þ Not enough to satisfy any other request
Þ Deadlock
51
DEADLOCK RECOVERY
DEADLOCK HANDLING
APPROACHES
• Prevention
• do not allow deadlock at all (by eliminating any one of
the conditions leading to deadlock)
• Avoidance
• deadlock may occur, but avoid it (using deadlock
avoidance algo). Need to know future resource demands.
• Detection
• allow deadlock to occur, then detect it, and perform
deadlock recovery.
EXERCISE 53
P1 5 2 5 2 P1 3 1 2 1
P2 2 3 1 6 P2 2 1 0 3
P3 1 4 2 4 P3 1 3 1 2
P4 3 6 6 5 P4 1 4 3 2