Title: Deadlock Management
1Deadlock Management
2Topics
- Introduction to Deadlocks
- Deadlock Management Approaches
- Deadlock Acceptance (Ostrich Algorithm)
- Deadlock Prevention
- Deadlock Detection and Recovery
- Deadlock Avoidance (Banker Algorithm)
- Other issues
3Introduction to Deadlocks
- Deadlock definition
- Contributing factors to deadlocks
- Concurrent processing
- Needed for high performance and utilization
- Competition over limited resources
- Examples of applications that have deadlock
- Operating systems, Database systems, Distributed
Systems,
Deadlock is a situation where a group of
processes are permanently blocked waiting for the
resources held by each other in the group.
4System Model
- Concurrent processes P1, P2, ,Pn
- Resource types R1, R2, . . ., Rm
- CPU cycles, memory space, I/O devices, printers
- Each resource type Ri has Wi instances
- Each process utilizes a resource as follows
- Request
- Use
- Release
Represent the relationships between processes
and resources as a graph
5Resource-Allocation Graph
A set of vertices V and a set of edges E.
- V is partitioned into two types
- Processes P1, P2, , Pn
- Resource types R1, R2, , Rm
- E is partitioned into two types
- Request edge directed edge Pi Rj
- Assignment edge directed edge Rj Pi
6Resource-Allocation Graph (Contd)
Pi
- Process
- Resource Type with 4 instances
- Pi requests instance of Rj
- Pi is holding an instance of Rj
Rj
Rj
Pi
Rj
Pi
7Graph Construction
C
A
B
8Deadlock Characterization
Four necessarily and sufficient conditions
- Mutual exclusion Only one process at a time can
use a resource - Hold and wait A process holding at least one
resource is waiting to acquire additional
resources held by other processes - No preemption A resource can be released only
voluntarily by the process holding it, after that
process has completed its task - Circular wait Every process Pi is waiting for
a resource that is held by P(i1 mod n), 1 i lt n
9Examples
Cycle, No Deadlock
Deadlock ?Cycle
- Cycles do NOT necessarily imply deadlock unless
there is ONLY one instance of each resource type
10Topics
- Introduction to Deadlocks
- Deadlock Management Approaches
- Deadlock Acceptance (Ostrich Algorithm)
- Deadlock Prevention
- Deadlock Detection and Recovery
- Deadlock Avoidance (Banker Algorithm)
- Other issues
11Deadlock Management Approaches
- Ignore the problem altogether (Ostrich Algorithm)
- Prevent deadlocks from happening
- Negate one of the necessary conditions
- Allow deadlocks to happen
- Detect and recover if happened
- Allow deadlocks to happen (Bankers Algorithm)
- Dynamic avoidance with careful resource
assignment
121- The Ostrich Algorithm
- Pretend there is no problem
- Reasonable if
- Deadlocks occur very rarely
- Cost of prevention is high
- UNIX and Windows takes this approach
- It is a trade off between
- Convenience Correctness
Not for real-time or critical systems
132- Deadlock Prevention
Take out any of the four necessarily conditions
- Mutual exclusion Only one process at a time can
use a resource - Hold and wait A process holding at least one
resource is waiting to acquire additional
resources held by other processes - No preemption A resource can be released only
voluntarily by the process holding it, after that
process has completed its task - Circular wait Every process Pi is waiting for
a resource that is held by P(i1 mod n), 1 i lt n
142- Deadlock Prevention (Breaking Mutual Exclusion)
- Not easy to break because it is a resource
characteristic - Not required for sharable resources (E.g. File
opened for read) - Must hold for non-sharable resources (E.g., tape
drivers, printers, ) - Some devices can be spooled (requests added to
buffer) - Deadlock is eliminated for spooled resources,
e.g. printers - Problems
- Not all devices can be spooled
Buffer
152- Deadlock Prevention (Breaking Hold and Wait)
- Must guarantee that whenever a process requests a
resource, it does not hold any other resources - Request and be allocated all its resources before
it begins execution - Allow process to request resources only when the
process has none - Problems
- May not know the required resources at the
beginning - Low resource utilization
- Indefinite postponement (Starvation)
162- Deadlock Prevention (Breaking No Preemption)
- If a process that is holding some resources
requests another resource that cannot be
immediately allocated - 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 - Problems
- May not be a viable solution (process is killed)
- Try to Stop/Resume the process (very expensive,
if feasible)
172- Deadlock Prevention (Breaking Circular Wait)
- Impose a total ordering of all resource types
- Require each process to request resources in
order (increasing or descending) - Problems
- May not know in advance all the required
resources - Low resource utilization
18Resource Ordering Example
C
A
B
1- R 2- S 3- T
19Summary of Deadlock Prevention
20Topics
- Introduction to Deadlocks
- Deadlock Management Approaches
- Deadlock Acceptance (Ostrich Algorithm)
- Deadlock Prevention
- Deadlock Detection and Recovery
- Deadlock Avoidance (Banker Algorithm)
- Other issues
213- Deadlock Detection and Recovery
- Allow the system to enter a deadlock state
- Periodically run a deadlock detection algorithm
- Recover if deadlock is detected
22 3- Deadlock Detection and Recovery
Detection with One Instance of Each Resource
Type
- Check the Resource-Allocation Graph
- If a cycle exists ? A deadlock exists
233- Deadlock Detection and Recovery
- Recovery through preemption
- Take a resource from one process
- Recovery through rollback
- Checkpoint processes periodically
- Restart one process from its last check point
- Recovery through killing processes
- Simplest way to break a deadlock
- Free its resources for other processes to complete
-- Assign priorities -- How long a process has
computed? How much longer remains? --
Resources already allocated to it? Resources
still needed? -- Random selection
-- Good scheduling (E.g., Ageing)
Common Problems 1- Selecting a victim
2- Starvation
244- Deadlock Avoidance
Requires that the system has some additional a
priori information available in advance
- Requires that each process declare the maximum
number of resources of each type that it may need - The algorithm dynamically examines the
resource-allocation state - Prevents the circular-wait condition
- Resource-allocation state is defined by
- The number of available resources
- The number allocated resources
- The maximum demands of the processes
25Safe State
- When a process requests a resource, system
decides if immediate allocation leaves the system
in a safe state - System is in safe state if there exists a safe
sequence of all processes - Sequence ltP1, P2, , Pngt 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
jlti - If Pi resource-needs are not immediately
available, then Pi waits until all Pj have
finished, with jlti - When Pj is finished, Pi can obtain needed
resources, execute, return allocated resources,
and terminate - When Pi terminates, Pi1 can obtain its needed
resources, and so on
26Safe, Unsafe , Deadlock State
27Resource-Allocation Graph
- Claim edge Pi ? Rj indicate that process Pi may
request resource Rj - Claim edge converts to request edge when a
process actually requests a resource - Resources must be claimed a priori in the system
28Bankers Algorithm (Key Features)
- Multiple instances of a resource type
- Each process must a priori claim maximum use
(claim edges) - When a process requests a resource it may have to
wait - When a process gets all its resources it must
return them in a finite amount of time - Moves the system from one safe state to another
safe state
29Bankers Algorithm Data Structures
Let n number of processes, and m number of
resources types.
- Available Vector of length m. If available j
k, there are k instances of resource type Rj
available - Max n x m matrix. If Max i,j k, then
process Pi may request at most k instances of
resource type Rj - Allocation n x m matrix. If Allocationi,j
k then Pi is currently allocated k instances of
Rj. - Need n x m matrix. If Needi,j k, then Pi
may need k more instances of Rj to complete its
task. - Need i,j Maxi,j Allocation i,j
30Safety Algorithm (Tested with each request)
- 1. Let Work and Finish be vectors of length m and
n, respectively. Initialize - Work Available
- Finish i false, for i1,3, , n
- 2. Find process i such that both
- (a) Finish i false
- (b) Needi ? Work
- If no such i exists, go to step 4.
- 3. Work Work AllocationiFinishi truego
to step 2. - 4. If Finish i true for all i, then the
system is in a Safe state, otherwise, Unsafe
state.
31Resource-Request Algorithm for Process Pi
- -Request request vector for process Pi
- -If Requesti j k then process Pi wants k
instances of Rj - 1. If Requesti ? Needi
- Go to step 2.
- Else
- Raise error since Pi has exceeded its
maximum claim. - 2. If Requesti ? Available
- Go to step 3.
- Else
- Pi must wait, since resources are not
available. - 3. Pretend to allocate requested resources to Pi
- Available Available - Requesti
- Allocationi Allocationi Requesti
- Needi Needi Requesti
- If safe ? the resources are allocated to Pi.
- If unsafe ? Pi must wait, and the old
resource-allocation state is restored
32Safety Algorithm Example
- 5 processes P0 through P4
- 3 resource types A, B, C
- A(10 instances), B (5 instances), and C (7
instances) - Snapshot at time T0
- Allocation Max Available
- A B C A B C A B C
- P0 0 1 0 7 5 3 3 3 2
- P1 2 0 0 3 2 2
- P2 3 0 2 9 0 2
- P3 2 1 1 2 2 2
- P4 0 0 2 4 3 3
System is in a safe state because lt P1, P3, P4,
P2, P0gt is a safe sequence
33Request Algorithm Example P1 Request (1,0,2)
- Check that Request ? Available (that is, (1,0,2)
? (3,3,2) ? true) - Allocation Need Available
- A B C A B C A B C
- P0 0 1 0 7 4 3 2 3 0
- P1 3 0 2 0 2 0
- P2 3 0 1 6 0 0
- P3 2 1 1 0 1 1
- P4 0 0 2 4 3 1
- Executing safety algorithm shows that sequence
ltP1, P3, P4, P0, P2gt satisfies safety
requirement. - Exercise
- Can request for (3,3,0) by P4 be granted?
- Can request for (0,2,0) by P0 be granted?
34Topics
- Introduction to Deadlocks
- Deadlock Management Approaches
- Deadlock Acceptance (Ostrich Algorithm)
- Deadlock Prevention
- Deadlock Detection and Recovery
- Deadlock Avoidance (Banker Algorithm)
- Other issues
35Starvation
- Starvation definition
- Main cause is bad scheduling
- Example
- Driving license branch takes the shortest request
first - Solution is good scheduling
- First-come, first-serve policy
- Priorities Aging
A process may wait indefinitely to complete
without being part of a deadlock
36Two-Phase Locking in DBMS
- Two-phase locking is a concept of database
systems - Concurrency control
- Phase 1(Expanding phase) collect all locks a
transaction needs - Phase 2 (Shrinking phase) start releasing the
locks
Notices the similarity with Requesting all
resources at once Not the same
37Topics
- Introduction to Deadlocks
- Deadlock Management Approaches
- Deadlock Acceptance (Ostrich Algorithm)
- Deadlock Prevention
- Deadlock Detection and Recovery
- Deadlock Avoidance (Banker Algorithm)
- Other issues
38Summary
- Deadlock definition
- Deadlock management
- Ignore the problem altogether (Ostrich Algorithm)
- Prevent deadlocks from happening
- Negate one of the necessary conditions
- Allow deadlocks to happen
- Detect and recover if happened
- Allow deadlocks to happen
- Dynamic avoidance with careful resource
assignment
Deadlock is a situation where a group of
processes are permanently blocked waiting for the
resources held by each other in the group.