OS Unit 4 Deadlock
OS Unit 4 Deadlock
A deadlock happens in operating system when two or more processes need some
resource to complete their execution that is held by the other process.
A deadlock occurs if the four Coffman conditions hold true. But these conditions are
not mutually exclusive. They are given as follows −
Mutual Exclusion
There should be a resource that can only be held by one process at a time. In the
diagram below, there is a single instance of Resource 1 and it is held by Process 1
only.
Circular Wait
A process is waiting for the resource held by the second process, which is waiting
for the resource held by the third process and so on, till the last process is waiting
for a resource held by the first process. This forms a circular chain. For example:
Process 1 is allocated Resource2 and it is requesting Resource 1. Similarly, Process
2 is allocated Resource 1 and it is requesting Resource 2. This forms a circular wait
loop.
Deadlock Prevention
We can prevent a Deadlock by eliminating any of the above four conditions.
Eliminate Mutual Exclusion: It is not possible to dis-satisfy the mutual exclusion because
some resources, such as the tape drive and printer, are inherently non-shareable.
Eliminate Hold and wait: Allocate all required resources to the process before the start of its
execution, this way hold and wait condition is eliminated but it will lead to low device
utilization. for example, if a process requires a printer at a later time and we have allocated a
printer before the start of its execution printer will remain blocked till it has completed its
execution. The process will make a new request for resources after releasing the current set
of resources. This solution may lead to starvation.
Eliminate No Preemption : Preempt resources from the process when resources are required
by other high-priority processes.
Eliminate Circular Wait : Each resource will be assigned a numerical number. A process can
request the resources to increase/decrease. order of numbering. For Example, if the P1
process is allocated R5 resources, now next time if P1 asks for R4, R3 lesser than R5 such a
request will not be granted, only a request for resources more than R5 will be granted.
Detection and Recovery: Another approach to dealing with deadlocks is to detect and
recover from them when they occur. This can involve killing one or more of the processes
involved in the deadlock or releasing some of the resources they hold.
Some ways of recovery are as follows.
Therefore, it requires additional information such as how many resources of each type is required by a process.
If the system enters into an unsafe state, it has to take a step back to avoid deadlock.
So, resource allocation graph is explained to us what is the state of the system in terms of
processes and resources. Like how many resources are available, how many are allocated
and what is the request of each process. Everything can be represented in terms of the
diagram. One of the advantages of having a diagram is, sometimes it is possible to see a
deadlock directly by using RAG.
we can quickly spot a deadlock in the system by looking at the graph rather than the
tables we use in Banker’s algorithm. Deadlock avoidance can also be done with Banker’s
Algorithm.
So RAG also contains vertices and edges. In RAG vertices are two types
1. Process Vertex: Every process will be represented as a process vertex. Generally, the
process will be represented with a circle.
2. Resource Vertex: Every resource will be represented as a resource vertex. It is also two
types:
● 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,
Now coming to the edges of RAG.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
Here’s another example, that shows Processes P1 and P2 acquiring resources R1 and R2
while process P3 is waiting to acquire both resources. In this example, there is no deadlock
because there is no circular dependency. So cycle in single-instance resource type is the
sufficient condition for deadlock.
Example 2 (Multi-instances RAG)
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.
Banker's Algorithm in Operating System (OS)
It is a banker algorithm used to avoid deadlock and allocate resources safely to each process in the
computer system. 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. The banker's algorithm is named because it checks whether
a person should be sanctioned a loan amount or not to help the bank system safely simulate
allocation resources. In this section, we will learn the Banker's Algorithm in detail. Also, we will solve
problems based on the Banker's Algorithm. To understand the Banker's Algorithm first we will see a
real word example of it.
Suppose the number of account holders in a particular bank is 'n', and the total money in a bank is 'T'.
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.
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. Therefore, it is also known as
deadlock avoidance algorithm or deadlock detection in the operating system.
Advantages
Following are the essential characteristics of the Banker's algorithm:
Disadvantages
1. It requires a fixed number of processes, and no additional processes can be started in the
system while executing the process.
2. The algorithm does no longer allows the processes to exchange its maximum needs while
processing its tasks.
3. Each process has to know and state their maximum resource requirement in advance for the
system.
4. The number of resource requests can be granted in a finite time, but the time limit for allocating
the resources is one year.
Example:
Considering a system with five processes P0 through P4 and three resources of type A, B,
C. Resource type A has 10 instances, B has 5 instances and type C has 7 instances.
Suppose at time t0 following snapshot of the system has been taken:
Deadlock Detection : If deadlocks do occur, the operating system must detect and resolve
them. Deadlock detection algorithms, such as the Wait-For Graph, are used to identify
deadlocks, and recovery algorithms, such as the Rollback and Abort algorithm, are used to
resolve them. The recovery algorithm releases the resources held by one or more processes,
allowing the system to continue to make progress.
0 seconds of 0 secondsVolume 0%
In the above diagram, resource 1 and resource 2 have single instances. There is a cycle R1 →
P1 → R2 → P2. So, Deadlock is Confirmed.
Deadlock Recovery :
A traditional operating system such as Windows doesn’t deal with deadlock recovery as it is
a time and space-consuming process. Real-time operating systems use Deadlock recovery.
Killing all the processes involved in the deadlock. Killing process one by one. After
killing each process check for deadlock again and keep repeating the process till the
system recovers from deadlock. Killing all the processes one by one helps a system
2. Resource Preemption –
Resources are preempted from the processes involved in the deadlock, and
recovering the system from the deadlock. In this case, the system goes into
starvation.
Deadlock Prevention
This is done by restraining the ways a request can be made. Since deadlock occurs when all the above four
conditions are met, we try to prevent any one of them, thus preventing a deadlock.
Deadlock Avoidance
When a process requests a resource, the deadlock avoidance algorithm examines the resource-allocation state.
If allocating that resource sends the system into an unsafe state, the request is got granted.
Therefore, it requires additional information such as how many resources of each type is required by a process.
If the system enters into an unsafe state, it has to take a step back to avoid deadlock.
Deadlock Ignorance
In the method, the system assumes that deadlock never occurs. Since the problem of deadlock situation is not
frequent, some systems simply ignore it. Operating systems such as UNIX and Windows follow this approach.
However, if a deadlock occurs we can reboot our system and the deadlock is resolved automatically.