Professional Documents
Culture Documents
Deadlock
Deadlock
– 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