Deadlocks
Chapter 6
6.1. Resources
6.2. Introduction to deadlocks
6.4. Deadlock detection and recovery 6.5. Deadlock avoidance
Resources
• A resource is something that can be acquired, used and released over the time.
• Examples of computer resources used by processes
– Printers
– Tape drives
Resources
• Processes need access to resources in reasonable order
– Two processes writing simultaneously to a printer will lead to gibberish (meaningless characters).
Resources
• Suppose a process holds resource A and requests resource B
– at same time another process holds B and requests A
– both are blocked and remain so
• Example Scenario
– Two processes each want to write a scanned document on the CD. Process A request permission to use the scanner and is granted it. Process B is programmed differently and request 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 CD recorder B asks for the
Resources (1)
• Deadlocks occur when …
– processes are granted exclusive access to devices
– we refer to these devices generally as resources
• Preemptable resources
– can be taken away from a process with no ill effects
• Nonpreemptable resources
Resources (2)
• Sequence of events required to use a resource
1. request the resource
2. use the resource
3. release the resource
• Must wait if request is denied
– requesting process may be blocked
Introduction to Deadlocks
• Formal definition :
A set of processes is deadlocked if each process in the set is waiting for an event that only another process in the set can cause
• Usually the event is release of a currently held resource • None of the processes can …
– run
– release resources
Four Conditions for Deadlock
1. Mutual exclusion condition
• each resource assigned to 1 process or is available
2. Hold and wait condition
• process holding resources can request additional
3. No preemption condition
• previously granted resources cannot forcibly taken away
4. Circular wait condition
• must be a circular chain of 2 or more processes
Deadlock Modeling (2)
• Modeled with directed graphs
a) resource R assigned to process A
b) process B is requesting/waiting for resource S
Deadlock Modeling (3)
Strategies for dealing with Deadlocks
1. just ignore the problem altogether
2. detection and recovery
• Let deadlocks occur, detect them, and take action.
3. dynamic avoidance
• careful resource allocation
4. prevention
How deadlock occurs
Deadlock Modeling (5)
How deadlock can be avoided
Detection with One Resource of Each Type (1)
a) Note the resource ownership and requests
Detection with One Resource of Each Type (1)
• Algorithm to detect Deadlock:
1. For each node, N in the graph, perform the following five steps with N as the starting node.
2. Initialize L to the empty list, and designate all the arcs as unmarked.
3. Add the current node to the end of L and check to see if the node now appears in L two times. If it does, the graph contains a cycle (listed in L) and the algorithm terminates.
4. From the given node, see if there are any unmarked outgoing arcs. If so, go to step 5; if not, go to step 6.
5. Pick an unmarked outgoing arc at random and mark it Then follow it to the new current node and go to step 3.
• End product of the algorithm
– L = [B, T, E, V, G, U, D, T ]
– Algorithm stops at this point because T is repeated twice in the list L.
– Deadlock detected.
• Matrix-based algorithm for detecting deadlock
– Among n processes P1 to Pn
– m is no. of resource classes (E1 is resources of class 1, E2 is resources of class 2, Em is resources of class m)
– E is the existing resource vector
• E.g. if class 1 is of tape drive then E1 = 2 means system has two tape drives
– A is the available resource vector (i.e. currently available)
• E.g. if both tape drives are assigned then A1=0
• Two arrays C and R
– C is the current allocation matrix
• i-th row of C tells how many instances of each resource class Pi currently holds.
• Cij is the number of instances of resource j that are held by process i.
– R is the request matrix
• Similarly, Rij is the number of instances of resource j that Pi wants.
Detection with Multiple Resources of Each Type
An example for the deadlock detection algorithm
Algorithm for detection:
1. Initially all process are unmarked. Look for an
unmarked process, Pi, for which the i-th row of R is less than or equal to A.
2. If such a process is found, add the i-th row of C to A, mark the process, and go back to step 1.
3. If no such process exists, the algorithm terminates.
When the algorithm finishes, all the unmarked processes, if any, are deadlocked.
When to run this algorithm:
a) check every time a resource request is made
b) check every k minutes
c) when the CPU utilization has dropped below some threshold
• there will be few runnable processes, and the CPU will often be idle
15
Recovery from Deadlock
• Deadlock detected, what’s next ? Recovery!
• Recovery through preemption
– take a resource from some other process
– depends on nature of the resource
• For example, to take a laser printer away from its owner, the operator can collect all the sheets already printed and put them in a pile. Then the
• Recovery through rollback
– Checkpoint a process periodically
• State is written to a file
– State includes 1) memory image 2) resource state
» Which resources are assigned to which process
– use this saved state
– restart the process if it is found deadlocked
Recovery from Deadlock
• Recovery through killing processes
– crudest but simplest way to break a deadlock
– kill one of the processes in the deadlock cycle
– the other processes get its resources
Deadlock Avoidance
• In deadlock detection algorithms,
– All resources were requested at once
– But in real only one resource at a time is requested by a process
Deadlock Avoidance: Safe State
• A state is said to be safe if there is some
scheduling order in which every process can run to completion even if all of them
Safe and Unsafe States (1)
Demonstration that the state in (a) is safe because the system by careful scheduling,
can avoid deadlock.
Safe and Unsafe States (2)
Demonstration that the sate in b) is unsafe because there is no sequence that guarantees completion. Hence it goes from safe to unsafe i.e. from a) to
Safe and Unsafe States (3)
• Note that an unsafe state is not a deadlocked state.
• System can still run for a while at b)
Banker's Algorithm
• It is modeled on the way a small-town banker might deal with a group of
customers to whom he has granted lines of credit.
• What the algorithm does is check to see if
Banker's Algorithm - Example
• Four customers A,B,C and D.
The Banker's Algorithm for a Single Resource
• Three resource allocation states
– safe
– safe
Banker's Algorithm for Multiple Resources
• The algorithm for checking to see if a state is safe:
– 1. Look for a row, R, whose unmet resource needs are all smaller than or equal to A. If no such row exists, the system will eventually dead-lock since no process can run to completion (assuming processes keep all resources until they exit).
– 2. Assume the process of the row chosen requests all the resources it needs (which is guaranteed to be possible) and finishes. Mark that process as terminated and add all its resources to the A vector.
– 3. Repeat steps 1 and 2 until either all processes are marked terminated (in which case the initial state was safe) or no process is left whose resource needs can be met (in which case there is a deadlock).
• Current State in shown in figure is safe.
– First D can run then either A or E then the rest of the processes.
• It can lead to unsafe state if B acquires one scanner and after that E also acquires the last scanner.
– Available vector becomes (1 0 0 0) leading to deadlock state.
• Developed by Djikstra’s but it is only good theoretically good enough. In practical, it is not that useful
– as prior needed resource information is required all the time
– and resources may crash in between.
Deadlock Prevention
• Deadlock avoidance is difficult as prior information of future requests needs to be known. So for real systems better approach is to prevent it.
• If we can ensure that at least one of four
conditions is never satisfied, then
Deadlock Prevention
1. Attacking the Mutual Exclusion Condition
• Some devices (such as printer) can be spooled
– only the printer daemon uses printer resource
– thus deadlock for printer eliminated
• Not all devices can be spooled • Principle:
– avoid assigning resource when not absolutely necessary
2. Attacking the Hold and Wait Condition
• Require processes to request resources before starting
– a process never has to wait for what it needs
• Problems
– may not know required resources at start of run
– also ties up resources other processes could be using
• Variation:
– process must give up all resources
3. Attacking the No Preemption Condition
• This is not a viable option
• Consider a process given the printer
– halfway through its job
– now forcibly take away printer
– !!??
4. Attacking the Circular Wait Condition (1)
• Normally ordered resources • A resource graph
4. Attacking the Circular Wait Condition (1)
The Difference Between Deadlock Prevention and Deadlock Avoidance
• Deadlock Prevention:
– Preventing deadlocks by constraining how requests for resources can be made in the system and how they are handled (system design).
– The goal is to ensure that at least one of the necessary conditions for deadlock can never hold.
• Deadlock Avoidance:
– The system dynamically considers every request and decides whether it is safe to grant it at this point,
– The system requires additional priori information regarding the overall potential use of each resource for each process.
– Allows more concurrency.