Deadlock
Deadlock (cont.)
n
Consider this example:
Process A printer>wait( ); disk->wait( ); print file printer->signal( ); disk->signal( ); Process B disk>wait( ); printer->wait( ); print file disk->signal( ); printer->signal( );
OS must distribute system resources among competing processes:
q q q q
CPU cycles Memory space Files I/O devices (printer)
preemptable preemptable non-preemptable non-preemptable
Deadlock occurs when two or more processes are each waiting for an event that will never occur, since it can only be generated by another process in that set
Deadlock is one of the more difficult problems that OS designers face
q
A request for a type of resource can be satisfied by any resource of that type
q q
Use any 100 bytes in memory Use either one of two identical printers
As we examine various approaches to dealing with deadlock, notice the tradeoffs between how well the approach solves the problem, and its performance /OS overhead
Fall 1998, Lecture 19 2
Process requests resource(s), uses it/them, then releases it/them
q
We will assume here that the resource is re-usable; it is not consumed Waits if resource is not currently available
Fall 1998, Lecture 19
Deadlock Conditions
n
Resource-Allocation Graph
n
These 4 conditions are necessary and sufficient for deadlock to occur:
q
Mutual exclusion if one process holds a resource, other processes requesting that resource must wait until the process releases it (only one can use it at a time) Hold and wait processes are allowed to hold one (or more) resource and be waiting to acquire additional resources that are being held by other processes No preemption resources are released voluntarily; neither another process nor the OS can force a process to release a resource Circular wait there must exist a set of waiting processes such that P0 is waiting for a resource held by P1, P1 is waiting for a resource held by P2, Pn-1 is waiting for a resource held by Pn, and Pn is waiting for a resource held P0
Fall 1998, Lecture 19 4
The deadlock conditions can be modeled using a directed graph called a resourceallocation graph (RAG)
q
2 kinds of nodes:
n
Boxes represent resources
Instances of the resource are represented as dots within the box
Circles represent threads / processes Request edge from process to resource indicates the process has requested the resource, and is waiting to acquire it Assignment edge from resource instance to process indicates the process is holding the resource instance
2 kinds of (directed) edges:
n
When a request is made, a request edge is added
n
When request is fulfilled, the request edge is transformed into an assignment edge When process releases the resource, the assignment edge is deleted
Fall 1998, Lecture 19
Interpreting a RAG With Single Resource Instances
n
Dealing with Deadlock
n
If the graph does not contain a cycle, then no deadlock exists
r1 r2
The Ostrich Approach stick your head in the sand and ignore the problem Deadlock prevention prevent deadlock from occurring by eliminating one of the 4 deadlock conditions Deadlock detection algorithms detect when deadlock has occurred
q
n
p1 p2 p3
r3
r4
If the graph does contain a cycle, then a deadlock does exist
r1 r2
Deadlock recovery algorithms break the deadlock
p1
p2
p3
r3
r4
With single resource instances, a cycle is a necessary and sufficient condition for deadlock
Fall 1998, Lecture 19 6
Deadlock avoidance algorithms consider resources currently available, resources allocated to each thread, and possible future requests, and only fulfill requests that will not lead to deadlock
Fall 1998, Lecture 19
Deadlock Prevention
n
Deadlock Prevention (cont.)
n
Basic idea: ensure that one of the 4 conditions for deadlock can not hold Mutual exclusion if one process holds a resource, other processes requesting that resource must wait until the process releases it (only one can use it at a time)
q
Circular wait there must exist a set of waiting processes such that P0 is waiting for a resource held by P1, P1 is waiting for a resource held by P2, Pn-1 is waiting for a resource held by Pn, and Pn is waiting for a resource held P0
q
Hard to avoid mutual exclusion for nonsharable resources
n n
To avoid, impose a total order on all resources, and require process to request resource in that order
n n n n
Printer & other I/O devices Files
However, many resources are sharable, so deadlock can be avoided for those resources
n
Order: disk drive, printer, CDROM Process A requests disk drive, then printer Process B requests disk drive, then printer Process B does not request printer, then disk drive, which could lead to deadlock
Read-only files
Order should be in the logical sequence that the resources are usually acquired
n
For printer, avoid mutual exclusion through spooling then process wont have to wait on physical printer
Fall 1998, Lecture 19 8
Allow process to release all resources, and start request sequence over Or force process to request total number of each resource in a single request
Fall 1998, Lecture 19
Deadlock Prevention (cont.)
n
Deadlock Prevention (cont.)
n
No preemption resources are released voluntarily; neither another process nor the OS can force a process to release a resource
q
Hold and wait processes are allowed to hold one (or more) resource and be waiting to acquire additional resources that are being held by other processes
q
To avoid, allow preemption
n
If process A requests resources that arent available, see who holds those resources
If the holder (process B) is waiting on additional resources, preempt the resource requested by process A Otherwise, process A has to wait While waiting, some of its current resources may be preempted Can only wake up when it acquires the new resources plus any preempted resources
To avoid, ensure that whenever a process requests a resource, it doesnt hold any other resources
n
Request all resources (at once) at beginning of process execution
Process which loops forever?
Request all resources (at once) at any point in the program To get a new resource, release all current resources, then try to acquire new one plus old ones all at once
If a process requests a resource that can not be allocated to it, all resources held by that process are preempted
Can only wake up when it can acquire all the requested resources
Difficult to know what to request in advance Wasteful; ties up resources and reduces resource utilization Starvation is possible
Fall 1998, Lecture 19
n
9
Only works for resources whose state can be saved/restored (memory, not printer)
Fall 1998, Lecture 19 10