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

Chapter-4

Deadlocks

Introduction:

In a multiprogramming environment, several processes may compete for resources. A
process requests resources; and if the resources are not available at that time, the process enters a
waiting state. Sometimes, a waiting process is never again able to change state, because the
resources it has requested are held by other waiting processes. This situation is called a deadlock.

System Model:

A system consists of a finite number of resources to be distributed among a number of
processes. The resources are partitioned into several Types, each consisting of some number of
identical instances.
Examples: Memory space, CPU cycles, files, and I/O devices etc.
Resources are two types.
1. Pre-emptable resource: it is the one can be taken away from a process. (Memory)
2. Non pre-emptable resource: it is the one cannot be taken away from a process. (Printer)

A process may utilize a resource in only the following sequence:
1. Request: Initially, the process must request for a resource.
2. Use: The process can operate on the resource (for example, if the resource
is a printer, the process can print on the printer).
3. Release. The process releases the resource.

Necessary Conditions for deadlock situation:
A deadlock situation can arise if the following four conditions hold simultaneously
In a system:
1. Mutual exclusion: only one process at a time can use the resource. If another Process
requests that resource, the requesting process must be wait until the resource has been
released.
2. Hold and wait: A process must be holding at least one resource and waiting to acquire
additional resources that are currently being held by other processes.
3. No preemption: Resources cannot be preempted. The resource can be released by the
process holding it only after that process has completed its task.
4. Circular wait: a circular chain of two or more process must exist. each process waiting
for a resource held by other process.


Resource-Allocation Graph:

Deadlocks can be described more precisely in terms of a directed graph called a system
resource-allocation graph. This graph consists of a set of vertices V and a set of edges E. The set
of vertices V is partitioned into two different types of nodes:
1. P = {P
1
, P
2
,,.., P
n
) the set consisting of all the active processes in the system.
2. R = {R
1
,R
2
. R
n
} the set consisting of all resource types in the system.

A directed edge from process P
1
to resource type R
1
is denoted by P
1
R
1
(request edge)

it
indicates that process P
1
has requested an instance of resource type R
1.

A directed edge from resource type R
1
to process P
1
is denoted by R
1
P
1
(assignment edge)
it indicates that resource type R
1
is allocated to p
1
.

Pictorially, we represent each process P as a circle and each resource type R as a rectangle. A
resource may have more instance denoted by placing dotes in rectangular box of resource R.

Example:


P= {P
1
, P
2
, P
3
}, R= {R
1
, R
2
, R
3
, R
4
}
E= {P
1
R
1
, P
2
R
3
, R
1
P
2
, R
2
P
1
, R
2
P
2
, R
3
P
3
}
R
1
-one instance, R
2
- two instance, R
3
-one instance, R
4
-three instance

Resource-allocation graph with cycle with dead lock:


The above graph having two cycles

1. P
1
R
1
P
2
R
3
P
3
R
2
P
1
.
2. P
2
R
3
P
3
R
2
P
2
.
All the three process are deadlocked. Process P
1
waiting for P
2
to release the R
1
. Process P
2
is
waiting for P
3
to release the resource R
3
and P
3
is waiting for P
1
or P
2
to release the resource R
2
.

Resource-allocation graph with cycle with no dead lock:



The cycle in this graph is P
1
R
1
P
3
R
2
P
1
.Since P4 after use it releases its resource R2 then it
can be allocated to P3 thus breaking the cycle leads to no dead lock.

If a resource-allocation graph does not have a cycle, then the system is not in a deadlocked state.
If there is a cycle, then the system may or may not be in a deadlocked state.


Deadlock Prevention:

As we know deadlock to occur, each of the four necessary conditions must hold. By ensuring that
at least one of these conditions cannot hold, we can prevent the occurrence of a deadlock.

1 Mutual Exclusion:
The mutual-exclusion condition must hold for non-sharable resources. For example, a
printer cannot be simultaneously shared by several processes. Sharable resources, in contrast, do
not require mutually exclusive access and thus cannot be involved in a deadlock. We cannot
prevent deadlocks by denying the mutual-exclusion condition

2. Hold and Wait: To ensure that the hold-and-wait condition never occurs in the system, we must
guarantee that, whenever a process requests a resource, it does not hold any other resources.

3. No Preemption: The third necessary condition for deadlocks is that there be no preemption of
resources that have already been allocated. If a process is holding some resources and requests
another resource that cannot be immediately allocated to it, then all resources currently being held
are preempted.

4. Circular Wait: The fourth and final condition for deadlocks is the circular-wait condition. This
condition can be prevented by defining a linear ordering of resource. Each resource having a
unique integer number. Each process can request resource only in an increasing order

You might also like