Shivangi submitted a document on deadlocks to Tapas Sangiri. The 3-sentence summary is:
The document discusses different aspects of deadlocks including definitions, characteristics, methods for handling them such as prevention, avoidance and recovery. Prevention methods aim to ensure a deadlock never occurs by restricting resource allocation in different ways. Avoidance algorithms analyze the resource allocation graph to determine if a system is in a safe state to avoid deadlocks.
2. The Deadlock Problem
System Model
Deadlock Characterization
Methods for Handling Deadlocks
Deadlock Prevention
Deadlock Avoidance
Recovery from Deadlock
CONTENTS
3. The Deadlock Problem
EXAMPLES:
"It takes money to make money".
You can't get a job without experience; you can't get
experience without a job.
BACKGROUND:
The cause of deadlocks: Each process needing what another
process has. This results from sharing resources such as
memory devices, links.
Under normal operation, a resource allocations proceed like
this:
Request a resource (suspend until available if necessary.
Use the resource.
4. Deadlock Characterization
Deadlock can arise if four conditions hold simultaneously.
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: there exists a set {P0, P1, …, Pn} of waiting
processes such that P0 is waiting for a resource that is held
by P1, P1 is waiting for a resource that is held by P2, …, Pn–1
is waiting for a resource that is held by Pn, and Pn is waiting
5. A visual ( mathematical ) way to determine if a deadlock has, or may occur.
G = ( V, E ) The graph contains nodes and edges
V Nodes consist of processes = { P1, P2, P3, ...} and
resource types
{ R1, R2, ...}
E Edges are ( Pi, Rj ) or ( Ri, Pj )
An arrow from the process to resource indicates the process is
requesting the resource. An arrow from resource to process shows an
instance of the resource has been allocated to the process.
Process is a circle, resource type is square; dots represent number of
instances of resource in type. Request points to square, assignment
comes from dot.
RESOURCE ALLOCATION
GRAPH
Pi
Rj
Pi
Rj
Pi
6. Example of a
Resource
Allocation Graph
Resource
Allocation
Graph With A
Deadlock
Graph With A
Cycle But No
Deadlock
RESOURCE ALLOCATION GRAP
7. Methods for Handling
Deadlocks
Ensure that the system will never enter a
deadlock state – deadlock prevention
Allow the system to enter a deadlock state
and then recover
Ignore the problem and pretend that
deadlocks never occur in the system; used
by most operating systems, including UNIX
8. Do not allow one of the four conditions to occur.
Mutual exclusion:
a) Automatically holds for printers and other non-
sharables.
b) Shared entities (read only files) don't need mutual
exclusion (and aren’t susceptible to deadlock.)
c) Prevention not possible, since some devices are
intrinsically non-sharable.
Hold and wait:
a) Collect all resources before execution.
b) A particular resource can only be requested when no
others are being held. A sequence of resources is
always collected beginning with the same one.
c) Utilization is low, starvation possible.
Deadlock
Prevention
9. Deadlock Prevention (Cont.)
No Preemption –
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
Circular Wait – impose a total ordering of all
resource types, and require that each process
requests resources in an increasing order of
enumeration
10. NOTE: All deadlocks are unsafe, but all unsafes
are NOT deadlocks.
SAFE
DEADLOCK
UNSAFE
Only with luck will
processes avoid
deadlock.
O.S. can avoid
deadlock.
Deadlock
Avoidance
11. Single instance of a resource type
Use a resource-allocation graph
Multiple instances of a resource
type
Use the banker’s algorithm
Avoidance
algorithms
12. Safety Algorithm
1. Let Work and Finish be vectors of length m and n,
respectively. Initialize:
Work = Available
Finish [i] = false for i = 0, 1, …, n- 1
2. Find an i such that both:
(a) Finish [i] = false
(b) Needi Work
If no such i exists, go to step 4
3. Work = Work + Allocationi
Finish[i] = true
go to step 2
13. Example of Banker’s Algorithm
5 processes P0 through P4;
3 resource types:
A (10 instances), B (5instances), 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
14. Example (Cont.)
The content of the matrix Need is defined to be Max
– Allocation
Need
A B C
P0 7 4 3
P1 1 2 2
P2 6 0 0
P3 0 1 1
P4 4 3 1
The system is in a safe state since the sequence <
P1, P3, P4, P2, P0> satisfies safety criteria
15. Recovery from Deadlock:
Process 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
Is process interactive or batch?
16. Recovery from Deadlock:
Resource Preemption
Selecting a victim – minimize cost
Rollback – return to some safe state,
restart process for that state
Problem: starvation – same process may
always be picked as victim, include number
of rollback in cost factor