Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 17

UNIT V

Deadlocks

Deadlock Avoidance in Multiple


Instances of Resources
The Resource Allocation Graph cycle cannot be applied when there are multiple
instances of resources, as it is not certain that deadlock will occur.
Hence, we need an algorithm to check the safe state, when a resource is requested.
Dijkstra designed an algorithm, known as Bankers Algorithm, which considers the
analogy of the bank.
The algorithm maintains few data structures, such that whenever a resource is
requested, it can be checked whether the request maintains the safe state of the
system.
If it does, the request can be granted; otherwise, the process must wait until there
are sufficient resources available.
The Bankers Algorithm has two parts: Safety Test Algorithm that checks the current
state of the system for its safe state and Resource Request-Handling Algorithm that
verifies whether the requested resources, when allotted to the process, affect the safe
state.

11/10/2014

ChonuChinnus

Deadlock Avoidance in Multiple


Instances of Resources: Data Structures
Total Resources in a System: Stores the total number of resources in a system
and denoted by Total_Res[i]=j.
It means that there are j instances of Resource type Ri in the system.
Maximum Demand of a Process: Whenever a process enters the system, it
declares its maximum demand of resources. It is denoted by Max[i,j]=k.
It means, process Pi has a total demand of k instances of resources of type Rj. This
is data structure is a p*r matrix, where p is number of processes and r is the
number of resource types.
Current Allocation of Instances of Each Type of Resource: Indicates the current
allocation status of all resource types to various processes in the system and is
denoted by Alloc[i,j]=k.
It means process Pi is allotted k instances of resource type Rj. This is also a p*r
matrix.
11/10/2014

ChonuChinnus

Deadlock Avoidance in Multiple


Instances of Resources: Data Structures
Number of Available Resources: Stores the current available instances of each
resource type and is denoted by Av[i]=j.
Av[i]=Total_Res[i]-all processes Alloc[i], where i is the resource type Ri.
Current Need of a Process: Indicates the current remaining resource need of
each process and is denoted by Need[i,j]=k.
It means, process Pi may require k more instances of resource Type Rj.
Need[i,j]= Max[i,j]-Alloc[i,j].
Request for a Process: Stores the resource request of a process Pi and is denoted
by Reqi[j]=k.
It means, process Pi has requested k instances of resource types Rj.

11/10/2014

ChonuChinnus

Deadlock Avoidance in Multiple


Instances of Resources
Let Current_Avail and Marked be two
vectors of length n and p respectively.
SafeString is an array to store the
process IDs. Algorithm is given by:
1. Current_Avail= Av
Initialize Marked as:
2. for(i=0;i<p;i++)
Marked[i]=false
3. Find a process Pi such that
Needi Current_Avail and
Marked[i]=false

4. If(found)
{
Current_Avail=Current_Avail + Alloci
Marked[i]=true
Save the process no in SafeString[ ]
Goto step 3
}
5. If(Marked[i]==true) for all processes,
then system is in safe state
6. Print SafeString. Otherwise, System is
not in safe state and is in deadlock

11/10/2014

ChonuChinnus

Deadlock Avoidance in Multiple


Instances of Resources
2. else
{

Let Reqi be the vector to store the


resource request for process Pi

Av=Av-Reqi

1. If(ReqiNeedi)

Alloci=Alloci + Reqi

The request is not a feasible request


and is rejected.

Needi=Needi Reqi

2. elseif(ReqiAvi)

3. Resources are not available, so the


process must wait

Execute safety test algorithm, assuming


state has been
changed.
If ( State is safe)
Resources will be allocated to the process
else
Do not allocate the resources to the
process
}

11/10/2014

ChonuChinnus

Example
Consider a system with the following information and determine whether the
system is in safe state
Total_Res

R1
15

R2
8

Max

Process

11/10/2014

R3
8

Alloc

R1

R2

R3

R1

R2

R3

P1

P2

P3

P4

P5

ChonuChinnus

Deadlock Detection
Due to practical issues, Deadlock Avoidance cannot be implemented.
So, if a system cannot implement Deadlock Prevention or Deadlock Avoidance
then it may lead to deadlock situation.
It is necessary to detect whether there is a deadlock in the system.
If there is a deadlock, then which process is causing it.
It is better to find deadlock process, as it helps in recovering from deadlock.

Deadlock Detection Algorithm has two parts:


Detection of single instance of resource.
Detection of multiple instances of resources.

11/10/2014

ChonuChinnus

Deadlock Detection:

Detection of Single Instance of Resource

To understand and detect single instance of resource, we need to perform some


modifications to Resource Allocation Graph (RAG).
RAG is used for detecting a cycle in Deadlock Avoidance Algorithm.
It becomes difficult to store information about request edge and assignment edge
to detect a cycle.
Therefore, in the graphical representation of RAG, some modifications can be
performed and a new graph, known as wait-for graph is formed.

In fact, RAG is optimized by eliminating the resource nodes to get the wait-for
graph.

11/10/2014

ChonuChinnus

Deadlock Detection:

Detection of Single Instance of Resource

The wait for graph for the RAG is as shown below.


There is an edge from P1 to P2, because P1 waits for R1, and R1 is held by P2.
Therefore, an edge exits between the processes, only if one process waits for another.
For example, an edge Pa Pb exists in wait-for graph only if there are edges Pa Ri and Ri Pb in
RAG.
P1

P2

P3

R1

R2

R3

P1

P4

P3

P4

Resource Allocation Graph


11/10/2014

P2

Wait-for Graph
ChonuChinnus

10

Deadlock Detection:

Detection of Multiple Instances of Resources

When there are multiple instance of resource types, then cycle detection is not
sufficient to detect deadlock.
So, a algorithm similar to bankers algorithm is needed which checks the system
state with some data structures maintained and signal whether the system is in
deadlock.
As in deadlock avoidance algorithm, Deadlock Detection also has two phases:
In the first phase, the data structures needed are defined and maintained.

In the second phase, the algorithm detects the process which causes the
deadlock if any.

11/10/2014

ChonuChinnus

11

Deadlock Detection:

Detection of Multiple Instances of Resources

PHASE I: (Data Structures are defined)


1. Total Resources in system: Stores total number of resources in the system.
Total_Res[i]=j
2. Current allocation of instances of each resource type of resource: Indicates the current allocation
status of all resources types to various processes in system.
Alloc[i,j]=k
3. Number of available resources: Stores current available instances of each resource type.

Av[i]=j
Av[i]=Total_Res[i]-allprocesses Alloc[i], where i is the resource type Ri
4. Request of each process: Stores the current request of each process of each process in the form of
p*r matrix, where p is the number of processes and r is the number of resource types.

11/10/2014

ChonuChinnus

12

Deadlock Detection:

Detection of Multiple Instances of Resources

PHASE II: (Detection Algorithm)

PHASE II: (Detection Algorithm)

Let Current_Avail and Marked be the two vectors

4. If(found)

of lengths n and p respectively.

SafeString is an array to store the process ids

Current_Avail=Current_Avail + Alloci

1. Current_Avail=Av

Marked[i]=false

2. For all i=1 to p

Save process to SafeString[]

initialize Marked as Marked[i]=false

goto to step 3

if(Alloci==0)

initialize Marked as Marked[i]=false


3. Find a process P such that
ReqiCurrent_Avail and Marked[i]=false

11/10/2014

5. If (marked[i]==true) is for all processes, the


system has no deadlock.
Print SafeString
Else Deadlock by Process Pi

ChonuChinnus

13

Deadlock Recovery
The consequent action from deadlock is recovery from it.
The aim of the deadlock detection is to find out which process has caused the
deadlock and resolve it, so that the system can resume its work.
Imagine two cars approaching each other on a narrow bridge, through which
only one car can pass through at a time. If no car is ready to move back, then of
course there will be deadlock situation forever.
This is same as in a non pre-emption condition. So, the only solution for
resolving the deadlock is for one of the cars to move back (rollback) and allow the
other car to pass through the bridge, thereby resolving the deadlock.
An Operating System handles the deadlock in the same way.
There should be pre-emption of resources from one process, so that the other
process can continue and deadlock is removed.

11/10/2014

ChonuChinnus

14

Deadlock Recovery
Another solution is to abort the process which cause the deadlock.
Rollback and aborting a process cannot ne implemented always, as there is a
cost incurred in each solution.
Some recovery methods are:
Resource Pre-emption
Rollback
Aborting a process

Resource Pre-emption: In this method, resources are pre-empted from a process,


which has caused the deadlock. These resources are then given to other
processes, such that other processes execute their completion and in this way,
the deadlock is removed. In other words, No Pre-emption condition for deadlock
should be broken, wherever applicable. The first issue is to choose a process
whose resources can be pre-empted and given to other processes.
11/10/2014

ChonuChinnus

15

Deadlock Recovery
The decision to choose a process for pre-emption is driven by following cost
factors:
Number and type of resource a deadlock process is holding: If the number of
resources is less compared to the requirement of other processes to break the
deadlock, or if the process is not holding the desired type of resource, then those
resources need not be pre-empted.
Execution span of a process: Process with the longest execution span should be
pre-empted first. A process, whose execution has just started and requires many
resources to complete, will be the right victim for pre-emption.
Starvation of Process: Based on the above discussed factors, it may be possible
that the same process is always chosen for resource pre-emption, resulting in a
starvation situation. Thus, it is important to ensure that the process should not
starve. This can be done by fixing the number of times a process can be chosen
for resources pre-emption
11/10/2014

ChonuChinnus

16

Deadlock Recovery
Rollback: Checkpoints are good solution to recover from deadlock, apart from
using it in the resource pre-emption. The checkpoints save the state of the
system at specified point in time. The resource state is saved in a separate file,
along with the memory image. When a deadlock is detected, the state is analyzed
and the deadlock process is detected. The process, holding that resources rolled
back to safe checkpoint. After allocating the resource to the desired process, the
system continues, thereby, recovering from the deadlock. The process, which
was rolled back, restarts from the checkpoint.

Abort the Process: Aborting a process to resolve the deadlock is a costly solution
in terms of process computation. After aborting a process, run the deadlock
detection algorithm to confirm whether the deadlock still exists. As in resource
pre-emption, a process has to be chosen for aborting. But that aborted process
should least affect the system and should run without any further deadlocks.
Aborting a process depends on: Number and type of resources a deadlock
process holds, Execution span of a process, Process Priority, Type of Resources
11/10/2014

ChonuChinnus

17

You might also like