Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
19 views13 pages

Deadlocks

The document discusses deadlock characterization in operating systems, outlining the necessary conditions for deadlocks, which include mutual exclusion, hold and wait, no pre-emption, and circular wait. It also introduces the resource-allocation graph as a method to represent and analyze deadlocks, indicating that cycles in this graph can indicate potential deadlocks. Finally, it presents various methods for handling deadlocks, including avoidance, prevention, and detection strategies.

Uploaded by

vinod.pq
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views13 pages

Deadlocks

The document discusses deadlock characterization in operating systems, outlining the necessary conditions for deadlocks, which include mutual exclusion, hold and wait, no pre-emption, and circular wait. It also introduces the resource-allocation graph as a method to represent and analyze deadlocks, indicating that cycles in this graph can indicate potential deadlocks. Finally, it presents various methods for handling deadlocks, including avoidance, prevention, and detection strategies.

Uploaded by

vinod.pq
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

resource type.

5.4 Deadlock Characterization


In a deadlock, processes never and system resources are tied up, preventing
complete executing,
other jobs from starting their execution. Before we discuss the various methods for dealing with the
deadlock problem, we look more closely at features that characterize deadlocks.

5.4.1 Necessary Condition for Deadlocks


A deadlock happens in operating system when two or more processes need some resource to complete
their execution that is held by the other process. A deadlock occurs if the four Coffman conditions
hold true. But these conditions are not mutually exclusive. They are given as follows:
1. Mutual Exclusion: At least there should be one resource that has to be held in a non-sharable
manner; i.e., only a single process at a time can utilize the resource. If other process demands
that resource, the requesting process must be postponed until the resource gets released. In
the diagram below, there is a single instance of Resource 1 and it is held by Process 1 only.
Doadlocto 5.5

Allocated
Process1 Resource 1

Figure 5.2 : Example for the Mutual Exclusion Condition


2. Hold and Wait: A job must be holding at least one single resource and waiting to obtain
supplementary resources which are currently being held by several other processes. In
the diagram given below, Process 2 holds Resource 2 and Resource 3 and is requesting the
Resource 1 which is held by Process1.

Allocated
Resource 2
Allocated Request
Process 1 Resource 1 Process 2)

Allocated Resource 3

Figure 5.3 Example for the Hold and Wait Condition


3. No Pre-emption: Resources can't be anticipated; i.e., a resource can get released only willingly
by the process holding it, then after that, the process has completed its task. A resource cannot
be pre-empted froma process by force. A process can
only release a resource voluntarily, In the
diagram below, Process 2 cannot pre-empt Resource 1 from Process 1. It will only be released
when Process 1 relinquishes it voluntarily after its execution is complete.

Allocated Request Allocated


Process 1 Resource 1 Process 2 Resource 2

Figure 5.4: Example for the No Pre-emption Condition


4. The circular wait: The circular- wait situation
implies the hold-and-wait state or condition,
and hence all the four conditions are not
completely independent. They are interconnected
among each other. 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.
F1gur
5.4.2 Resource-Allocation Graph

precisely in terms of a directed graph called a system resource-


Deadlocks can be described more
set of edges E. The set of vertices
of a set of vertices V and a
allocation graph. This graph consists the set consisting of all the active
is partitioned into two different types of nodes: P {P,, P P}, ..,

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, ..,

it signifies that process


from P to r e s o u r c e type R is denoted by P, R;
A directed edge process
for that resource. A
instance of r e s o u r c e type R, and is currently waiting
P has requested an

is called a request edge;


R
directed edge P,
to process P, is denoted by R P it signifies thata
A directed edge from resource type R,
called assignment edge.
R, has been allocated to process P, is
an
instance of resource type
process P, as a circle and type R, as a rectangle. Sin
each resource
Pictorially, represent each
we
as a dot wi
resource type R, may have more
than one instance, we represent each such instance
the rectangle R, whereas an assignment eug
the rectangle. Note that a request edge points to only irce

must also designate one of the dots in the rectangle.


When process P, requests an instance of resou
lled
resource-allocation graph. When this request can be ful
type R,,a request edge is inserted in the
the request edge is instantaneously transformed to an assignment edge.
Deadlodko 5.7

no longer needs access to


when the process
it releases the resource; as a R R,
the resource,
is deleted.
result, the assignment edge
shown in
The resource-allocation graph
the following situation.
Figure 5.6 depicts
The sets P R, and E: P2 P
where P represents the processes; R
represents the resources; E represents the
Edges;
P {P, P,P}
R {R, R, R, R Ra
E {P R, P2 R, R, P
RP R P, R, - P R
Resource Instances: Figure 5.6: Resource-allocation Graph
One instance of resource type R,
Two instances ofresource type R,
One instance of resource type R,
Three instances of resource type R,

.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

resource type R, That


be allocated to breaking the cycle. In
P
resource-allocation graph Graph witha
summary, if
Resource-allocation
a
Figure 5.7
cycle, then the system is not Deadlock
does not have a Cycle but n o deadlocked
not be in a
then the system may or may
deadlocked state. If there is a cycle,
in a deadlock problem.
is important when w e deal with the
state. This observation

Deadlocks
Handling
5.5 Methods For for handling
deadlock avoidance are the main methods
deadlock prevention and
Deadlock detection,
as follows -

deadlocks. Details about


these are given of a deadlock is
to ignore the issue if the risk
issue: It might be preferable if a deadiock
1. Ignoring the one is sufficiently high. For instance,
low and the cost of avoiding limitations required
sufficiently
a reboot may be less painful than the
occurs ona PC once every 100 years,
to stop it. scheduler a
Deadlock can be detected by the resource

Deadlock Detection and Recovery: After a deadlocks


2. allocated to different processes.
the r e s o u r c e s that
are
it keeps track of all methods -

handled using recovery n


detected, it c a n be are terminated.
This approach is
in the deadlock
are involved
All the processes that
all the progress made by the processes is destroyed.
that useful as deadloo
to others until the
can be pre-empted from s o m e processes and given
.Resources

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

dDreviously, all four of the prerequisite conditions be met for a deadlock to


must
me rrence of a deadlock can be avoided by making sure one of the conditions
oCTur
he
Here, we have expanded on this technique
doesn't
hold true. by individually examining each of the four
prerequisites

.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

Figure 5.8 Hold and Wait Condition


This
S strategy
Strategy
The system requires that all of the resources process will need must be
a
System must grant resources "all or none" basis. on requested at once
requested at

OCess is not currently available, then the set If the


process complete
must set of
wait until of
theresources
completeneede
resoura sset is
dilable. While the process waits, however, it it for
Condition is denied and may not hold any resources. Tnus
This strateg deadlocks simply cannot
eEy can lead to serious waste of
occur ten
resources, For
V E S must request and receive all ten derives before it begins executing.
example, a program requts
the
Operating Systems
the remaining tape drives
needs only one tape drive to begin execution and then does not need will sit idle for several
for several hours. Then substantial computer resources (9
tapn).
(starvation). Since
Since not
n otaall the required
i l the

strategy
hours. t'his cause indefinitepostponement
can

resources may become available at once.


ondi
"No-pre-emption" Condition: The non-pre-emption condiOn can be
3. Elimination of immediately be allocated
that cannot
by forcing a process waiting for a
resource
alleviated use

its currently held resources, so


that other processes may them to
to relinquish all of
hold resources
while
requesting aaditional
tinish. Suppose a system does allow processes to satisfied. A process holds
cannot be
Consider what happens when a request hold the
while second process may
resources.
need in order to proceed that when
r e s o u r c e s a second process may
This strategy requires
This is a deadlock.
r e s o u r c e s needed by the first resources. The
process.
a request
for additional
r e s o u r c e s is denied
a process that is holding
s o m e
them again together with
if request
release its held r e s o u r c e s and,
necessary,
process m u s t the "no-pre-emptive condition
of this strategy denies
additional r e s o u r c e s .
Implementation all its work to that point
r e s o u r c e s the process
may lose
release
effectively. When a process of indefinite postponement
this strategy is the possibility releases the
of requests and
One serious c o n s e q u e n c e as it repeatedly
be held off indefinitely
(starvation). A process might
can be
same resources.
condition, the circular wait,
Condition T h e last
"Circular Wait" then forcing, all processes
4 Elimination of
all of the resource types and total
on imposes a
total ordering strategy
decreasing). This
a
denied by imposing or in a
the resources in order (increasing requests
resources

to request that each process


to require
types, and
resource
all resources With this rule, the
ordering of
enumeration.
of
or decreasing) is allocated to P,if
order (increasing that R, resource
numerical Assume
n e v e r have a
can
cycle. Example: will not be granted. Only
allocation graph then such request
that are lesser than R,;
next time P, asks for R, R,that are more
than R, will be granted.

the request for


resources

5.7 Deadlock Avoidance This method


deadlock before it
actually happens. method
deadlock issue
foresees action. This
the and take appropriate
This method of solving a deadlock
the possibility of cannot exist by eliminatin
uses an algorithm to evaluate that deadlock
which guarantees
deadlock prevention,
is different from
systeu
one of its prerequisites. if the resulting state of the
will be granted only the opera
for any
resource
performed by
this method, the request checks every step aters
n in the system. This method
state. Once the system en
aoesn't cause any deadlock is in a safe
until the system
its execution
Any process continues has to take a step back.
unsafe state, the operating system
resource-alloce
Dan assess the
can dynamically
the help d e a d l o c k - a v o i d a n c e algorithm,
you the simplest and un e
of a circular-wait
situation. According to
it
be
state so that there
at there can
ca never

the
a
maximum number
of resources
ofeach type there

any process should declare allocations So i d t


T
Therithms of deadlock avoidance
resource
mainly examine
the
ver be an occurrence of circular wait conditions.
Deadlodko
5.7.1 Safe State and Unsafe State 5.11
Safe State: A state is safe
if the
system can allocate
Tesources to each
process (up to its
requirement) in some order and still avoid a maximum
deadlock.
Formally, system is in a safe state
a
Deadlock unsafe
a safe sequence. S0 a safe state only, if there exists
is not
state and conversely a deadlocked
a
deadlocked Safe
state is an
state. unsafe
Unsafe State: In Unsafe state, the
an
system cannot prevent
processes from
operating
urces in such a way that requesting
any deadlock occurs. It
not necessary that all unsafe is
states are
unsafe state may lead to a deadlocks; an Figure 5.9: Safe, Unsafe, and
deadlock.
We can design avoidance algorithms that Deadlocked State Spaces
ensure the
rancept of a safe state. Simply system will never reach a
The system is initially in a safe
ensuring that the
systems deadlock given the
state. The
are
always in a safe state is the
immediately in response to a request for onesystem must determine if a resource may be objective.
that is allocated
Wait. The allocation is
granted only if the allocation currently available, or whether the
leaves the system in a process must
if a process
requests a resource that is safe state. In this
urtilisation may be lower currently available. It may still
than it would be
scheme,
have to wait. Thus,
without a deadlock resource
avoidance algorithm.
5.7.2

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

resource*di con hecaue


becauseti
n a m e was
chosen
This algorithm is commonly known as the
banker's algorithm.
allocates its available cae
that the bank n e v e r
could be used in a banking c v s t e m to ensure
8 m customers. The
Banker's algorithm is a resoure
it can no longer satisfv the needs of all its
tnat Dijkstra.
allocation and deadlock avaidance alporithm developed by Edsger
maximum number of instances
wnen a new process enters the svstem, it
must declare the
exceed the total number of resourr
e d c h resource that it need. This number may not
type mav

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

structures encode the state of the


resource-allocation system.

Data Structures
(Banker's Algorithm)

Max Allocation Need


Available

Figure 5.11 Data Structures


be the number of resource types
Let nbe the number of processes in the system and m
We need the following data structures:
of each type
Available: a of
vector length m indicates the number of available resources
f Availablej] =k, there arekinstances of resource type Ravailable.
Deadlocks3
5.13
Max:Ar An nxmmatrix defines the
maximum demand of each process. If
ocess Pi may est at most
kinstances of resource type. Max[]=k, then
Allocation: An
A n xm
matrix
ted to each process. lf defines the umber of resources of each type currently
R

instances of resource type R Allocationfij]= k, then process P is currently


allocatedk
dAn
Need: An n xm matrix indicates the
remaining resource need of each
ENNleediil=k, then process P, may need process.
kmore instances of resource type R, to
itstask. complete
Note that Need[ijl =Maxiijl-AllocationjL
Theseace data structures vary over time in both size and value.
S a f e t yA l g o r i t h m

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
=

Finishfi false; for i=1, 2, 3, 4..


such that both
2) Find an i
a) Finish[i]= false
(b) Need <= Work
if no such i exists goto step (4)
3Work=Work +Allocation[i]
Finish[i] = true

goto step (2)


4) if Finish [i] = true for all i

then the system is in a safe state

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

Here are important characteristics of banker's algorithm:


Keep many resources that satisfy the requirement of at least one client
Whenever a process gets all its resources, it needs to return them in a restricted period.
When a process requests a resource, it needs to wait
.The system has a limited number of resources
Advance feature for max resource allocation
5.8 Deadlock Detection
lt asystem does not employ either a deadlock-prevention or deadlock-avoidance algorithm, then
there are chances of occurrence of a deadlock.

Deadlock Detection

Single Instance of each |Multiple Instance of each|


Resource Type Resource Type

Detect Cycle in Safety Algorithm


wait for Graph

Figure 5.12 Deadlock Detection


In this case, the system may provide two things:
An algorithm is used to examines the state of the system in order to determine whether a
deadlock has occurred.
An algorithm that is used to recover from the deadlock.
checks the for any
nus order to get rid of deadlocks the operating system periodically system
deadlock. After Finding the deadlock the operating system will recover from it using recovery
techniques.
Now, the main task of the operating system is to detect the deadlocks and this is done with the heip
of Resource Allocation
Graph.
m
gorithm and the
and the whole prcess is repeated. This algorithm does not assure deadlock
determines the existence of deadlock.
n
instead.it
d u ti n s t e a d
t

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

are terminated to eliminate deadlock. It has two methods as follows,


a one or more processes
all deadlocked processes which will break
Aborting all Deadlocked Processes: Terminate
because there
deadiock immediately, but it is a bit expensive be some
may processes that have
been executing for a long time consuming considerable CPU time and
their termination will
result in wasting those CPU cycles.
Abort one Process at a time until the Elimination of the Deadlock Cycle: In order to
overcome the drawback of the above method, this method terminates one process at a time
until the deadlock is recovered. However, it has some overhead since, after terminating
each process, a detection algorithm has to be executed for deciding to further terminate the
processes or not. This method is slower than the fîrst one.

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.

You might also like