Deadlocks
Deadlocks
Allocated
Process1 Resource 1
Allocated
Resource 2
Allocated Request
Process 1 Resource 1 Process 2)
Allocated Resource 3
in the system
and R {R,, R,,
=
R, the set consisting of all r e s o u r c e types
processes in the system, ..,
.Process States:
Process P, is holding an instance of resource type R, and is waiting for an instance
of resource type R,.
Process P, is holding an instance of R, and an instance of R, and is waiting for an
instance of R
Process P, is holding an instance of R,
Given the definition of a resource-allocation graph,3 it can be shown that
fthe graph contains no cycles, then no process in the system is deadlocked.
If the graph does contain a cycle, then a deadlock may exist.
lfeach resource type has exactly one instance, then a cycle implies that a deadlock has occurred.
the cycle involves only a set of resource types, each of which has only a single instance, then
a deadlock has occurred. Each process involved in the cycle is deadlocked. In this case, a cycle
n ene graph is both a necessary and a sufficient condition for the existence of deadlock.
each resource type has several instances, then a cycle does not necessarily imply that a
eadlock has occurred. In this case, a cycle in the graph is a necessary but not a sufficient
condition for the existence of deadlock.
CStrate this concept, we look at the resource-allocation graph depicted in Figure 5.6.
ppose that process P, requests an instance of resource type R. Since no resource instance is
rently available, a request edge P,-R, is added to the graph (Figure 5.7). At this point, two
nimal cycles exist in the
system:
OperatingSystems
PR,-P,R,P, R,P,
P2R,P,R,>P, R
Processes P,, P and P, are deadlocked.
Process P, is waiting for the resource R
is
which is held by process P Process P,
waiting for either process P, process P,
or to
release resource R, In addition, processP, is |
P,
waiting for process P, to
release resource R,.
Now considerthe
resource-allocation graph (P
in Figure 5.7. In this example, w e also have a
cycle: R
P R ,P,~R,>P,
deadlock. Observe
However, there is no
its instance of
that process P, may release
r e s o u r c e can then
P
Deadlocks
Handling
5.5 Methods For for handling
deadlock avoidance are the main methods
deadlock prevention and
Deadlock detection,
as follows -
situation is resolved.
r e s o u r c e scheduling
3. Deadlock Avoidance:
Avoid deadlock by careful
to prevent a deadlock before it can
occur. So, the sys
4. Deadlock Prevention: It is important
executed to make sure it does not lead
to deadlock
checks each transaction before it is ed
possibility that a transaction may lead to deadlock, it is never allowe
there is even a slight
execute.
Deadiocks 5.9
5.6 Deadlock P r e v e n t i o n
.limination of "Mutual Exclusion Condition: The mutual exclusion condition must hold
far non-sharable resources. That is, several processes cannot
simultaneously share a single
sOurce. This condition is difficult to eliminate because some resources, such as the tape drive
nd printer, inherently non-shareable. A good example of a sharable
an
are
resource is Read-only
fles because if several processes attempt to open a
read-only file at the same time, then
they
can be granted simultaneous access to the file
A processneed not to wait for the sharable resource.
by denying the mutual exclusion condition
Generally, deadlocks cannot be prevented
because there are some resources
intrinsically non-sharable. Note that shareable that are
resources, like
mutually exclusive access and thus cannot be
involved in
read-only-file do not require
deadlock.
2. Elimination of "Hold and Wait Condition: There are two
hold and wait condition. possibilities for elimination of the
The first alternative is that a
process request be granted all of the resources it
once, prior to execution.
needs at
The second alternative is to disallow a
process from
has previously allocated resources. requesting resources whenever it
P is
holding R Resource R
Process
P1
P, is
waiting for R. Resource R
strategy
hours. t'his cause indefinitepostponement
can
the
a
maximum number
of resources
ofeach type there
If we have
Resource-Allocation Graph Algorithm
a
resource-allocation system with only one
the
resource-allocation graph instance of each resource
be used for deadlock
can type, a variant of
assignment edges, we introduce a new avoidance. In addition to the
that process type of edge, called a claim request and
P, may request resource R at some edge. A claim edge P R indicates
in
direction, but is represented by a dashed time in the future. This edge resembles a request edge
line. When process
Ris converted to a request edge. P, requests resource R, the claim
Similarly, when a resource R, is released edge
geKP,is
e
reconverted to a claim edge by P, the assignment
P,> X, We note that the resources must be claimed a
systerm. That is, before priori
process P, starts executing, all its claim
Ce-allocation edges must already appear in
the graph. We can relax this condition by allowing a claim the
graph only iff all
: the edges associated with edge P,>R to be added
Prequests resource R, The request can be
process Pi are claim edges. Suppose that
process
J
asignment edge R>\
t
Note ge R,> P, does not
granted only if converting the request
result in the formation of a cycle in the edgeP,>R
to an
that
ininthis we check for safety by resource-allocation graph
this Pra using a cycle-detection algorithm. An algorithm for detecting a cycle
graph requires
u i r e s an a order of
n, operations, where n is the number of processes in the
system.
5.7.3
Bankers Algorithm
resource-allocation system
descrwith
The resource-allocation applicable to a
n g r agraph
p h algorithm
algorithm is not
not
P that we
multiple instances of each resource type. The ck-avoidance algorithm
algoru
deadlock-avoidance
cribe
next is applicable to such a
system, but is less
nt
efficient than the
The
graph
scheme
resource-allocation
in the system.
the system must determine whether the allocation
When a user requests a set of resources,
a safe state. If it will,
the r e s o u r c e s are allocated
of these resources will leave the system in
s o m e other process
releases enough resources.
the must wait until
otherwise, process
the banker's algorithm. These data
Several data structures must be maintained to implement
Data Structures
(Banker's Algorithm)
Thealgorit
hm far finding out whether or not a system is in a
safe state can be described
(1) Let Work and Finish be vectors of length 'm' and 'n' respectively. as follows:
Initialize: Work Available
=
Resource-Request Algorithm
Rt
Kequest, be the request array for process P, Request, ] k means process P wants k instances of
=
SOurce type
R. When a request for resources is made by process P, the following actions are taken:
1 If Request <= Need
u0TO step (2); otherwise, raise an error condition, since the process has exceeded its maximum
claim.
2.FRequest= Available
Goto step (3); otherwise, P must wait, since the resources are not available.
3.
Have the system
system pretend to have allocated the requested resources to process Pi by modifying
the state as
follows:
Available Available Requesti
Allocation= Allocation, Request
+
Need = Need-
Request
Characteristics of Banker's Algorithm
Deadlock Detection
Recovery
Deadloek
99
in the system by deadlock detection algorithms, then it has to be
k has
e adeadlokh been detected
some recovery mechanism. The brute force approach is to reboot the computer,
d by using other
ient because it may lose complete data and waste computing time. Hence,
are broadly classified into two types. They are,
to recover from deadlock. They
are sed
Process Temination
Resource Preemption..
Process Termination
92 Resource Preemption
a this is
EtnOd, resources are deallocated or preempted from some processes, and the same
this
t o others until the deadlock is resolved. We have three important issues to implement
sheame. They are,
Sel to be preempted,
O n of Victim Process: Weneed to decide which process or resource is
deadlocked
ECIsion is based on cost factor which includes the number of resources, a
process is holding and CPU time consumed by it, etc.
Rollback: The process which was preempted cannot continue normal execution, because its
resources
d r e taken back. Hence, we need to roll back to some previous checkpoint or total
rollback to start it from the
beginning
Starvation Should ensure that a particular process should not
starve every time
preemption is done.