Deadlock is a situation where a set of processes are blocked because each process is holding a resource and waiting for another resource acquired by some other process.
Mutual Exclusion: One or more than one resource are non-sharable (Only one process can use at a time)
3. Deadlock
› Deadlock is a situation where a set of processes are blocked because each process
is holding a resource and waiting for another resource acquired by some other
process.
› Consider an example when two trains are coming toward each other on same track
and there is only one track, none of the trains can move once they are in front of
each other. Similar situation occurs in operating systems when there are two or
more processes hold some resources and wait for resources held by other(s). For
example, in the below diagram, Process 1 is holding Resource 1 and waiting for
resource 2 which is acquired by process 2, and process 2 is waiting for resource 1.
3
4. Deadlock Necessary
conditions
› Mutual Exclusion: One or more than one resource are non-sharable (Only one
process can use at a time)
› Hold and Wait: A process is holding at least one resource and waiting for resources.
› No Preemption: A resource cannot be taken from a process unless the process
releases the resource.
› Circular Wait: A set of processes are waiting for each other in circular form.
4
5. Deadlock handling strategies
› Deadlock Prevention: The system checks every transaction before it is executed to
make sure it doesn't lead the deadlock situations. Deadlocks can be prevented by
preventing at least one of the four required conditions:
– Mutual Exclusion: Resources shared such as read-only files do not lead to
deadlocks but resources, such as printers and tape drives, requires exclusive
access by a single process.
– Hold and Wait: In this condition processes must be prevented from holding one
or more resources while simultaneously waiting for one or more others.
– No Preemption: Preemption of process resource allocations can avoid the
condition of deadlocks, where ever possible.
– Circular Wait: Circular wait can be avoided if we number all resources, and
require that processes request resources only in strictly increasing(or decreasing)
order.
5
6. Deadlock handling strategies
› Deadlock Avoidance:
– The general idea behind deadlock avoidance is to prevent deadlocks from ever
happening, by preventing at least one of the above-mentioned conditions.
– This requires more information about each process, and tends to lead to low
device utilization.
– Deadlock avoidance is the simplest and most useful model that each process
declares the maximum number of resources of each type that it may need.
– The deadlock-avoidance algorithm helps you to dynamically assess the resource-
allocation state so that there can never be a circular-wait situation.
– Banker’s Algorithm is one of the strategy used to avoid deadlock.
6
7. Banker's Algorithm
› Banker's algorithm is a deadlock avoidance algorithm. It is named so because this
algorithm is used in banking systems to determine whether a loan can be granted or
not.
› Consider there are n account holders in a bank and the sum of the money in all of
their accounts is s. Every time a loan has to be granted by the bank, it subtracts the
loan amount from the total money the bank has. Then it checks if that difference is
greater than s. It is done because, only then, the bank would have enough money
even if all the n account holders draw all their money at once.
› Banker's algorithm works in a similar way in computers. Whenever a new process is
created, it must specify the maximum instances of each resource type that it needs,
exactly.
7
8. Banker's Algorithm
› Let us assume that there are n processes and m resource types. Some data structures
that are used to implement the banker's algorithm are:
– Available array : Available [j] = k
– Max matrix: Max [i][j] = k
– Allocation matrix: Allocation [i][j] = k
– Need matrix : Need[i][j] = Max [i][j] - Allocation [i][j]
› Banker’s Algorithm is further divided in two sub algorithms such as:
– Safety Algorithm: In order to apply the Banker's algorithm, we first need an
algorithm for determining whether or not a particular state is safe. This algorithm
determines if the current state of a system is safe or not.
– Resource Request Algorithm: This algorithm determines if a new request is safe,
and grants it only if it is safe to do so. When a request is made ( that does not
exceed currently available resources ), pretend it has been granted, and then see if
the resulting state is a safe one. If so, grant the request, and if not, deny the
request on basis of some conditions.
8
9. Banker's Algorithm› Example 1: Consider the given snapshot of the system. A system has 5 process and 3 types of
resources A, B, C.
9
A B C
P0 0 1 0
P1 2 0 0
P2 3 0 2
P3 2 1 1
P4 0 0 2
A B C
P0 7 5 0
P1 3 2 2
P2 9 0 2
P3 2 2 2
P4 4 3 3
Allocation Max
A B C
3 3 2
Available
Answer the following questions using Banker’s Algorithm:
Q1. What is the contents of Need Matrix?
Q2. Is the system in safe state?
Q3. If request from process P1 arrives as (1,0,2) can the request be granted immediately?
11. Banker's Algorithm
› Step 2: Safety Algorithm: Let Work and Finish be vectors of length m and n, respectively.
Initially,
Work = Available = {3, 3, 2}
Finish [i] = False (for i = 0, 1, 2, …., n – 1)
i.) Let i = 0, Finish [0] = F
Need (P0) ≤ Work
(7, 4, 0) ≤ (3, 3, 2) => False
11
Process P0 cannot be given resources. Hence, P0 has to wait.
ii.) Let i = 1, Finish [1] = F
Need (P1) ≤ Work
(1, 2, 2) ≤ (3, 3, 2) => True
12. Banker's Algorithm
12
Work = Work + Allocation (P1)
= (3, 3, 2) + (2, 0, 0)
Work = (5, 3, 2)
P1 releases resources after its execution.
Finish = {F, T, F, F, F}
Safe Sequence = {P1}
iii.) Let i = 2, Finish [2] = F
Need (P2) ≤ Work
(6, 0, 0) ≤ (5, 3, 2) => False
Process P2 cannot be given resources. Hence, P2 has to wait.
13. Banker's Algorithm
13
iv.) Let i = 3, Finish [3] = F
Need (P3) ≤ Work
(0, 1, 1) ≤ (5, 3, 2) => True
Work = Work + Allocation (P3)
= (5, 3, 2) + (2, 1, 1)
Work = (7, 4, 3)
P3 releases resources after its execution.
Finish = {F, T, F, T, F}
Safe Sequence = {P1,P3}
14. Banker's Algorithm
14
iv.) Let i = 4, Finish [4] = F
Need (P4) ≤ Work
(4, 3, 1) ≤ (7, 4, 3) => True
Work = Work + Allocation (P4)
= (7, 4, 3) + (0, 0, 2)
Work = (7, 4, 5)
P4 releases resources after its execution.
Finish = {F, T, F, T, T}
Safe Sequence = {P1,P3,P4}
15. Banker's Algorithm
15
vi.) Let i = 0, Finish [0] = F
Need (P0) ≤ Work
(7, 4, 3) ≤ (7, 4, 5) => True
Work = Work + Allocation (P0)
= (7, 4, 5) + (0, 1, 0)
Work = (7, 5, 5)
P0 releases resources after its execution.
Finish = {T, T, F, T, T}
Safe Sequence = {P1,P3,P4,P0}
16. Banker's Algorithm
16
vii.) Let i = 2, Finish [0] = F
Need (P2) ≤ Work
(6, 0, 0) ≤ (7, 5, 5) => True
Work = Work + Allocation (P0)
= (7, 5, 5) + (3, 0, 2)
Work = (10, 5, 7)
P2 releases resources after its execution.
Finish = {T, T, T, T, T}
Safe Sequence = {P1,P3,P4,P0,P2}
∴ Yes, The system is in safe state
17. Banker's Algorithm
17
Step 3: Resource Request Algorithm:
Request (P1) ≤ Need (P1)
(1, 0, 2) ≤ (1, 2, 2) => True
Request (P1) ≤ Available
(1, 0, 2) ≤ (3, 3, 2) => True
Then system pretends to fulfill request then modify resource allocation state as follows:
Available = Available – Request (P1)
= (3, 3, 2) – (1, 0, 2)
Available = (2, 3, 0)
19. Banker's Algorithm
› Example 1: Consider the given snapshot of the system. A system has 5 process and 4 types of
resources A, B, C, D. There are 3 instances of type A, 14 instances of type B, 12 instances of type
C and 12 instances of type D.
19
A B C D
P0 0 6 3 2
P1 0 0 1 2
P2 1 0 0 0
P3 1 3 5 4
P4 0 0 1 4
A B C D
P0 0 6 5 2
P1 0 0 1 2
P2 0 7 5 0
P3 2 3 5 6
P4 0 6 5 6
Allocation Max
Answer the following questions using Banker’s Algorithm:
Q1. What is the contents of Need Matrix?
Q2. Is the system in safe state?
Q3. If request from process P4 arrives as (0, 0, 4, 1) can the request be granted
immediately?
20. Banker's Algorithm
20
Solution:
Available = Total Resources - Total Allocation
= (3, 14, 12, 12) - (2, 9, 10, 12)
Available = (1, 5, 2, 0)
Solve the remaining as previous question.
21. Deadlock handling strategies
› Deadlock Detection:
– If deadlocks are not avoided, then another approach is to detect when they have
occurred and recover somehow.
– A deadlock occurrence can be detected by the resource scheduler. A resource
scheduler helps OS to keep track of all the resources which are allocated to
different processes. So, when a deadlock is detected, it can be resolved using
above two strategies.
– Deadlock detection can be done with the help of following technique:
› Resource allocation graph
› Wait – for – graph
21
22. Resource allocation graph
› If resource categories have only single instances of their resources, then deadlock
states can be detected by cycles in the resource-allocation graphs.
› In this case, unsafe states can be recognized and avoided by augmenting the resource-
allocation graph (RAG) with claim edges, which point from a process to a resource
that it may request in the future.
› In RAG vertices are two type –
– Process vertex – Every process will be represented as a process vertex. Generally,
the process will be represented with a circle.
– Resource vertex – Every resource will be represented as a resource vertex.
Generally, the resource will be represented with a rectangle. It is also two type –
› Single instance type resource – It represents as a box, inside the box, there will
be one dot. So the number of dots indicate how many instances are present of
each resource type.
› Multi-resource instance type resource – It also represents as a box, inside the
box, there will be many dots present.
22
23. Resource allocation graph
› There are two types of edges in RAG –
– Assign Edge – If you already assign a resource to a process then it is called Assign
edge.
– Request Edge – It means in future the process might want some resource to
complete the execution, that is called request edge.
23
24. Resource allocation graph
› Example 1 (Single instances RAG) – If there is a cycle in the Resource Allocation
Graph and each resource in the cycle provides only one instance, then the processes
will be in deadlock. For example, if process P1 holds resource R1, process P2 holds
resource R2 and process P1 is waiting for R2 and process P2 is waiting for R1, then
process P1 and process P2 will be in deadlock.
24
25. Resource allocation graph
› Example 2 (Multi-instances RAG) – From the given fig., it is not possible to say the
RAG is in a safe state or in an unsafe state. So to do that we use safe state algorithm.
25
26. Resource allocation graph
26
Example 1: Consider a system with 7 processes A through G and six types of resources
R through W with one resource for each type. Resource ownership is as follows
A holds R and wants S
B holds nothing but wants T
C holds nothing but wants S
D holds U and wants S and T
E holds T and wants V
F holds W and wants S
G holds V and wants U.
Is the system deadlocked, and if so, which processes are involved ?
27. Resource allocation graph
27
A holds R and wants S
B holds nothing but wants T
C holds nothing but wants S
D holds U and wants S and T
E holds T and wants V
F holds W and wants S
G holds V and wants U.
A
R
F
W
C
S
B
T
D
U
G E
V
28. Wait – for – graph
28
A
F
C
D
B
E
G
A holds R and wants S
B holds nothing but wants T
C holds nothing but wants S
D holds U and wants S and T
E holds T and wants V
F holds W and wants S
G holds V and wants U.
As cycle exists: D - > E -> G - > D
29. Resource allocation graph
29
Example 2: Consider the following sets P, R and E
P = {P1, P2, P3, P4}
R = {R1, R2, R3, R4}
E = {P1 -> R1 , P2 -> R3 , R1 -> P2, R3 - > P4, R2 - > P1, P4 - >R3}
Is the system deadlocked, and if so, which processes are involved ?
30. Deadlock
DEADLOCK
› The deadlock situation occurs when
one of the processes got blocked.
› Deadlock is an infinite process.
› Every Deadlock always has starvation.
› Deadlock happens then Mutual
exclusion, hold and wait. Here,
preemption and circular wait do not
occur simultaneously.
STARVATION
› Starvation is a situation where all the
low priority processes got blocked,
and the high priority processes
execute.
› Starvation is a long waiting but not an
infinite process.
› Every starvation doesn't necessarily
have a deadlock.
› It happens due to uncontrolled
priority and resource management.
30