Deadlock Goal#8
Deadlock Goal#8
Deadlock Goal#8
Chap#7
CHAPTER OBJECTIVES
• To develop a description of deadlocks, which
prevent sets of concurrent processes from
completing their tasks.
• To present a number of different methods for
preventing or avoiding deadlocks in a computer
system.
Goal # 8
• Concept and Definition
• Consumable and Reusable Resources
• Conditions for Deadlock
• Deadlock Representation
• Resource Allocation Graph
• Assignment and Request Edges
• Methods to Handle Deadlock
• Deadlock Prevention
• Deadlock Avoidance
• Safe state and Safe sequence
Deadlock
A process requests resources; 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.
Consumable and Reusable Resources
• Resource instances:
◦ One instance of resource type R1
◦ Two instances of resource type R2
◦ One instance of resource type R3
◦ Three instances of resource type R4
• Process states:
◦ Process P1 is holding an instance of resource type R2 and is waiting for an instance of resource
type R1.
◦ Process P2 is holding an instance of R1 and an instance of R2 and is waiting for an instance of R3.
◦ Process P3 is holding an instance of R3
• Given the definition of a resource-
allocation graph, it can be shown
that, if the graph contains no cycles,
then no process in the system is
deadlocked. If the graph does
contain a cycle, then a deadlock
may exist.
• Suppose that process P3 requests
an instance of resource type R2.
Since no resource instance is
currently available, we add a
request edge P3 → R2 to the graph
(Figure 7.2).
• At this point, two minimal cycles
exist in the system:
• P1 → R1 → P2 → R3 → P3 → R2 →
P1
• P2 → R3 → P3 → R2 → P2
Explaination
• Processes P1, P2, and P3 are deadlocked.
• Process P2 is waiting for the resource R3, which is held by process P3.
• Process P3 is waiting for either process P1 or process P2 to release resource R2.
• In addition, process P1 is waiting for process P2 to release resource R1.
• Now consider the resource-allocation graph in Figure 7.3. In this example, we also have a
cycle:
• P1 → R1 → P3 → R2 → P1
Example
Cont...
• However, there is no deadlock. Observe that process P4 may
release its instance of resource type R2. That resource can then
be allocated to P3, breaking the cycle.
• In summary, 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.
This observation is important when we deal with the deadlock
problem.
Deadlock handling methods
Deadlock Ignorance
Deadlock prevention
Deadlock avoidance
• Alternatively, if a process requests some resources, we first check whether they are
available. If they are, we allocate them.
• If they are not, we check whether they are allocated to some other process that is waiting
for additional resources.
• If so, we preempt the desired resources from the waiting process and allocate them to the
requesting process.
• If the resources are neither available nor held by a waiting process, the requesting process
must wait. While it is waiting, some of its resources may be preempted, but only if
another process requests them.
• A process can be restarted only when it is allocated the new resources it is requesting and
recovers any resources that were preempted while it was waiting.
Circular Wait
• The fourth and final condition for deadlocks is the circular-wait
condition.
• One way to ensure that this condition never holds is to impose a total
ordering of all resource types and to require that each process
requests resources in an increasing order of enumeration.
Drawbacks of Deadlock
prevention
• Deadlock-prevention algorithms, as discussed prevent deadlocks by
limiting how requests can be made. The limits ensure that at least one
of the necessary conditions for deadlock cannot occur.
• Possible side effects of preventing deadlocks by this method,
however, are low device utilization and reduced system throughput.
Deadlock Avoidance
• The simplest and most useful model requires that each process
declare the maximum number of resources of each type that it may
need.
• A deadlock-avoidance algorithm dynamically examines the resource-
allocation state to ensure that a circular-wait condition can never
exist.
• The resource-allocation state is defined by the number of available
and allocated resources and the maximum demands of the processes.
In the following
sections, we
explore two • Safe State
avoidance
algorithms
Safe state
• A state is safe if the system can allocate resources to each process (up
to its maximum) in some order and still avoid a deadlock.
• More formally, a system is in a safe state only if there exists a safe
sequence.
• A sequence of processes is a safe sequence for the current allocation
state if, for each Pi , the resource requests that Pi can still make can be
satisfied by the currently available resources plus the resources held
by all Pj , with j < i.
Cont...
• In this situation, if the resources that Pi needs are not immediately
available, then Pi can wait until all Pj have finished. When they have
finished, Pi can obtain all of its needed resources, complete its
designated task, return its allocated resources, and terminate. When
Pi terminates, Pi+1 can obtain its needed resources, and so on. If no
such sequence exists, then the system state is said to be unsafe
Safe state
• A safe state is not a
deadlocked state.
• An unsafe state may lead to
a deadlock.
Example
To illustrate, we consider a system with twelve magnetic tape
drives and three processes: P0, P1, and P2. Process P0 requires ten
tape drives, process P1 may need as many as four tape drives, and
process P2 may need up to nine tape drives. Suppose that, at time
t0, process P0 is holding five tape drives, process P1 is holding two
tape drives, and process P2 is holding two tape drives. (Thus, there
are three free tape drives.)
Resource-Allocation-Graph
Algorithm
• If we have a resource-allocation system with only one instance of
each resource type.
• claim edge:
A claim edge Pi → Rj indicates that process Pi may request resource
Rj at some time in the future. This edge resembles a request edge in
direction but is represented in the graph by a dashed line.
• When process Pi requests resource Rj , the claim edge Pi → Rj is
converted to a request edge.
• Similarly, when a resource Rj is released by Pi , the assignment edge Rj
→ Pi is reconverted to a claim edge Pi → Rj .
Cont...
• Suppose that P2 requests
R2. Although R2 is
currently free, we cannot
allocate it to P2, since this
action will create a cycle
in the graph
Banker’s Algorithm
• The resource-allocation-graph algorithm is not applicable to a resource
allocation system with multiple instances of each resource type.
• The deadlock avoidance algorithm that we describe next is applicable to
such a system but is less efficient than the resource-allocation graph
scheme.
• This algorithm is commonly known as the banker’s algorithm.
• The name was chosen because the algorithm could be used in a banking
system to ensure that the bank never allocated its available cash in such
a way that it could no longer satisfy the needs of all its customers.
• When a new process enters
the system, it must declare
the maximum number of
instances of each resource
type that it may need.
Cont...
• Available = Available–Requesti ;
• Allocationi = Allocationi + Requesti ;
• Needi = Needi –Requesti ;
Deadlock
Detection
• An algorithm that
examines the state of the
system to determine
whether a deadlock has
occurred
• An algorithm to recover
from the deadlock
Several instances of a resource
type
• Similar to banker's algorithm
Recovery from deadlock
Process Termination
• Abort all deadlocked processes
• Abort one process at a time until the deadlock cycle is eliminated.
Resource Preemption
• Selecting the victim
• Rollback
• Starvation
Thank you