Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

PROJECT ON

DEADLOCK DETECTION,
RECOVERY AND AVOIDANCE

Submitted By:
Praveen Dewani 8103524

Aakansha Garg 8103543

Saumya Jain 8103552

Batch B-5
DEADLOCKS
A deadlock is a situation where in two or more competing actions are each
waiting for the other to finish, and thus neither ever does.

Conditions for Deadlock


There are four necessary and sufficient conditions for a Coffman deadlock to
occur.
 Mutual exclusion: resources cannot be shared.
 Hold and wait: processes request resources incrementally, and hold on
to what they've got.
 No preemption: Resources cannot be forcibly taken from processes.
 Circular wait: circular chain of waiting, in which each process is waiting
for a resource held by the next process in the chain.

Strategies for dealing with Deadlock


We can deal with the deadlock problem in one of four ways:

 Ignore the problem altogether ie, Ostrich algorithm .It may occur very
infrequently, cost of detection/prevention etc may not be worth it.
 Detection and Recovery
 Avoidance by careful resource allocation
 Prevention by structurally negating one of the four necessary conditions.

Prevention
Difference from avoidance is that here, the system itself is build in such a way
that there are no deadlocks.

Make sure atleast one of the 4 deadlock conditions is never satisfied.

 Removing the mutual exclusion condition means that no process may


have exclusive access to a resource.
 The "hold and wait" conditions may be removed by requiring processes to
request all the resources they will need before starting up. Another way
is to require processes to release all their resources before requesting all
the resources they will need.
 A "no preemption" (lockout) condition may also be difficult or impossible
to avoid as a process has to be able to have a resource for a certain
amount of time, or the processing outcome may be inconsistent or
thrashing may occur.
 The circular wait condition: Algorithms that avoid circular waits include
"disable interrupts during critical sections", and "use a hierarchy to
determine a partial ordering of resources".

Avoidance

Deadlock can be avoided if certain information about processes is available in


advance of resource allocation. For every resource request, the system sees if
granting the request will mean that the system will enter an unsafe state,
meaning a state that could result in deadlock. The system then only grants
requests that will lead to safe states. In order for the system to be able to figure
out whether the next state will be safe or unsafe, it must know in advance at
any time the number and type of all resources in existence, available, and
requested. One known algorithm that is used for deadlock avoidance is the
Banker's algorithm, which requires resource usage limit to be known in
advance.

Safe state is one where:

 It is not a deadlocked state


 There is some sequence by which all requests can be satisfied.

To avoid deadlocks, we try to make only those transitions that will take you
from one safe state to another. We avoid transitions to unsafe state.

Detection

Detecting a deadlock that has already occurred is easily possible since the
resources that each process has locked and/or currently requested are known
to the resource scheduler or OS. A special resource-allocation graph algorithm
can be used to detect whether there is any deadlock in the system. A resource-
allocation graph is a directed graph consisting of two different types of
nodes P = P1, P2,..., Pn, the set consisting of all active processes in the
system, and R = R1, R2,..., Rm, the set consisting of all resource types in the
system.

A directed edge from process Pi to resource Rj is denoted by Pi   Rj and


means that process Pi requested an instance resource type Rj, and is currently
waiting for that resource. A directed edge from resource type Rj to process Pi, is
denoted by Rj   Pi and means that an instance of resource type Rj has been
allocated to process Pi.

If the graph contains no cycles then no process in the system is deadlocked. If


the graph does contain a cycle then deadlock may exist.

Recovery

 Through preemption
 Rollback
o Keep checkpointing periodically
o When a deadlock is detected, see which resource is needed.
o Take away the resource from the process currently having it.
o Later on, you can restart this process from a check pointed state
where it may need to reacquire the resource.
 Killing processes
o Where possible, kill a process that can be rerun from the beginning
without ill effects.
ALGORITHMS USED
 Deadlock Avoidance-Bankers Algorithm
STEP 1: Initialize

(a) Work=Available
(b) Finish[i] = false for i=0..n

STEP 2: Find an index i such that

(a) Finish[i] = false


(b) Need[i] <= Work

If no such i exists goto STEP4

STEP 3: Work = Work + Allocation[i]

Finish[i] = true

Goto STEP2

STEP 4: If Finish[i] = true, for all i

The system is in safe state.

Description. The algorithm checks whether allocating the resources to


a requesting process will leave the system in a safe state or not. If the
resources required by the process are less than or equal to the resources
available, the process is provided with the resources else the process has
to wait till required resources are available. After the process is
completed, it returns the resources to the system. The algorithm also
helps in finding the safe sequence of allocation of resources to the
requesting processes.

 Deadlock Detection Algorithm


STEP 1: Initialize

(a) Work=Available
(b) For i=1,2,...,n
If Allocation i ≠ 0
Then Finish[i] = false
Else Finish[i] = true

STEP 2: Find an index i such that

(a) Finish[i] = false


(b) Request[i] <= Work

If no such i exists goto STEP4

STEP 3: Work = Work + Allocation[i]

Finish[i] = true

Goto STEP2

STEP 4: If Finish[i] = false, for some i (1<= i <= n)

The system is deadlocked due the process P[i].

Description. The algorithm checks whether a system is in deadlock or


not. When a process is completed, it returns the resources back to the
system. If the process is not complete, it is waiting for allocation of
resources. If there exists a process for which the resources requested are
more than the resources available, the system is in a deadlock due to the
process.

You might also like