Chapter - 3
Chapter - 3
three
Deadlock
Management
1
Outlin
e
• Deadlock: definition
• Conditions for Deadlock
• Deadlock examples
• Starvation
• Resource types
• Methods for handling
deadlock
- Ostrich algorithm
- Deadlock detection and
recovery
- Deadlock prevention
- Deadlock avoidance 2
Deadlo
• cka process needs exclusive
For many applications,
access to not one resource, but several.
• Suppose, for example, two processes each want to
record a scanned document on a CD.
– Process A requests permission to use the scanner and is
granted it.
– Process B is programmed differently and requests the CD
recorder first and is also granted it.
• Now A asks for the CD recorder, but the request is
denied until B releases it.
• Unfortunately, instead of releasing the CD recorder B
asks for the scanner.
• At this point both processes are blocked and will
remain so forever.
3
• This situation is called a deadlock.
Deadlock
• …defined formally as
Deadlock can be
follows:
6
Existence of
Deadlock
• Mutual
Exclusion
• No
preemption
• Hold and wait
• Circular wait
7
Deadlock
• examples
Examples
– studying students
– traffic intersection
– airline reservation system…
• evaluation
– four conditions: mutual exclusion, hold and
wait, no preemption, circular wait
Studying
studying Students
students: bothstudents need the
textbook and the course notes to study, but
there is only one copy of each
consider the following situation:
Student A Student B
get get textbook
coursenotes get
get textbook coursenotes
study study
release release
textbook coursenotes
release release
coursenotes textbook
Students
• Evaluation
mutual exclusion
– books and course notes can be used only
by one student
• hold and wait
– a student who has the book waits for the course
notes, or vice versa
• no preemption
– there is no authority to take away book or
course notes from a student
• circular wait
– student A waits for resources held by student
B, who waits for resources held by A
Traffic
• Intersection
at a four-way intersection, four cars
arrive simultaneously
• if all proceed, they will be stuck in
the middle
Traffic
• Evaluation
mutual exclusion
– cars can’t pass each other in the intersection
• hold and wait
– vehicles proceed to the center, and wait for
their path to be clear
• no preemption
– there is no authority to remove some vehicles
• circular wait
– vehicle 1 waits for vehicle 2 to move, which
waits for 3, which waits for 4, which waits for 1
Starvati
• a process can’ton
proceed because other
processes always have the resources it
needs
• the request of the process is never
satisfied
• in principle, it is possible to get the
resource, but doesn’t happen because
of
– low priority of the process
– timing of resource requests
– ill-designed resource allocation or
scheduling algorithm
• different from deadlock
Examples
Starvation
• batch processes with low priority in a
heavily used time-sharing system
• crossing a very busy road
• trying to call a very popular phone
number
– radio station giveaways
• getting into a very popular course with
limited enrollment
Solution
Starvation
• fairness: each process gets its fair share
of all resources it requests
– how is fair defined?
– how is it enforced?
• aging
– the priority of a request is increased the
longer the process waits for it
Starvation vs
•
Deadlock
– Starvation: process waits indefinitely
Example, low-priority process waiting for
resources constantly in use by high-priority
processes
– Deadlock: circular waiting for resources
• Process A owns Res 1 and is waiting for Res
2
Process
• Process B owns Res 2 and is Wait
waiting for Res
Owned A For
1 By
Res 1 Res 2
Owned
Wait Process By
For B
– Deadlock Starvation but not vice
versa
• Starvation can end (but doesn’t have to)
• Deadlock can’t end without external
intervention
Resource
Types
• reusable resources
– can be used repeatedly by different
processes
– does not imply simultaneous use
– OS examples: CPU, main memory, I/O
devices, data structures
• consumable resources
– are produced by one entity, and
consumed by another
– reuse is not possible, or impractical
– OS examples: messages
Resource-Allocation
• GraphModel
System Symbol
s
– A set of Processes P1, P2, . . ., Pn P1 P2
P1 P2 P3
P1 P2 P3
P1 P3
R3 P4
R3 R2
R4
R4
20
Methods for Handling Deadlocks
• Ensure that the system will never
enter a deadlock state.
– have strict rules that prevent a
deadlock from occurring
25
Deadlock Detection and
Recovery
Detection Algorithms
– Deadlock Detection with One Resource of Each
Type
– Deadlock Detection with Multiple Resources of
Each Type
26
Deadlock Detection with One
Resource of Each
Type
• Assume that only one resource of
each type exists.
– Such a system might have one scanner,
one CD recorder, one plotter, and one
tape drive.
• We can construct a resource graph.
• If this graph contains one or more cycles, a
deadlock exists.
• Any process that is part of a cycle is
deadlocked.
• If no cycles exist, the system is not 27
deadlocked.
Deadlock Detection with One
Resource of Each
Type…
• Maintain wait - for graph
– Nodes are processes
– Pi Pj if Pi is waiting for Pj
• What is next?
33
Deadlock
Prevention
• set of rules ensures that at least one of
the four necessary conditions for
deadlock doesn’t hold
– mutual exclusion
– hold and wait
– no preemption
– circular wait
36
Deadlock
Avoidance
• Basic Principle: Requires that the system
has some additional a priori information
available
• Simplest and most useful model requires that each
process declare the maximum number of
resources of each type that it may need to
hold simultaneously. (maximum demand)
• Algorithms
– Safe and Unsafe States
– The Bankers algorithm
38
Safe
• statean available resource,
When a process requests
system must decide if immediate allocation leaves
the system in a safe state
39
Basic
• If a system is in Facts
safe state no deadlocks
• If a system is in unsafe state possibility of
deadlock
unsafe
Deadlock
safe
41
Deadlock Avoidance
Algorithms
• Single instance of a resource
type
– Use a resource-allocation graph
42
Safe State Space
unsafe
Safe
deadlock
43
Bank Safe State
Space
Saf
e
deadlock
44
The Banker’s
Algorithm
• before a request is granted, check the
system’s state
– assume the request is granted
– if it is still safe, the request can be honored
– otherwise the process has to wait
– overly careful
• there are cases when the system is unsafe, but
not in a deadlock
45
Combined Approach to
Deadlock
Handling
• Combine the three basic approaches
– prevention
– avoidance
– detection
allowing the use of the optimal approach for
each of resources in the system.