OS Module3.1 Deadlocks
OS Module3.1 Deadlocks
Chapter 7: Deadlocks
The Deadlock Problem
System Model
Deadlock Characterization
Methods for Handling Deadlocks
Deadlock Prevention
Deadlock Avoidance
Deadlock Detection
Recovery from Deadlock
To develop a description of deadlocks, which prevent
sets of concurrent processes from completing their
tasks.
Consider an example when two trains are coming toward each other on the same
track and there is only one track, none of the trains can move once they are in front
of each other.
A similar situation occurs in operating systems when there are two or more
processes that hold some resources and wait for resources held by other(s).
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.
Note – Most OSes do not prevent or deal with deadlocks.
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.
Let us assume that there are three processes P1, P2 and P3. There are three
different resources R1, R2 and R3. R1 is assigned to P1, R2 is assigned to P2 and
R3 is assigned to P3.
After some time, P1 demands for R2 which is being used by P2. P1 halts its
execution since it can't complete without R2. P2 also demands for R3 which is being
used by P3. P2 also stops its execution because it can't continue without R3. P3
also demands for R1 which is being used by P1 therefore P3 also stops its
execution.
In this scenario, a cycle is being formed among the three processes. None of the
process is progressing and they are all waiting. The computer becomes
unresponsive since all the processes got blocked.
Deadlock happens only when Mutual Exclusion, hold and wait, No
preemption and circular wait holds simultaneously.
1.Mutual Exclusion
A resource can only be shared in mutually exclusive manner. It implies, if two
process cannot use the same resource at the same time.
3.No preemption
The process which once scheduled will be executed till the completion. No other
process can be scheduled by the scheduler meanwhile.
4.Circular Wait
All the processes must be waiting for the resources in a cyclic manner so that the
last process is waiting for the resource which is being held by the first process.
Strategies for handling Deadlock
1. Deadlock Ignorance
• Deadlock Ignorance is the most widely used approach among all the
mechanism.
• This is being used by many operating systems mainly for end user uses.
• In this approach, the Operating system assumes that deadlock never occurs.
• This approach is best suitable for a single end user system where User uses
the system only for browsing and all other normal stuff.
If it is possible to violate one of the four conditions at any time then the deadlock
can never occur in the system.
The process continues until the system is in safe state. Once the system moves
to unsafe state, the OS has to backtrack one step.
If it occurs then it applies some of the recovery methods to the system to get rid of
deadlock.
The resource allocation graph is converted into an allocation and request matrix to
apply a safety algorithm to the system.
This technique does not limit resources access or restrict process action.
◦ P = {P1, P2, …, Pn}, the set consisting of all the processes in the
system.
◦ R = {R1, R2, …, Rm}, the set consisting of all resource types in the
system.
Pi requests instance of Rj
Pi
Rj
Pi is holding an instance of Rj
Pi
Rj
If graph contains no cycles no deadlock
System is in safe state if there exists a sequence <P1, P2, …, Pn> of ALL the
processes is the systems such that for each Pi, the resources that Pi can still
request can be satisfied by currently available resources + resources held by all
the Pj, with j < i
That is:
◦ If Pi resource needs are not immediately available, then Pi can wait until all Pj
have finished.
From the above example, it is not possible to say the RAG is in a safe state or in
an unsafe state. So to see the state of this RAG, let’s construct the allocation
matrix and request matrix.
Multiple Instance RAG Example
•The total number of processes are three; P1, P2 & P3 and the total number of
resources are two; R1 & R2.
Allocation matrix –
•For constructing the allocation matrix, just go to the resources and see to which
process it is allocated.
Request matrix –
•In order to find out the request matrix, you have to go to the process and see the
outgoing edges.
So, there is no deadlock in this RAG. Even though there is a cycle, still there is no
deadlock. Therefore in multi-instance resource cycle is not sufficient condition for
deadlock.
Multiple Instance RAG Example
Multiple Instance RAG Example
Above example is the same as the previous example except that, the process P3 requesting
for resource R1.
So the table becomes as shown in below.
Multiple Instance RAG Example
So, the Available resource is = (0, 0), but requirement are (0, 1), (1, 0) and (1,
0).So you can’t fulfill any one requirement. Therefore, it is in deadlock.
So, in case of RAG with multi-instance resource type, the cycle is a necessary
condition for deadlock, but not sufficient.
Claim edge Pi → Rj indicated that process Pj may request
resource Rj; represented by a dashed line
• The 'S-State' examines all possible tests or activities before deciding whether the
allocation should be allowed to each process.
• It also helps the operating system to successfully share the resources between all
the processes.
• If an account holder applies for a loan; first, the bank subtracts the loan amount
from full cash and then estimates the cash difference is greater than T to approve
the loan amount.
• These steps are taken because if another person applies for a loan or withdraws
some amount from the bank, it helps the bank manage and operate all things
without any restriction in the functionality of the banking system.
• Similarly, it works in an operating system.
• When a new process is created in a computer system, the process must provide
all types of information to the operating system like upcoming processes, requests
for their resources, counting them, and delays.
• Based on these criteria, the operating system decides which process sequence
should be executed or waited so that no deadlock occurs in a system.
• Each process should provide information to the operating system for upcoming
resource requests, the number of resources, and how long the resources will be
held.
• It helps the operating system manage and control process requests for each
type of resource in the computer system.
• The algorithm has a Max resource attribute that represents indicates each
process can hold the maximum number of resources in a system.
Disadvantages
• The algorithm does no longer allows the processes to exchange its maximum
needs while processing its tasks.
• Each process has to know and state their maximum resource requirement in
advance for the system.
• The number of resource requests can be granted in a finite time, but the time
limit for allocating the resources is one year.
When working with a banker's algorithm, it requests to
know about three things:
How much each process can request for each resource in the system. It is denoted
by the [MAX] request.
Available: It is an array of length 'm' that defines each type of resource available in
the system. When Available[j] = K, means that 'K' instances of Resources type R[j]
are available in the system.
Max: It is a [n x m] matrix that indicates each process P[i] can store the maximum
number of resources R[j] (each type) in a system.
5 Pen drives
2 Printers
4 Scanners
3 Hard disks
Here, we have created a vector representing total resources:
Available = (5, 2, 4, 3).
Assume there are four processes. The available resources are already allocated as
per the matrix table below.
P 1 1 0 0
Q 0 1 1 2
R 2 1 0 0
S 0 0 1 0
Available=Available- Allocated
= (5, 2, 4, 3) -(4, 2, 2, 3)
=(1, 0, 2, 0)
Banker’s algorithm comprises of two algorithms:
1.Safety algorithm
2.Resource request algorithm
1. Safety algorithm
The safety algorithm is used to check the system state means whether the
system is in a safe state or not.
2. Check the availability status for each type of resources [i], such as:
(a) Finish [i] == false
(b) Needi Work
go to step 2
4. If Finish [i] == true for all i, then the system is in a safe state
Request = request vector for process Pi. If Requesti [j] = k then
process Pi wants k instances of resource type Rj
Question2. Is the system in a safe state? If Yes, then what is the safe
sequence?
Applying the Safety algorithm on the given system,
Question3. What will happen if process P1 requests one additional instance of resource
type A and two instances of resource type C?
We must determine whether this new system state is safe. To do so, we again
execute Safety algorithm on the above data structures.
Hence the new system state is safe, so we can immediately
grant the request for process P1 .
Example of Banker’s Algorithm
Consider the following snapshot of a system:
Available
Allocation Max
Processes A B C
A B C A B C
P0 1 1 2 4 3 3 2 1 0
P1 2 1 2 3 2 2
P2 4 0 1 9 0 2
P3 0 2 0 7 5 3
P4 1 1 2 1 1 2
1. Content of the need matrix can be calculated by using the below formula
Safe sequence:
1. For process P0, Need = (3, 2, 1) and
Available = (2, 1, 0)
Need ? Available = False
So, the system will move for the next process.
The system allocates all the needed resources to each process. So, we can say
that system is in a safe state.
For resource A: One resource is allocated to each P1 and P2. One resource is
available. Hence the total number of instances of A in the system is 3.
available resources will now be increased since the resources held by two are now
free.
Available vector:
From the remaining processes, i.e. (P1, P3, and P4) , we can observe that the
available resources are more than the needed resources.
Hence any of the processes can now be allocated the resources. Let P3 be
allocated the resources.
All the resources released will add up to the available resources vector.
available vector:
Only P1 and P4 are left now.
After allocating resources, the safe sequence and available vector will be as
follow:
Available vector:
At last we can comfortably allocate the resources to P1.
Available vector:
We can observe here that all five processes have been allocated the
resources without any disruption. Hence the system is said to be in a safe
state.
Resource Request Algorithm
Before we move to the algorithm part, let us discuss the importance of the resource
request algorithm. Consider the table below:
Suppose the process P1 makes a new request from the operating system for the
resources such that it requires 1 instance of resource A, 1 instance of resource B.
It does not require any other extra resource. These resources are the extra
resources sought by P1 and should not be considered as a completely new
request.
Actually, the operating system does not allocate resources to the P1 even if it is
available at first. There may be chance that deadlock may occur in a later phase.
The operating system will then try to first detect whether allocating the resources
will lead to the system in any unsafe state or not without actually allocating
resources to P1.
Request can only be granted ( resource can only be allocated to a process) if the
request for the instances are less than the need vector.
Also we can only allocate the resources to a process if the instances requested is
less than or equal to the the available resources.
If a request for instances more than the available instances is made, it would not be
possible to allocate the resources.
In simple term, the resource request algorithm as the name suggests determine
whether a request for instances of resources can be granted or not.
Deadlock Detection
• The OS doesn't apply any mechanism to avoid or prevent the deadlocks.
• Therefore the system considers that the deadlock will definitely occur.
• In order to get rid of deadlocks, The OS periodically checks the system for
any deadlock.
• In case, it finds any of the deadlock then the OS will recover the system using
some recovery techniques.
• The OS can detect the deadlocks with the help of Resource allocation
graph.
Deadlock Detection
Recovery From Deadlock
When a detection algorithm determines that a deadlock exists then there are
several available alternatives.
There one possibility and that is to inform the operator about the deadlock and let
him deal with this problem manually.
Another possibility is to let the system recover from the deadlock automatically.
These are two options that are mainly used to break the deadlock.
we will successively preempt some resources from processes and will give these
resources to some other processes until the deadlock cycle is broken and there is a
possibility that the system will recover from deadlock.
But there are chances that the system goes into starvation.
The operating system can rollback the system to the previous safe state. For this
purpose, OS needs to implement check pointing at every state.
The moment, we get into deadlock, we will rollback all the allocations to get into the
previous safe state.
Process Termination
In order to eliminate deadlock by aborting the process, we will use one of two methods
given below. In both methods, the system reclaims all resources that are allocated to
the terminated processes.
•Aborting all deadlocked Processes Clearly, this method is helpful in breaking the
cycle of deadlock, but this is an expensive approach.
This approach is not suggestable but can be used if the problem becomes very
serious.
If all the processes are killed then there may occur insufficiency in the system and all
processes will execute again from starting.
•Abort one process at a time until the elimination of the deadlock cycle This
method can be used but we have to decide which process to kill and this method
incurs considerable overhead. The process that has done the least amount of work is
killed by the Operating system firstly.
QUESTION BANK
1. What are deadlocks? What are its characteristics? Explain the necessary
conditions forits occurrence.
2. Explain the process of recovery from deadlock.
3. Describe RAG:
i) With deadlock
ii) With a cycle but no deadlock
4. What is Resource Allocation Graph (RAG)? Explain how RAG is very useful in
describing deadly embrace (dead lock ) by considering your own example.
5. With the help of a system model, explain a deadlock and explain the
necessary conditions that must hold simultaneously in a system for a deadlock
to occur.
6. Explain how deadlock can be prevented by considering four necessary
conditions cannot hold.
7. Using Banker's algorithm determines whether the system is in a safe state.
8. How is a system recovered from deadlock? Explain the different methods used
to recover from deadlock.
9. Explain deadlock detection with algorithm and example
10. Define the terms: safe state and safe sequence. Give an algorithm to find
whether or not a system is in a safe state.