Академический Документы
Профессиональный Документы
Культура Документы
Outline:
Deadlock problem
System Model
Deadlock Characterization
Methods for Handling Deadlocks
Deadlock Prevention
Deadlock Avoidance
Deadlock Detection
Recovery from Deadlock
Combined Approach to Deadlock Handling
8.1
The Deadlock Problem
P0 P1
wait (A); wait(B)
wait (B); wait(A)
8.2
Bridge Crossing Example
8.3
System Model
8.4
Deadlock Characterization
Deadlock can arise if four conditions hold simultaneously.
8.6
Resource-Allocation Graph (Cont.)
Process
Pi requests instance of Rj
Pi
Rj
Pi is holding an instance of Rj
Pi
Rj
8.7
Example of a Resource Allocation Graph
8.8
Resource Allocation Graph With A Deadlock
8.9
Resource Allocation Graph With A Cycle But No Deadlock
8.10
Basic Facts
8.11
Methods for Handling Deadlocks
Three ways
Ensure that the system will never enter a deadlock state.
8.12
Methods for Handling Deadlocks
8.13
Deadlock Prevention
8.14
Deadlock Prevention
Restrain the ways request can be made.
8.16
Deadlock Prevention (Cont.)
No Preemption –
Protocol: If a process that is holding some resources
requests another resource that cannot be immediately
allocated to it, then all resources currently being held are
released.
Preempted resources are added to the list of resources for
which the process is waiting.
Process will be restarted only when it can regain its old
resources, as well as the new ones that it is requesting.
Possible for CPU registers and memory space whose state
can be restored later, but not possible for printers and tape
drives.
8.17
Deadlock Prevention (Cont.)
Circular Wait –
F:RN is a one-to-one function, where N is a set of natural
numbers.
Protocol 1: impose a total ordering of all resource types,
and require that each process requests resources in an
increasing order of enumeration.
A process initially can request any number of instances
of type Ri. Then the process can request instances of a
resource type Rj if and only if F(Rj) >F(Ri).
Protocol 2: Whenever a process requests an instance of
resource type Rj, it has released any other resources Ri
such that F(Ri) >=F(Rj).
Ordering
8.18
Deadlock Avoidance
Deadlock prevention algorithms
Low device utilization, and reduced system throughput.
Alternative method is get additional information about the
processes
Resources currently available, resources currently allocated
to a process, and future requests and releases of each
process.
Various algorithms differ about amount and type of
information required.
8.19
Deadlock Avoidance
8.20
Safe State
When a process requests an available resource, system must
decide if immediate allocation leaves the system in a safe state.
Sequence <P1, P2, …, Pn> is safe if for each Pi, the resources
that Pi can still request can be satisfied by currently available
resources + resources held by all the Pj, with j<I.
If Pi resource needs are not immediately available, then Pi can wait
until all Pj have finished.
When Pj is finished, Pi can obtain needed resources, execute,
return allocated resources, and terminate.
When Pi terminates, Pi+1 can obtain its needed resources, and so
on.
8.21
Basic Facts
8.22
Safe, Unsafe , Deadlock State
8.23
Example
Consider a system with 12 magnetic tape drives and three processes
P0,P1,P2.
P0 requires 10 tape drives, P1 may need 4, and P2 may need up to 9
tape drives.
Suppose at time t0, P0 is holding 5 tape drives, P1 holding 2, and P2 is
holding 2 tape drives.
There are 3 free tape drives
8.25
Resource-Allocation Graph Algorithm
If we have a resource-allocation (RA) system with one instance of
each resource type, we can use this approach.
Claim edge Pi Rj indicated that process Pj may request resource
Rj; represented by a dashed line.
Claim edge converts to request edge when a process requests a
resource.
When a resource is released by a process, assignment edge
reconverts to a claim edge.
Resources must be claimed a priori in the system.
Before the process starts executing, all its claim edges must appear in
the RA graph.
Suppose a process Pi requests a resource Rj. The request can be
granted if converting the request edge Pi Rj to an assignment
edge Rj Pi does not result in the formation of a cycle in the RA
graph.
8.26
Resource-Allocation Graph Algorithm
Cycle detection algorithm is used to detect a cycle.
If there are n processes detecting a cycle requires an order of n*n
operations.
8.27
Resource-Allocation Graph For Deadlock Avoidance
Suppose P2 requests R2
8.28
Unsafe State In Resource-Allocation Graph
Multiple instances.
8.30
Data Structures for the Banker’s Algorithm
Let n = number of processes, and m = number of resources types.
8.31
Resource-Request Algorithm for Process Pi
8.32
Safety Algorithm
8.33
Example of Banker’s Algorithm
8.34
Example (Cont.)
8.35
Example P1 Request (1,0,2) (Cont.)
8.36
Deadlock Detection
Detection algorithm
Resource allocation graph algorithm
Detection algorithm (similar to Banker’s algoritm)
Recovery scheme
8.37
Single Instance of Each Resource
Graph based approach
Maintain wait-for graph
Nodes are processes.
Pi Pj if Pi is waiting for Pj.
8.38
Resource-Allocation Graph and Wait-for Graph
8.39
Several Instances of a Resource Type
8.40
Detection Algorithm
8.41
Detection Algorithm (Cont.)
8.42
Example of Detection Algorithm
8.43
Example (Cont.)
8.44
Detection-Algorithm Usage
8.45
Recovery from Deadlock: Process Termination
Several alternatives exist.
Inform the operator
Automatic: system will recover from the deadlock automatically
Two options exist for automatically breaking a deadlock
Process termination
Resource preemption
8.46
Recovery from Deadlock: Process Termination
Abort all deadlocked processes.
More expensive
Abort one process at a time until the deadlock cycle is eliminated.
Overhead: after the abort of each process detection algorithm have to be
invoked.
Aborting a process is not easy.
Printing or updating a file
In which order should we choose to abort? (should incur minimum cost)
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.
Is process interactive or batch?
8.47
Recovery from Deadlock: Resource Preemption
We preempt some resources from one process and give
it to other processes. Until the deadlock cycle is broken.
For preemption three issues are to be addressed
Selecting a victim – minimize cost.
Number of resources the deadlocked process is
holding, time consumed by it for execution.
Rollback – return to some safe state, restart process
for that state.
Rollback as far as necessary to break the deadlock.
Starvation – same process may always be picked as
victim,
Solution: include number of rollbacks in cost factor.
8.48
Deadlock Handling: summary
8.49