You are on page 1of 54

1

CHAPTER 5

CONCURRENCY:
DEADLOCK
2
DEADLOCK

• In an operating system, a deadlock occurs when a process or


thread enters a waiting state because a requested system resource
is held by another waiting process, which in turn is waiting for
another resource held by another waiting process

• A set of processes is deadlocked when each process in the set is


blocked awaiting an event that can only be triggered by another
blocked process in the set
3

DEADLOCK EXAMPLE - BRIDGE CROSSING

• Traffic only in one direction.


• Each section of a bridge can be viewed as a resource.
• If a deadlock occurs, it can be resolved if one car backs up (preempt
resources and rollback).
• Several cars may have to be backed up if a deadlock occurs.
• Starvation is possible.
4
DEADLOCK
5

RESOURCE ALLOCATION GRAPHS

• Directed graph that depicts a state of the system of


resources and processes
6

RESOURCE-ALLOCATION GRAPH

• Process

• Resource Type with 4 instances

• Pi requests instance of Rj Pi
Rj

• Pi is holding an instance of Rj Pi
Rj
7

RESOURCE ALLOCATION GRAPHS –


DEADLOCK/NO DEADLOCK?
RESOURCE ALLOCATION GRAPH WITH A 8

DEADLOCK

Resources in R2 are fully allocated to


P1 and P2. P3 request more from R2*.
Since no more unallocated resources, P3
must wait P1 and P2 release resources
of R2. But at the same time P1 is
waiting for R1 held by P2, and P2 waits
R3 held by P3, which R3 is not released
until P3 received R2*.

 Deadlock, since all processes


expecting each other to release
resources.
9

RESOURCE ALLOCATION GRAPH WITH A CYCLE BUT NO DEADLOCK

R1 allocated to P2 and P3. If P1


request R1, even in worst case
scenario at least P2 will release R1
for P1.

R2 allocated to P1 and P4. If P3


request R2, even in worst case
scenario at least P4 will release R2
for P3.
WAIT-FOR GRAPHS 10

• Simplified resource allocation graphs


• Only shows processes, no resources shown, i.e., which process is
waiting for which.
WAIT-FOR GRAPHS 11

P1 P2 P3

* P1 wait for P2, P2 wait for P3


(arrow points to the 'waited for' process)

Wait-for graph

Resource allocation graph


WAIT-FOR GRAPHS 12

P2 P3
P1

P1 wait for P2, P2 wait for P3,


P3 wait for P1 and P2

Wait-for graph

Resource allocation graph


13

RESOURCE-ALLOCATION GRAPH
AND WAIT-FOR GRAPH

(In class exercise)


Draw the resource allocation
graph
• P1 holds R2, and requests R1
• P2 holds R1, and requests R3, R4
& R5
• P3 holds R4, and requests R5
• P4 holds R5, and requests R2
• P5 holds R3
14

RESOURCE-ALLOCATION GRAPH AND WAIT-


FOR GRAPH

Resource-Allocation Graph Corresponding wait-for graph

What will be the wait-for graph?


15

CONDITIONS FOR DEADLOCK

• 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

• Do not start a process if its demands might lead to deadlock

• Do not grant an incremental resource request to a process if this


allocation might lead to deadlock
19

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

SAFE, UNSAFE , DEADLOCK STATE

• If a system is in safe state  no deadlocks.

• If a system is in unsafe state  possibility of deadlock.

• Avoidance  ensure that a system will never enter an unsafe


state.
21
22
23
24
25
26
27
28
29
30

Safe state with one of the sequences is P2, P1, P3


and P4
31
32
33

DEADLOCK DETECTION

• Deadlock detection do not limit resource access or


restrict process action. Resources are allocated
whenever demanded.

• Periodically, OS checks for circular wait condition


using some sort of algorithm for detection.
34

DEADLOCK DETECTION

• Allow system to enter deadlock state

• Detection algorithm

• Recovery scheme
DEADLOCK DETECTION 35

Algorithm using Request matrix (Q)


• The steps with the assumption that all processes are
marked initially indicating not deadlocked:
1. Mark each process for which all the elements are zeros in the allocation
matrix.
2. Initialize a matrix w equal to the Available matrix.
3. Recursively, find an index i, such that process i is currently unmarked and the
i-th row of Q is less than or equal to W meaning:

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

Mark P4, because no allocated resources


DEADLOCK DETECTION – STEP 2 38

0 0 0 0 0

Set W same as Available


DEADLOCK DETECTION – STEP 3 39

Q(P3) <= W, so mark P3

W = W + A(P3)
DEADLOCK DETECTION – STEP 4 40

Q(P1) <= W? No
Terminate algo.
Q(P2) <= W? No

P1 & P2 unmarked  deadlocked


DEADLOCK DETECTION 41

 Using wait-for graph


 For deadlock detection on single instance of a resource type,
wait-graph can be used.
 Detect cyclic graph – shows a deadlock has occurred
42

DETECTION ALGORITHM

For multiple instances of resource type.


• Five processes P0 through P4; three resource types
A (7 instances), B (2 instances), and C (6 instances).
• Snapshot at time T0:
Allocation Request Available
ABC ABC ABC
P0 0 1 0 000 000
P1 2 0 0 202
P2 3 0 3 000
P3 2 1 1 100
P4 0 0 2 002

• Sequence <P0, P2, P3, P1, P4> will result in Finish[i] = true for all i.
43

EXAMPLE OF DETECTION ALGORITHM

Allocation Request Available


ABC ABC ABC
P0 0 1 0 000 000
P1 2 0 0 202
P2 3 0 3 000
P3 2 1 1 100
P4 0 0 2 002

W = Available = (0 0 0)
EXAMPLE OF DETECTION ALGORITHM 44

Allocation Request Available


ABC ABC ABC
P0 0 1 0 000 000
P1 2 0 0 202
P2 3 0 3 000
P3 2 1 1 100
P4 0 0 2 002

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

Allocation Request Available


ABC ABC ABC
P0 0 1 0 000 000
P1 2 0 0 202
P2 3 0 3 000
P3 2 1 1 100
P4 0 0 2 002

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

Allocation Request Available


ABC ABC ABC
P0 0 1 0 000 000
P1 2 0 0 202
P2 3 0 3 000
P3 2 1 1 100
P4 0 0 2 002

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

Allocation Request Available


ABC ABC ABC
P0 0 1 0 0 0 0 0 0 0
P1 2 0 0 2 0 2
P2 3 0 3 0 0 0
P3 2 1 1 1 0 0
P4 0 0 2 0 0 2

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

Allocation Request Available


ABC ABC ABC
P0 0 1 0 0 0 0 0 0 0
P1 2 0 0 2 0 2
P2 3 0 3 0 0 0
P3 2 1 1 1 0 0
P4 0 0 2 0 0 2

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

 P2 requests an additional instance of type C.


Request
ABC
P0 0 0 0
P1 2 0 1
P2 0 0 1
P3 1 0 0
P4 0 0 2
 State of system?
 Can reclaim resources held by process P0, but insufficient resources
to fulfill other processes; requests.
 Deadlock exists, consisting of processes P1, P2, P3, and P4.
50

EXAMPLE OF DETECTION ALGORITHM


Allocation Request Available
ABC ABC ABC
P0 0 1 0 000 000
P1 2 0 0 202
P2 3 0 3 001
P3 2 1 1 100
P4 0 0 2 002

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

Strategies once deadlock is detected:


• Abort all deadlocked processes
• Back up each deadlocked process to some previously defined
checkpoint, and restart all process
• Successively abort deadlocked processes until deadlock no longer
exists
• Successively preempt resources until deadlock no longer exists
52

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

Claim Matrix (C) Allocation Matrix (A) Available Vector (V)


R1 R2 R3 R4 R1 R2 R3 R4 R1 R2 R3 R4
P0 4 2 1 2 P0 2 0 0 1 3 3 2 1

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

Given the information above. Find the answer


by using the banker’s algorithm
1. Resource Vector
2. Need Matrix
3. Is the system is safe? If yes find one of the
sequences.

You might also like