Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views42 pages

Deadlock Goal#8

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 42

Deadlocks

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

• Resources can be reusable or consumable. They are reusable if


they can be used again after a process is done using them.
Memory, printers, tape drives(a device that stores computer
data on magnetic tape, especially for backup and archiving
purposes) are examples of reusable resources.
• Consumable resources are resources that can be used only once.
• One that can be created ( produced) and destroyed
(consumed). Example, signals , messages
A process may utilize a resource in only
the following sequence:
1. Request. The process requests the resource. If the request
cannot be granted immediately (for example, if the
resource is being used by another process), then the
requesting process must wait until it can acquire the
resource.
2. Use. The process can operate on the resource (for example,
if the resource is a printer, the process can print on the
printer).
3. Release. The process releases the resource.
These resources may be either physical or logical resources.
A deadlock situation can arise if
the following four
conditions hold simultaneously in
a system:
Deadlock 1. Mutual exclusion.
Necessary 2. Hold and wait.
Conditions 3. No preemption.
4. Circular wait.
Cont...
Mutual Exclusion
At least one resource must be held in a non-sharable mode; that is, only one
process at a time can use the resource. If another process requests that
resource, the requesting process must be delayed until the resource has
been released.
Hold and wait.
A process must be holding at least one resource and waiting to acquire
additional resources that are currently being held by other processes.
Cont...
No preemption.
Resources cannot be preempted; that is, a resource can be released
only voluntarily by the process holding it, after that process has
completed its task.
Circular wait.
A set {P0, P1, ..., Pn} of waiting processes must exist such that P0 is
waiting for a resource held by P1, P1 is waiting for a resource held by
P2, ..., Pn−1 is waiting for a resource held by Pn, and Pn is waiting for
a resource held by P0
Resource-
Allocation Graph
• Deadlocks can be described more precisely in
Resource- terms of a directed graph called a system
resource-allocation graph.
Allocation • This graph consists of a set of vertices V and a set
Graph of edges E.

• A directed edge from process Pi to resource type


Rj is denoted by Pi → Rj
• A directed edge from resource type Rj to process
Pi is denoted by Rj → Pi
• A directed edge Pi → Rj is called a request edge; a
directed edge Rj → Pi is called an assignment
edge.
The resource-
allocation
graph shown in
Figure depicts
the following
situation.
• The sets P, R, and
E:
o P = {P1, P2, P3}
o R = {R1, R2, R3, R4}
o E = {P1 → R1, P2 → R3,
R1 → P2, R2 → P2, R2 →
P1, R3 → P3}
Cont...

• 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

Deadlock detection and recovery


Methods for Handling Deadlocks

Generally speaking, we can deal with the deadlock problem in one


of three ways:
• We can use a protocol to prevent or avoid deadlocks, ensuring
that the system will never enter a deadlocked state
(Deadlock prevention and Deadlock avoidance scheme).
• We can allow the system to enter a deadlocked state, detect it,
and recover.
• We can ignore the problem altogether and pretend that
deadlocks never occur in the system.
Deadlock prevention vs Deadlock avoidance

Deadlock prevention provides a set of methods to ensure


that at least one of the necessary conditions cannot hold.
These methods prevent deadlocks by constraining how
requests for resources can be made.
Deadlock avoidance requires that the operating system be
given additional information in advance concerning which
resources a process will request and use during its lifetime.
With this additional knowledge, the operating system can
decide for each request whether or not the process should
wait. To decide whether the current request can be satisfied
or must be delayed, the system must consider the resources
currently available, the resources currently allocated to each
process, and the future requests and releases of each
process
Deadlock Prevention
A deadlock to occur, each of the four necessary conditions must hold.
By ensuring that at least one of these conditions cannot hold, we can
prevent the occurrence of a deadlock.
Mutual Exclusion
The mutual exclusion condition must hold. we cannot prevent
deadlocks by denying the mutual-exclusion condition.
Cont...
Hold and Wait
• To ensure that the hold-and-wait condition never occurs in the system, we must
guarantee that, whenever a process requests a resource, it does not hold any
other resources.
• One protocol that we can use requires each process to request and be allocated
all its resources before it begins execution.
• An alternative protocol allows a process to request resources only when it has
none. A process may request some resources and use them. Before it can request
any additional resources, it must release all the resources that it is currently
allocated.
Disadvantages
• Both these protocols have two main disadvantages.
• First, resource utilization may be low, since resources may be allocated but
unused for a long period.
• Second, starvation is possible. A process that needs several popular
resources may have to wait indefinitely, because at least one of the
resources that it needs is always allocated to some other process.
No preemption
• The third necessary condition for deadlocks is that there be no
preemption of resources that have already been allocated.
• To ensure that this condition does not hold, we can use the
following protocol.
• If a process is holding some resources and requests another
resource that cannot be immediately allocated to it (that is, the
process must wait), then all resources the process is currently
holding are preempted.
Another way

• 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

deadlock- • Resource-Allocation-Graph Algorithm

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

You might also like