Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
58 views28 pages

Deadlock Handling in Operating System

Deadlocks in operating systems occur when a set of blocked processes each hold a resource and wait for another resource held by another process, leading to a situation where none can proceed. The document outlines the necessary conditions for deadlocks, methods for handling them, and the differences between starvation and deadlock. It also discusses deadlock prevention, avoidance, detection, and recovery strategies, emphasizing the importance of managing resource allocation to prevent deadlock situations.

Uploaded by

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

Deadlock Handling in Operating System

Deadlocks in operating systems occur when a set of blocked processes each hold a resource and wait for another resource held by another process, leading to a situation where none can proceed. The document outlines the necessary conditions for deadlocks, methods for handling them, and the differences between starvation and deadlock. It also discusses deadlock prevention, avoidance, detection, and recovery strategies, emphasizing the importance of managing resource allocation to prevent deadlock situations.

Uploaded by

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

DeadLocks In Operating System

#Introduction to Deadlocks in
Operating System
Meaning: Deadlocks are a set of blocked processes
each holding a resource and waiting to acquire a
resource held by another process.

In the above figure, process T0 has resource1, it requires resource2 in order to


finish its execution. Similarly, process T1 has resource2 and it also needs to
acquire resource1 to finish its execution. In this way, T0 and T1 are in a deadlock
because each of them needs the resource of others to complete their execution but
neither of them is willing to give up their resources.

In General, a process must request a resource before using it and it must release the
resource after using it. And any process can request as many resources as it
requires in order to complete its designated task. And there is a condition that the
number of resources requested may not exceed the total number of resources
available in the system.

Basically in the Normal mode of Operation utilization of resources by a process is


in the following sequence:

1. Request:
Firstly, the process requests the resource. In a case, if the request cannot be
granted immediately(e.g: resource is being used by any other process), then
the requesting process must wait until it can acquire the resource.

2. Use:
The Process can operate on the resource ( e.g: if the resource is a printer then
in that case process can print on the printer).

3. Release:
The Process releases the resource.

Let us take a look at the differences between starvation and deadlock.

Starvation vs Deadlock

Starvation Deadlock
When all the low priority processes got Deadlock is a situation that occurs
blocked, while the high priority when one of the processes got blocked.
processes execute then this situation is
termed as Starvation.
Starvation is a long waiting but it is not Deadlock is an infinite process.
an infinite process.
It is not necessary that every starvation There is starvation in every deadlock.
is a deadlock.
Starvation is due to uncontrolled During deadlock, preemption and
priority and resource management. circular wait does not occur
simultaneously.

The occurrence of deadlock can be detected by the resource scheduler.


#Necessary Conditions for Occurring Deadlock Condition

The deadlock situation can only arise if all the following four conditions hold
simultaneously:

1.Mutual Exclusion

According to this condition, atleast one resource should be non-shareable (non-


shareable resources are those that can be used by one process at a time.)

2. Hold and wait

According to this condition, A process is holding atleast one resource and is


waiting for additional resources.

3. NO preemption

Resources cannot be taken from the process because resources can be released only
voluntarily by the process holding them.

4. Circular wait

In this condition, the set of processes are waiting for each other in the circular
form.

The above four conditions are not completely independent as the circular wait
condition implies the hold and wait condition. We emphasize on the fact that all
four conditions must hold for a deadlock.

#Methods For Handling Deadlocks

Methods that are used in order to handle the problem of deadlocks are as follows:

1. Ignoring the Deadlock

According to this method, it is assumed that deadlock would never occur. This
approach is used by many operating systems where they assume that deadlock will
never occur which means operating systems simply ignores the deadlock. This
approach can be beneficial for those systems that are only used for browsing and
for normal tasks. Thus ignoring the deadlock method can be useful in many cases
but it is not perfect in order to remove the deadlock from the operating system.

2.Deadlock Prevention

As we have discussed in the above section, that all four conditions: Mutual
Exclusion, Hold and Wait, No preemption, and circular wait if held by a system
then causes deadlock to occur. The main aim of the deadlock prevention method is
to violate any one condition among the four; because if any of one condition is
violated then the problem of deadlock will never occur. As the idea behind this
method is simple but the difficulty can occur during the physical implementation
of this method in the system.

3.Avoiding the Deadlock

This method is used by the operating system in order to check whether the system
is in a safe state or in an unsafe state. This method checks every step performed by
the operating system. Any process continues its execution until the system is in a
safe state. Once the system enters into an unsafe state, the operating system has to
take a step back.

Basically, with the help of this method, the operating system keeps an eye on each
allocation, and make sure that allocation does not cause any deadlock in the
system.

4.Deadlock detection and recovery

With this method, the deadlock is detected first by using some algorithms of the
resource-allocation graph. This graph is mainly used to represent the allocations of
various resources to different processes. After the detection of deadlock, a number
of methods can be used in order to recover from that deadlock.

 One way is preemption by the help of which a resource held by one process
is provided to another process.
 The second way is to roll back, as the operating system keeps a record of
the process state and it can easily make a process roll back to its previous
state due to which deadlock situation can be easily eliminate.

 The third way to overcome the deadlock situation is by killing one or more
processes.

#Deadlock Prevention
If we simulate deadlock with a table which is standing on its four legs then we can
also simulate four legs with the four conditions which when occurs simultaneously,
cause the deadlock.

However, if we break one of the legs of the table then the table will fall definitely.
The same happens with deadlock, if we can be able to violate one of the four
necessary conditions and don't let them occur together then we can prevent the
deadlock.

Let's see how we can prevent each of the conditions.

1. Mutual Exclusion

Mutual section from the resource point of view is the fact that a resource can never
be used by more than one process simultaneously which is fair enough but that is
the main reason behind the deadlock. If a resource could have been used by more
than one process at the same time then the process would have never been waiting
for any resource.

However, if we can be able to violate resources behaving in the mutually exclusive


manner then the deadlock can be prevented.

Spooling

For a device like printer, spooling can work. There is a memory associated with the
printer which stores jobs from each of the process into it. Later, Printer collects all
the jobs and print each one of them according to FCFS. By using this mechanism,
the process doesn't have to wait for the printer and it can continue whatever it was
doing. Later, it collects the output when it is produced.
Although, Spooling can be an effective approach to violate mutual exclusion
but it suffers from two kinds of problems.

1. This cannot be applied to every resource.


2. After some point of time, there may arise a race condition between the
processes to get space in that spool.

We cannot force a resource to be used by more than one process at the same time
since it will not be fair enough and some serious problems may arise in the
performance. Therefore, we cannot violate mutual exclusion for a process
practically.

2. Hold and Wait

Hold and wait condition lies when a process holds a resource and waiting for some
other resource to complete its task. Deadlock occurs because there can be more
than one process which are holding one resource and waiting for other in the cyclic
order.

However, we have to find out some mechanism by which a process either doesn't
hold any resource or doesn't wait. That means, a process must be assigned all the
necessary resources before the execution starts. A process must not wait for any
resource once the execution has been started

(Hold and wait) = !hold or !wait (negation of hold and wait is, either you don't
hold or you don't wait)
This can be implemented practically if a process declares all the resources initially.
However, this sounds very practical but can't be done in the computer system
because a process can't determine necessary resources initially.

Process is the set of instructions which are executed by the CPU. Each of the
instruction may demand multiple resources at the multiple times. The need cannot
be fixed by the OS.

The problem with the approach is:

1. Practically not possible.


2. Possibility of getting starved will be increases due to the fact that some
process may hold a resource for a very long time.

3. No Preemption

Deadlock arises due to the fact that a process can't be stopped once it starts.
However, if we take the resource away from the process which is causing deadlock
then we can prevent deadlock.

This is not a good approach at all since if we take a resource away which is being
used by the process then all the work which it has done till now can become
inconsistent.

Consider a printer is being used by any process. If we take the printer away from
that process and assign it to some other process then all the data which has been
printed can become inconsistent and ineffective and also the fact that the process
can't start printing again from where it has left which causes performance
inefficiency.

4. Circular Wait

To violate circular wait, we can assign a priority number to each of the resource. A
process can't request for a lesser priority resource. This ensures that not a single
process can request a resource which is being utilized by some other process and
no cycle will be formed.
Among all the methods, violating Circular wait is the only approach that can be
implemented practically.

#Deadlock Avoidance
In deadlock avoidance, the request for any resource will be granted if the resulting
state of the system doesn't cause deadlock in the system. The state of the system
will continuously be checked for safe and unsafe states.

In order to avoid deadlocks, the process must tell OS, the maximum number of
resources a process can request to complete its execution.

The simplest and most useful approach states that the process should declare the
maximum number of resources of each type it may ever need. The Deadlock
avoidance algorithm examines the resource allocations so that there can never be a
circular wait condition.

Safe and Unsafe States

the resource allocation state of a system can be defined by the instances of


available and allocated resources, and the maximum instance of the resources
demanded by the processes.
A state of a system recorded at some random time is shown below.

Table 1: Resources Assigned

Process Type 1 Type 2 Type 3 Type 4

A 3 0 2 2

B 0 0 1 1

C 1 1 1 0

D 2 1 4 0

Table 2: Resources still needed

Process Type 1 Type 2 Type 3 Type 4

A 1 1 0 0

B 0 1 1 2

C 1 2 1 0

D 2 1 1 2

1. E = (7 6 8 4)
2. P = (6 2 8 3)
3. A = (1 4 0 1)
Above tables and vector E, P and A describes the resource allocation state of a
system. There are 4 processes and 4 types of the resources in a system. Table 1
shows the instances of each resource assigned to each process.

Table 2 shows the instances of the resources, each process still needs. Vector E
is the representation of total instances of each resource in the system.

Vector P represents the instances of resources that have been assigned to


processes. Vector A represents the number of resources that are not in use.

A state of the system is called safe if the system can allocate all the resources
requested by all the processes without entering into deadlock.

If the system cannot fulfill the request of all processes then the state of the
system is called unsafe.

The key of Deadlock avoidance approach is when the request is made for
resources then the request must only be approved in the case if the resulting
state is also a safe state.

Resource Allocation Graph


The resource allocation graph is the pictorial representation of the state of a
system. As its name suggests, the resource allocation graph is the complete
information about all the processes which are holding some resources or waiting
for some resources.

It also contains the information about all the instances of all the resources whether
they are available or being used by the processes.

In Resource allocation graph, the process is represented by a Circle while the


Resource is represented by a rectangle. Let's see the types of vertices and edges in
detail.
Vertices are mainly of two types, Resource and process. Each of them will be
represented by a different shape. Circle represents process while rectangle
represents resource.

A resource can have more than one instance. Each instance will be represented by
a dot inside the rectangle.
Edges in RAG are also of two types, one represents assignment and other
represents the wait of a process for a resource. The above image shows each of
them.

A resource is shown as assigned to a process if the tail of the arrow is attached to


an instance to the resource and the head is attached to a process.

A process is shown as waiting for a resource if the tail of an arrow is attached to


the process while the head is pointing towards the resource.

Example
Let'sconsider 3 processes P1, P2 and P3, and two types of resources R1 and R2.
The resources are having 1 instance each.

According to the graph, R1 is being used by P1, P2 is holding R2 and waiting for
R1, P3 is waiting for R1 as well as R2.

The graph is deadlock free since no cycle is being formed in the graph.
Deadlock Detection using RAG

If a cycle is being formed in a Resource allocation graph where all the resources
have the single instance then the system is deadlocked.

In Case of Resource allocation graph with multi-instanced resource types, Cycle is


a necessary condition of deadlock but not the sufficient condition.

The following example contains three processes P1, P2, P3 and three resources R2,
R2, R3. All the resources are having single instances each.

If we analyze the graph then we can find out that there is a cycle formed in the
graph since the system is satisfying all the four conditions of deadlock.
Allocation Matrix

Allocation matrix can be formed by using the Resource allocation graph of a


system. In Allocation matrix, an entry will be made for each of the resource
assigned. For Example, in the following matrix, en entry is being made in front of
P1 and below R3 since R3 is assigned to P1.

Process R1 R2 R3

P1 0 0 1

P2 1 0 0

P3 0 1 0

Request Matrix
In request matrix, an entry will be made for each of the resource requested.
As in the following example, P1 needs R1 therefore an entry is being made in
front of P1 and below R1.
Process R1 R2 R3

P1 1 0 0

P2 0 1 0

P3 0 0 1
Avial = (0,0,0)

Neither we are having any resource available in the system nor a process going to
release. Each of the process needs at least single resource to complete therefore
they will continuously be holding each one of them.

We cannot fulfill the demand of at least one process using the available resources
therefore the system is deadlocked as determined earlier when we detected a cycle
in the graph.

Deadlock Detection and Recovery

In this approach, 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 main task of the OS is detecting the deadlocks. The OS can detect the
deadlocks with the help of Resource allocation graph.

In single instanced resource types, if a cycle is being formed in the system then
there will definitely be a deadlock. On the other hand, in multiple instanced
resource type graph, detecting a cycle is not just enough. We have to apply the
safety algorithm on the system by converting the resource allocation graph into the
allocation matrix and request matrix.
In order to recover the system from deadlocks, either OS considers resources or
processes.

For Resource
 Preempt the resource

We can snatch one of the resources from the owner of the resource (process) and
give it to the other process with the expectation that it will complete the execution
and will release this resource sooner. Well, choosing a resource which will be
snatched is going to be a bit difficult.

 Rollback to a safe state

System passes through various states to get into the deadlock state. 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.

For Process

 Kill a process

Killing a process can solve our problem but the bigger concern is to decide which
process to kill. Generally, Operating system kills a process which has done least
amount of work until now.

 Kill all process

This is not a suggestible approach but can be implemented if the problem becomes
very serious. Killing all process will lead to inefficiency in the system because all
the processes will execute again from starting.
Banker's Algorithm in Operating
System
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.

Characteristics of Banker's Algorithm

The characteristics of Banker's algorithm are as follows:

 If any process requests for a resource, then it has to


wait.

 This algorithm consists of advanced features for


maximum resource allocation.

 There are limited resources in the system we have.

 In this algorithm, if any process gets all the needed


resources, then it is that it should return the resources in
a restricted period.

 Various resources are maintained in this algorithm that


can fulfill the needs of at least one client.

Let us assume that there are n processes and m resource


types.

Data Structures used to implement the Banker’s Algorithm

Some data structures that are used to implement the


banker's algorithm are:
1. Available

It is an array of length m. It represents the number of


available resources of each type. If Available[j] = k, then there
are k instances available, of resource type Rj.

2. Max

It is an n x m matrix which represents the maximum number


of instances of each resource that a process can request.
If Max[i][j] = k, then the process Pi can request
atmost k instances of resource type Rj.

3. Allocation

It is an n x m matrix which represents the number of


resources of each type currently allocated to each process.
If Allocation[i][j] = k, then process Pi is currently
allocated k instances of resource type Rj.

4. Need

It is a two-dimensional array. It is an n x m matrix which


indicates the remaining resource needs of each process.
If Need[i][j] = k, then process Pi may need k more instances of
resource type Rj to complete its task.

Need[i][j] = Max[i][j] - Allocation [i][j]

Banker’s algorithm comprises of two algorithms:

1. Safety algorithm

2. Resource request algorithm


1. Safety Algorithm

A safety algorithm is an algorithm used to find whether or not


a system is in its safe state. The algorithm is as follows:

1. Let Work and Finish be vectors of length m and n,


respectively. Initially,

Work = Available

Finish[i] =false for i = 0, 1, ... , n - 1.

This means, initially, no process has finished and the number


of available resources is represented by the Available array.

2. Find an index i such that both

Finish[i] ==false

Needi <= Work

If there is no such i present, then proceed to step 4.

It means, we need to find an unfinished process whose


needs can be satisfied by the available resources. If no
such process exists, just go to step 4.

3. Perform the following:

Work = Work + Allocationi

Finish[i] = true

Go to step 2.

When an unfinished process is found, then the resources


are allocated and the process is marked finished. And
then, the loop is repeated to check the same for all other
processes.

4. If Finish[i] == true for all i, then the system is in a safe


state.

That means if all processes are finished, then the system


is in safe state.

This algorithm may require an order of mxn² operations in


order to determine whether a state is safe or not.

2.Resource Request Algorithm

Now the next algorithm is a resource-request algorithm and it


is mainly used to determine whether requests can be safely
granted or not.

Let Requesti be the request vector for the process Pi. If


Requesti[j]==k, then process Pi wants k instance of Resource
type Rj.When a request for resources is made by the process
Pi, the following are the actions that will be taken:

1. If Requesti <= Needi, then go to step 2;else raise an error


condition, since the process has exceeded its maximum
claim.

2.If Requesti <= Availablei then go to step 3; else Pi must


have to wait as resources are not available.

3.Now we will assume that resources are assigned to process


Pi and thus perform the following steps:

Available= Available-Requesti;

Allocationi=Allocationi +Requesti;

Needi =Needi - Requesti;


If the resulting resource allocation state comes out to be
safe, then the transaction is completed and, process Pi is
allocated its resources. But in this case, if the new state is
unsafe, then Pi waits for Requesti, and the old resource-
allocation state is restored.

Disadvantages of Banker's Algorithm

Some disadvantages of this algorithm are as follows:

1. During the time of Processing, this algorithm does not


permit a process to change its maximum need.

2. Another disadvantage of this algorithm is that all the


processes must know in advance about the maximum
resource needs.

3. This algorithm permits the requests to be provided in


constrained time, but for one year which is a fixed
period.

Now its time to take a look at the Example of Banker's


Algorithm:

Example:

Let us consider the following snapshot for


understanding the banker's algorithm:
1. calculate the content of the need matrix?
2. Check if the system is in a safe state?
3. Determine the total sum of each type of resource?

Solution:

1. The Content of the need matrix can be calculated by using


the formula given below:

Need = Max – Allocation

2. Let us now check for the safe state.

Safe sequence:

1. For process P0, Need = (3, 2, 1) and

Available = (2, 1, 0)

Need <=Available = False


So, the system will move to the next process.

2. For Process P1, Need = (1, 1, 0)

Available = (2, 1, 0)

Need <= Available = True

Request of P1 is granted.

Available = Available +Allocation

= (2, 1, 0) + (2, 1, 2)

= (4, 2, 2) (New Available)

3. For Process P2, Need = (5, 0, 1)

Available = (4, 2, 2)

Need <=Available = False

So, the system will move to the next process.

4. For Process P3, Need = (7, 3, 3)

Available = (4, 2, 2)

Need <=Available = False

So, the system will move to the next process.

5. For Process P4, Need = (0, 0, 0)

Available = (4, 2, 2)

Need <= Available = True

Request of P4 is granted.

Available = Available + Allocation


= (4, 2, 2) + (1, 1, 2)

= (5, 3, 4) now, (New Available)

6. Now again check for Process P2, Need = (5, 0, 1)

Available = (5, 3, 4)

Need <= Available = True

Request of P2 is granted.

Available = Available + Allocation

= (5, 3, 4) + (4, 0, 1)

= (9, 3, 5) now, (New Available)

7. Now again check for Process P3, Need = (7, 3, 3)

Available = (9, 3, 5)

Need <=Available = True

The request for P3 is granted.

Available = Available +Allocation

= (9, 3, 5) + (0, 2, 0) = (9, 5, 5)

8. Now again check for Process P0, = Need (3, 2, 1)

= Available (9, 5, 5)

Need <= Available = True

So, the request will be granted to P0.

Safe sequence: < P1, P4, P2, P3, P0>


The system allocates all the needed resources to each
process. So, we can say that the system is in a safe
state.

3. The total amount of resources will be calculated by the


following formula:

The total amount of resources= sum of columns of allocation


+ Available

= [8 5 7] + [2 1 0] = [10 6 7]

#Classical Problems of Synchronization


Semaphore can be used in other synchronization problems besides Mutual
Exclusion.

Below are some of the classical problems depicting flaws of process


synchronization in systems where cooperating processes are present.

We will discuss the following three problems:

1.Bounded Buffer Problem

Because the buffer pool has a maximum size, this problem is often called
the Bounded buffer problem.

 This problem is generalized in terms of the Producer-Consumer problem,


where a finite buffer pool is used to exchange messages between producer
and consumer processes.

 The solution to this problem is, creating two counting semaphores "full" and
"empty" to keep track of the current number of full and empty buffers
respectively.

 This Producers mainly produce a product and consumers consume the


product, but both can use one of the containers each time.

 The main complexity of this problem is that we must have to maintain the
count for both empty and full containers that are available.

2.Dining Philosophers Problem

 The dining philosopher's problem involves the allocation of limited


resources to a group of processes in a deadlock-free and starvation-free
manner.

 There are five philosophers sitting around a table, in which there are five
chopsticks/forks kept beside them and a bowl of rice in the center, When a
philosopher wants to eat, he uses two chopsticks - one from their left and
one from their right. When a philosopher wants to think, he keeps down both
chopsticks at their original place.
3.The Readers Writers Problem

 In this problem, there are some processes (called readers) that only read the
shared data, and never change it, and there are other processes
(called writers) that may change the data in addition to reading, or instead
of reading it.

 There is various type of readers-writers problems, most centered on relative


priorities of readers and writers.

 The main complexity of this problem occurs from allowing more than one
reader to access the data at the same time.

You might also like