You are on page 1of 46

Deadlock

• The earliest computer operating systems ran only one


program at a time. All of the resources of the system
were available to this one program. Later, operating
systems ran multiple programs at once, interleaving
them. Programs were required to specify in advance
what resources they needed so that they could avoid
conflicts with other programs running at the same
time. Eventually some operating systems offered
dynamic allocation of resources. Programs could
request further allocations of resources after they had
begun running. This led to the problem of the
deadlock. Here is the simplest example:
• Program 1 requests resource A and receives it.
• Program 2 requests resource B and receives
it.
• Program 1 requests resource B and is queued
up, pending the release of B.
• Program 2 requests resource A and is queued
up, pending the release of A.
• Now neither program can proceed until the other
program releases a resource. The operating
system cannot know what action to take. At this
point the only alternative is to abort (stop) one of
the programs.
• Learning to deal with deadlocks had a major
impact on the development of operating systems
and the structure of databases. Data was
structured and the order of requests was
constrained in order to avoid creating deadlocks.
Direction of arrow R is assign to p
means process is accessing resourse R
P3 request for R2. p1 and p2 is using R3 but P1 is waiting for R1. p2 is waiting
for R2 so they will not release R3 until they will finish their task so p3 has to

wait this cycle is infinite .


• Deadlock Characteristics
Conditions for Deadlock
Deadlock Prevention
Deadlock Prevention
• Deadlocks can be prevented by preventing at least one of the four required
conditions:
• 7.4.1 Mutual Exclusion
• A Resource may be acquired exclusively by only one process at a time.
• Shared resources such as read-only files do not lead to deadlocks.
• Unfortunately some resources, such as printers and tape drives, require exclusive
access by a single process. No other process can use it while it is allocated.
• 7.4.2 Hold and Wait
• To prevent this condition processes must be prevented from holding one or more
resources while simultaneously waiting for one or more others. There are several
possibilities for this:

– Require that all processes request all resources at one time. This can be wasteful of
system resources if a process needs one resource early in its execution and doesn't
need some other resource until much later.
– Require that processes holding resources must release them before requesting
new resources, and then re-acquire the released resources along with the new
ones in a single new request. This can be a problem if a process has partially
completed an operation using a resource and then fails to get it re-allocated after
releasing it.
– Either of the methods described above can lead to starvation if a process requires
one or more popular resources.
• 7.4.3 No Preemption
• Preemption of process resource allocations can prevent this condition of
deadlocks, when it is possible.
– One approach is that if a process is forced to wait when requesting a
new resource, then all other resources previously held by this process
are implicitly released, ( preempted ), forcing this process to re-
acquire the old resources along with the new resources in a single
request, similar to the previous discussion.
– Another approach is that when a resource is requested and not
available, then the system looks to see what other processes currently
have those resources and are themselves blocked waiting for some
other resource. If such a process is found, then some of their
resources may get preempted and added to the list of resources for
which the process is waiting.
– Either of these approaches may be applicable for resources whose
states are easily saved and restored, such as registers and memory,
but are generally not applicable to other devices such as printers and
tape drives.
• 7.4.4 Circular Wait
• One way to avoid circular wait is to number all
resources, and to require that processes request
resources only in strictly increasing ( or
decreasing ) order.
• In other words, in order to request resource Rj, a
process must first release all Ri such that i >= j.
• One big challenge in this scheme is determining
the relative ordering of the different resources
Deadlock Avoidance
Deadlock Avoidance
• If the necessary conditions for a deadlock are
in place, it is still possible to avoid deadlock by
being careful when resources are allocated.
Perhaps the most famous deadlock avoidance
algorithm, due to Dijkstra , is the Banker’s
algorithm. So named because the process is
analogous to that used by a banker in deciding
if a loan can be safely made.
• It is important to note that an unsafe state does
not imply the existence or even the eventual
existence a deadlock. What an unsafe state does
imply is simply that some unfortunate sequence
of events might lead to a deadlock.
• The Banker's algorithm is thus to consider each
request as it occurs, and see if granting it leads to
a safe state. If it does, the request is granted,
otherwise, it postponed until later.
• Banker's algorithm is a deadlock avoidance algorithm. It is
named so because this algorithm is used in banking
systems to determine whether a loan can be granted or
not.
• Consider there are n account holders in a bank and the sum
of the money in all of their accounts is S. Everytime a loan
has to be granted by the bank, it subtracts the loan amount
from the total money the bank has. Then it checks if that
difference is greater than S. It is done because, only then,
the bank would have enough money even if all the n
account holders draw all their money at once.
• Banker's algorithm works in a similar way in computers.
Whenever a new process is created, it must exactly specify
the maximum instances of each resource type that it needs.
Deadlock Avoidance
Banker’s Algorithm
• Banker's Algorithm
• Banker's algorithm is a deadlock avoidance algorithm.
It is named so because this algorithm is used in banking
systems to determine whether a loan can be granted or
not.
• Consider there are n account holders in a bank and the
sum of the money in all of their accounts is S.
Everytime a loan has to be granted by the bank, it
subtracts the loan amount from the total money the
bank has. Then it checks if that difference is greater
than S. It is done because, only then, the bank would
have enough money even if all the n account holders
draw all their money at once.
• Banker's algorithm works in a similar way in
computers. Whenever a new process is created, it must
exactly specify the maximum instances of each
resource type that it needs.
Deadlock Detection
•Wait For Graph
• Wait-For-Graphs
• A Wait-For Graph (WFG) is a graph whereEach
node represents a process;
• An edge, Pi --< Pj means that Pi is blocked
waiting for Pj to release a resource.
• Recall the single-instance SRAG given
earlier A WFG can be derived from that
graph.
• The cycles can be seen more clearly in this form
of the graph. There is deadlock in the system if
and only if there exists a cycle or a knot in the
wait-for-graph.
• With about half the number of nodes and edges,
the WFG requires only about half the storage of a
SRAG.
• However, WFGs can only be used for SRAGs with
single-instance resources. Resources for SRAGs
can have any number of instances; However,
one cannot create WFGs from these.
Deadlock Recovery

You might also like