0% found this document useful (0 votes)
12 views13 pages

OS Unit 4 Deadlock

Deadlock in operating systems occurs when two or more processes are unable to proceed because each is waiting for resources held by the other. The four Coffman conditions that lead to deadlock are mutual exclusion, hold and wait, no preemption, and circular wait. Prevention, avoidance, detection, and recovery strategies can be employed to manage deadlocks, including the use of resource allocation graphs and the Banker's algorithm.

Uploaded by

jaisingh2744
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views13 pages

OS Unit 4 Deadlock

Deadlock in operating systems occurs when two or more processes are unable to proceed because each is waiting for resources held by the other. The four Coffman conditions that lead to deadlock are mutual exclusion, hold and wait, no preemption, and circular wait. Prevention, avoidance, detection, and recovery strategies can be employed to manage deadlocks, including the use of resource allocation graphs and the Banker's algorithm.

Uploaded by

jaisingh2744
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Deadlock and its characterzitation

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.

Hold and Wait


A process can hold multiple resources and still request more resources from other
processes which are holding them. In the diagram given below, Process 2 holds
Resource 2 and Resource 3 and is requesting the Resource 1 which is held by
Process 1.
No Preemption
A resource cannot be preempted from a process by force. A process can only
release a resource voluntarily. In the diagram below, Process 2 cannot preempt
Resource 1 from Process 1. It will only be released when Process 1 relinquishes it
voluntarily after its execution is complete.

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.

● Aborting all the deadlocked processes.


● Abort one process at a time until the system recovers from the deadlock.
● Resource Preemption: Resources are taken one by one from a process and assigned to higher
priority processes until the deadlock is resolved.
Deadlock Avoidance
A deadlock avoidance policy grants a resource request only if it can establish that
granting the request cannot lead to a deadlock either immediately or in the future.
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.

Resource Allocation Graph (RAG)


A resource allocation graphs shows which resource is held by which process and which
process is waiting for a resource of a specific kind.

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.

Types of Vertices in RAG

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,

there will be many dots present.

How many Types of Edges are there in RAG?

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

complete the execution, that is called request edge.

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.

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:

1. It contains various resources that meet the requirements of each process.


2. 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.
3. It helps the operating system manage and control process requests for each type of resource in
the computer system.
4. The algorithm has a Max resource attribute that represents indicates each process can hold the
maximum number of resources in a system.

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:

Q.1: What will be the content of the Need matrix?

Need [i, j] = Max [i, j] – Allocation [i, j]

So, the content of Need Matrix is:


Q.2: Is the system in a safe state? If Yes, then what is the safe sequence?

Applying the Safety algorithm on the given system.

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.

The deadlock Detection Algorithm is of two types:

● Wait-for-Graph Algorithm (Single Instance)

● Banker’s Algorithm (Multiple Instance)

Wait-for-Graph Algorithm: It is a variant of the Resource Allocation graph. In this algorithm,


we only have processes as vertices in the graph. If the Wait-for-Graph contains a cycle then
we can say the system is in a Deadlock state. Now we will discuss how the Resource
Allocation graph will be converted into Wait-for-Graph in an Algorithmic Approach. We need
to remove resources while converting from Resource Allocation Graph to Wait-for-Graph.

● Resource Allocation Graph: Contains Processes and Resources.

● Wait-for-Graph: Contains only Processes after removing the Resources while

conversion from Resource Allocation Graph.

1. If resources have a single instance –


In this case for Deadlock detection, we can run an algorithm to check for the cycle in the
Resource Allocation Graph. The presence of a cycle in the graph is a sufficient condition for
deadlock.

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.

2. If there are multiple instances of resources –


Detection of the cycle is necessary but not a sufficient condition for deadlock detection, in this
case, the system may or may not be in deadlock varies according to different situations.

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.

1. Killing the process –

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

to break circular wait conditions.

2. Resource Preemption –

Resources are preempted from the processes involved in the deadlock, and

preempted resources are allocated to other processes so that there is a possibility of

recovering the system from the deadlock. In this case, the system goes into

starvation.

Methods of Handling Deadlocks in Operating System


The first two methods are used to ensure the system never enters a deadlock.

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 Detection and Recovery


We let the system fall into a deadlock and if it happens, we detect it using a detection algorithm and try to
recover.

Some ways of recovery are as follows.

● Aborting all the deadlocked processes.


● Abort one process at a time until the system recovers from the deadlock.
● Resource Preemption: Resources are taken one by one from a process and assigned to higher
priority processes until the deadlock is resolved.

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.

You might also like