0% found this document useful (0 votes)
2 views

Chapter - 3

The document discusses deadlock management in computing, defining deadlock, its conditions, and examples. It outlines methods for handling deadlocks, including prevention, avoidance, detection, and recovery strategies. Additionally, it differentiates between starvation and deadlock, and describes resource types and allocation graphs relevant to deadlock scenarios.

Uploaded by

salihking090912
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Chapter - 3

The document discusses deadlock management in computing, defining deadlock, its conditions, and examples. It outlines methods for handling deadlocks, including prevention, avoidance, detection, and recovery strategies. Additionally, it differentiates between starvation and deadlock, and describes resource types and allocation graphs relevant to deadlock scenarios.

Uploaded by

salihking090912
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 46

Chapter -

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:

– A set of processes is deadlocked if each


member of the set of deadlocked processes
is waiting for a resource that is owned by a
deadlocked process.

– None of the processes can run,


– none of them can release any resources, and
– all the processes continue to wait forever.
4
Conditions for

Deadlock
Four conditions must hold for there to be a
deadlock:
1. Mutual exclusion condition. Each resource is either currently
assigned to exactly one process or is available.
2. Hold and wait condition. Processes currently holding
resources granted earlier can request new resources.
3. No preemption condition. Resources previously granted
cannot be forcibly taken away from a process. They must
be explicitly released by the process holding them.
4. Circular wait condition. There must be a circular chain of two
or more processes, each of which is waiting for a resource
held by the next member of the chain.
• All four of these conditions must be present for a
deadlock to occur.
5
• If one of them is absent, no deadlock is possible.
Possibility of
Deadlock
• Mutual
Exclusion
• No
preemption
• Hold and wait

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

– Resource types R1, R2, . . ., Rm


CPU cycles, memory space, I/O devices
R1
– Each resource type Ri has Wi
R2
instances.
– Each process utilizes a resource as
follows:
• Request() / Use() / Release()
• Resource-Allocation
• P = {P1, P2, …, PnGraph:
}, the set processes in the
– V is
system.
partitioned into two types:
• R = {R1, R2, …, Rm}, the set of resource types in
system
– request edge – directed edge Pi  Rj
– assignment edge – directed edge R  P
Resource Allocation Graph
• Recall:
Examples
– request edge – directed edge P  1
Rj
–R1 assignment
R2 edge – directed edge Rj
R1 R2 R1
 Pi P2

P1 P2 P3
P1 P2 P3
P1 P3

R3 P4
R3 R2
R4
R4

Simple Allocation Allocation


Resource Graph With Graph With
Allocation Deadlock Cycle, but No
Graph Deadlock
Basic
Facts
• If graph contains no cycles  no
deadlock.

• If graph contains a cycle 


– if only one instance per resource type,
then deadlock.
– if several instances per resource type,
possibility of deadlock.

20
Methods for Handling Deadlocks
• Ensure that the system will never
enter a deadlock state.
– have strict rules that prevent a
deadlock from occurring

• Allow the system to enter a deadlock


state and then recover.

• Ignore the problem and pretend that


deadlocks never occur in the system;
frequently used by most operating
systems, like
• Windows, MacOS, Unix, ... 21
What kind of
• resources?
Resources come in two types:
– Pre-emptible and non pre-emptible.

• A pre-emptible resource is one that can be


taken away from the process owning it with
no ill effects.
– Memory is an example of a pre-emptible
resource.
• A nonpreemptable resource, in contrast,
is one that cannot be taken away from its
current owner without causing the
computation to fail.
22
– Burning file into CD
What kind of
resources?
• Potential deadlocks that involve pre-
emptible resources can usually be
resolved:
– by reallocating resources from one process
to another.

• Thus our treatment will focus on


nonpreemptable resources:
• The sequence of events required to use a
resource is given below in an abstract form.
1. Request the resource.
2. Use the resource. 23

3. Release the resource.


Four strategies are used for dealing
with deadlocks:
1 Just ignore the problem altogether - Maybe
if you ignore it, it will ignore you.

2 Detection and recovery - Let deadlocks occur,


detect them, and take action.

3 Dynamic avoidance by careful resource


allocation.

4 Prevention - by structurally negating one of


the four conditions necessary to cause a
deadlock.
24
THE OSTRICH
ALGORITHM
• The simplest approach the ostrich algorithm:
• stick your head in the sand and pretend
there is no problem at all.
– Mathematicians find it totally unacceptable and
say that dead locks must be prevented at all
costs.
– Engineers ask how often the problem is expected,
how often the system crashes for other reasons, and
how serious a deadlock is.

25
Deadlock Detection and
Recovery
Detection Algorithms
– Deadlock Detection with One Resource of Each
Type
– Deadlock Detection with Multiple Resources of
Each Type

Recovery from Deadlock


– Recovery through Preemption
– Recovery through Rollback
– Recovery through Killing Processes

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

• Periodically invoke an algorithm that


searches for a cycle in the graph.
• If there is a cycle, there exists a
deadlock.

• An algorithm to detect a cycle in a


graph requires an order of n 2 28
operations, where n is the number of
vertices in the graph
Exampl Answer
Construct wait for
e• Consider a system with graph
seven processes, A R A B
though G, and six
resources, R through W. C S D T E
• The state are as follows:
1. Process A holds R and wants F U V
S.
2. Process B holds nothing but W G
wants T.
3. Process C holds nothing but
wants S. Process D,E, and G
4. Process D holds U and wants are deadlocked
S and T.
Questio
5.
n Process E holds T and wants
• "IsV. this system deadlocked, and 41
if so, which processes are
6. involved?”
Process F holds W and wants
S.
Recovery from
Deadlock
• Suppose that our deadlock detection
algorithm has succeeded and detected
a deadlock.

• What is next?

• Some way is needed to recover and


get the system going again.

• In this section, we will discuss various


ways of recovering from deadlock.
30
Recovery from
Deadlock…
• Roll back each deadlocked process to
some previously defined checkpoint,
and restart all process
– Original deadlock may occur

• Successively kill deadlocked


processes until deadlock no longer
exists

• Successively preempt resources until


deadlock no longer exists
31
Recovery from Process
Deadlock: Termination
• Abort all deadlocked processes.
• Abort one process at a time until the
deadlock cycle is eliminated.

• In which order should we choose to


abort?
– Priority of the process.
– How long process has computed, and how much
longer to completion.
– Resources the process has used.
– Resources process needs to complete.
– How many processes will need to be terminated.
32
– Is process interactive or batch?
Recovery from Deadlock: Resource Preemption

• Selecting a victim – minimize cost.

• Rollback – return to some safe state,


restart process for that state.

• Starvation – same process may always


be picked as victim, include number of
rollback in cost factor.

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

• may result in low resource utilization,


reduced system throughput
34
Deadlock
Prevention…
1. Prevent the circular-wait condition by defining a linear
ordering of resource types
• A process can be assigned resources only according to
the linear ordering (e.g., sequence number)
• Disadvantages
- Resources cannot be requested in the order that are
needed
- Resources will be longer than necessary

2. Prevent the hold-and-wait condition by requiring the


process to acquire all needed resources before starting
execution
• Disadvantages
– Inefficient use of resources
– Reduced concurrency
– Process can become deadlocked during the initial
35
resource acquisition
– Future needs of a process cannot be always
predicted
Deadlock
Prevention…
3. Denying No Preemption
• means that processes may be preempted by the OS
– should only done when necessary
• resources of a process trying to acquire another
unavailable resource may be preempted
• preempt resources of processes waiting for
additional resources, and give some to the
requesting process
• possible only for some types of resources
– state must be easily restorable
– e.g. CPU, memory

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)

• The deadlock-avoidance algorithm dynamically


examines the resource-allocation state to ensure
that there can never be a circular-wait condition.

• Resource-allocation state is defined by the


number of available and allocated resources,
and the maximum demands of the processes 37
Deadlock

Avoidance…
The system must be able to decide whether
granting a resource is safe or not and only
make the allocation when it is safe.
• Thus, the question arises: Is there an algorithm
that can always avoid deadlock by making the
right choice all the time?
• The answer is a qualified yes-we can avoid
deadlocks.

• 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

• 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.

• We are considering a worst-case situation here.


• Even in the worst case (process requests up
their maximum at the moment), we don’t have
deadlock 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

• Avoidance  ensure that a system will never


enter an unsafe state.
– When a request is done by a process for
some resource(s):
– check before allocating resource(s);
– if it will leave the system in an unsafe state,
then do not allocate the resource(s);
– process is waited and resources are not allocated
to that process. 40
Safe State
• Space
if a system is in a safe state there are no
deadlocks
• in an unsafe state, there is a
possibility of deadlocks

unsafe
Deadlock
safe

41
Deadlock Avoidance
Algorithms
• Single instance of a resource
type
– Use a resource-allocation graph

• Multiple instances of a resource


type
– Use the banker’s algorithm

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.

• Partition resources into hierarchically ordered


classes.

• Use most appropriate technique for handling


deadlocks within each class.
46

You might also like