You are on page 1of 9

Chapter 4 – DeadLocks

RESOURCE CONCEPT

* Multiprogramming environment, resource sharing is one of the primary goals of the operating
system.
* Deadlocks can occur when processes have been granted exclusive access to these resources.
* A resource can be a hardware device (e.g. a tape derive, I/O device) or apiece of information
(e.g a locked record in a database) or a CPU cycles.

The resources can be of two types


1. Preemptable resources
2. Non-preemptable resources

Preemptable:- Preemptive resources are those which can be taken away from a process without
causing any ill effects to the process.
Example:- Memory.

Non-preemptable:- Non-pre-emptive resources are those which cannot be taken away from the
process without causing any ill effects to the process.
Example:- CD-ROM (CD recorder), Printer.

1. What is Deadlock?
A set of processes is deadlocked if each process in the set is waiting for an event that only
another process in the set can cause.

Deadlocks are a set of blocked processes each holding a resource and waiting to acquire a
resource held by another process.

1
Operating System BCA-Department
Chapter 4 – DeadLocks

2. What are the state of resource?

A process requests the resource only in the following sequence:


Request: The process must request for the resource. If the request cannot be granted immediately,
then the requesting process must wait until it acquires the resource.
Use: The process can operate on the allotted resource
Release: The process releases the resource after using it

3. What is system model?

A system consists of finite number of resurces which must be distributed among a number of
computing process.

4. What are the characteristic of deadlock?

In a deadlock, processes never finish executing and system resources are tied up, preventing
other process from starting. Before learning the various methods for handling deadlock,
The features that characterize deadlocks. They are:
1. Necessary condition
2. Resource-Allocation Graph

5. What are the necessary condition for deadlock?

A Deadlock situation occurs if the following four conditions hold simultaneously in a system:
1 Mutual Exclusion: Mutual exclusion means only one process can use the resource at time.
If another process requests the same resource, the requesting process must
wait until the resource has been released.
2 Hold and Wait: Each process continues to hold resources already allocated to it while waiting to
acquire resources held by other resources.
3. No Pre-emption: Resources cannot be preempted, that is, a resource can be released voluntarily
by the process holding it only after it has completed its task. Resources cannot be
forcibly taken from a process.
4. 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.

2
Operating System BCA-Department
Chapter 4 – DeadLocks
6. Explain the Resource-Allocation Graph.
Deadlocks can be described more precisely in terms of directed graph called a system
Resource-Allocation Graph
Resource-Allocation Graph consists of set of vertices V & a set of edges E
The set of vertices V is partitioned into two types P and R:
P = {p1,p2…,pn}is the set of all active processes in the system
R= {R1,R2…,Rn} is the set consisting of all resource types of in the system.
Pi Rj This edge is called a request edge.
Rj Pi This edge is called a assignment edge.

3
Operating System BCA-Department
Chapter 4 – DeadLocks
7. Explain the methods for handling deadlock?

The basic methods for handling deadlocks are:


1 Deadlock Prevention- It includes a set of methods for ensuring that at least one of the necessary
conditions do not hold good thus preventing deadlock.
2. Deadlock Avoidance - Do not grant a resource request to a process if this allocation has the
potential to lead to a deadlock.
3. Deadlock Detection and Recovery - Allows the system to enter a deadlock state and then
recovering from it
4. Head-in-the-sand approach Ignore the problem altogether and pretend that deadlocks never
occurred in the system. This is the approach used by most OS like UNIX,
Linux and Windows.

8. Explain deadlock Prevention?

We can prevent 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 printer at a later time and we
have allocated 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 required by other
high priority processes.
 
Eliminate Circular Wait:Each resource will be assigned with a numerical number. A process can
request the resources increasing/decreasing. order of numbering.

For Example, if P1 process is allocated R5 resources, now next time if P1 ask


for R4, R3 lesser than R5 such request will not be granted, only request for
resources more than R5 will be granted.:

4
Operating System BCA-Department
Chapter 4 – DeadLocks
8. Explain deadlock avoidance ?

Declare max no of resource that it may need


* Do not grant a resource request to a process if this allocation has the potential to lead to a deadlock.
* Requires that the system has some additional a priori information available

Safe state and unsafe


Resource allocation- graph algorithm
Bankers algorithm (dijkstra)

9. Explain safe and unsafe state ?

A state is said to be safe if it is not deadlocked and there is some


scheduling order in which every process can run to completion even
if all of them suddenly request their maximum number of resources
immediately.

EXAMPLE :
Total resources are 10

5
Operating System BCA-Department
Chapter 4 – DeadLocks
Resource allocation- graph algorithm
Ifall the resources in the system has only one instance, a variant of the resource-allocation
graph can be used for deadlock avoidance.

The Resource-allocation-Graph in addition to the request and assignment edges, it consists of a new
TYpe of edge called claim edge.
A claim edge P,→ R, indicates that the process P, may request R at some time in the future

V set of vertices
E set of edges
Request assignment claim
Edge Edge Edge
P R R P P –-->R
(in future may request )

6
Operating System BCA-Department
Chapter 4 – DeadLocks
8. Explain bankers algorithm ?

Banker's algorithm is the deadlock avoidance algorithm that is applicable to the resource
allocation system having multiple instances of each resource type

The banker's algorithm has two parts :


safety algorithm: is applied by an operating system, whether not a system is in safe state.
resource request algorithm: is applied by an operating system whether or not a request forwarded
by process for acquiring a resource would lead the system to an unsafe
state.

Banker's algorithm uses several data structures in form of vectors and matrices for its implementation.

Let there be n processes and m resource types.


1. Available: It is a vector of order m. It indicates the number of resources of each type that are
currently available.
2. Maximum: It is a request matrix of order of n x m. It indicates the maximum demand of each
process.
3. Allocation : It is a request matrix of order n x m. It indicates the number of resources currently
allocated to each process.
4. Need : It is a matrix of the order n x m. It indicates the number of resources that a process may
need in future.
5. Finish : It is the vector of order n. It contains the boolean value (true offalse) to indicate if a process
has been allocated the required resources andhas released all of its resources after using
them.

The algorithm for finding out whether or not a system is in a safe state can be described as follows:

Let Work and Finish be vectors of length ‘m’ and ‘n’ respectively.
1) Initialize: Work = Available
Finish[i] = false; for i=1, 2, 3, 4….n
2) Find an i such that both
a) Finish[i] = false
b) Needi <= Workif no such i exists goto step (4)
3) Work = Work + Allocation[i]
Finish[i] = truegoto step (2)
4) if Finish [i] = true for all i
then the system is in a safe state

7
Operating System BCA-Department
Chapter 4 – DeadLocks
Resource-Request Algorithm
Let Requesti be the request array for process Pi.
Requesti [j] = k means process Pi wants k instances of resource type Rj.
When a request for resources is made by process Pi, the following actions are taken:

1) If Requesti <= Need;
Goto step (2) ; otherwise, raise an error condition,
since the process has exceeded its maximum claim.
2) If Requesti <= Available
Goto step (3); otherwise, Pi must wait, since the resources are not available.
3) Have the system pretend to have allocated the requested resources to process Pi by
modifying the state as follows:
Available = Available – Request;
Allocationi = Allocationi + Reques;
Needi = Needi– Request;

solve the problem


Total availabe A=10 B=5 C=7

8
Operating System BCA-Department
Chapter 4 – DeadLocks
9. Explain Deadlock Detection and Recovery. 2 and 5 marks

Deadlock Detection: If a system does not employ either a deadlock prevention or a deadlock
avoidance algorithm, then a deadlock situation may occur. In this
environment, the system must provide.

* An Algorithm that examines the state of the system to determine whether a deadlock has occurred.
* An algorithm to recover from the deadlock,

Single Instance of Each Resource Type:


If all resources have only a single instance then we can define deadlock detection algorithm.

Deadlock Recovery:
Once a system has become deadlocked, the deadlock must be broken by removing
one or more of the necessary conditions
1) Process Termination: Recovery is ordinarily performed by forcibly removing a process from the
system and reclaiming its resources. Abort all deadlocked processes,
Abort one process at a time until the deadlock cycle is eliminated.

2) Resource Preemption: Recovery is performed using resource preemption.


We successively preempt some resource from process and give these
resources other processes until the deadlock cycle is broken.
Selecting a victim Rollback Starvation.

9
Operating System BCA-Department

You might also like