Chapter 8: Deadlocks
Detection Algorithm
1. Let Work and Finish be vectors of length m and n. Initialize: (a) Work = Available (b) For i = 1, ..., n if Allocation ≠ 0: Finish[i] = False else: Finish[i] = True 2. Find an index i such that both: (a) Finish[i] == False (b) Request ≤ Work o If no such i exists, go to Step 4 3. Process executes and terminates. o Work = Work + Allocation o Finish[i] = True o Go to Step 2 4. If Finish[i] == False, for some i (1 ≤ i ≤ n) then the system is in deadlock state and Pi is deadlocked.
Resource-Request Algorithm
1. Request ≤ Need? o If false, then process has exceeded its max claim 2. Request ≤ Available? o If false, must wait for resources to become available 3. Pretend to allocate by modifying state as followed: o Available = Available - Request o Allocation = Allocation + Request o Need = Need - Request 3-1. Run Safety Algorithm. o If safe => allocate resources o Else, process must wait, old resource-allocation state is restored
Safety Algorithm
1. Work and Finish are vectors of length m and n. o Work = Available o Finish[i] = False, for i = 0, ..., n-1 2. Find an i such that both: (a) Finish[i] = False (b) Need ≤ Work o If no such i exists, go to step 4 3. Process executes and terminates. o Work = Work + Allocation o Finish[i] = True o go to Step 2 4. If Finish[i] == True for all i, then system is in a safe state
Deadlock Characterization
A deadlock can arise (not guaranteed) if four conditions hold simultaneously: 1. Mutual exclusion - only one process at a time can use a resource 2. Hold and wait - a process holding at least one resource is waiting to acquire additional resource(s) held by other processes 3. No preemption - a resource can only be released voluntarily 4. Circular wait - There exists a set of waiting processes such that Pn is waiting for P0.
Deadlock Problem
A set of blocked processes each holding a resource and waiting to acquire a resource held by another in the set.
Resource-Allocation Graph
A set of vertices V and a set of edges E. V is partitioned into two types: o R (resources) o P (processes) Two types of edges: o Request edge - directed edge P->R o Assignment edge: directed edge R->P
Detection (Multi-Instance Resources)
Available: An array of length m indicates the # of available resources of each type Allocation: Same as Banker's Algorithm Request: An n x m matrix indicates the current request of each process. If Request[i, j] = k, then process Pi is requesting k more instances of resource type Rj.
A cycle in a resource-allocation graph is ____. A) a necessary and sufficient condition for deadlock in the case that each resource has more than one instance B) a necessary and sufficient condition for a deadlock in the case that each resource has exactly one instance C) a sufficient condition for a deadlock in the case that each resource has more than once instance D) is neither necessary nor sufficient for indicating deadlock in the case that each resource has exactly one instance
B
True or False: In deadlock prevention by denying hold-and-wait condition, A) resource utilization may below. B) starvation is possible. C) whenever a thread requests a resource, it does not hold any other resources. D) All of the above.
D
Prevention
Ensure at least one of the necessary conditions for deadlock is eliminated. o Mutual exclusion - make all resources shareable (except not all resources are shareable) o Hold and wait - Guarantee that whenever a process requests a resource that it does not hold any other resources (allocate all resources at once). Low resource utilization; starvation possible. o No preemption - If a process is holding resources and requests another that is unavailable, then all resources currently held are released. Process only restarts once its old resources and newly requested ones are available o Circular wait - Impose total ordering of resource types, and require each process requests resources in increasing enumeration (P1->R1, P1->R3, P1->R6)
Methods for Handling Deadlocks
Ensure the system will never enter a deadlock state o Prevention and Avoidance Allow the system to enter deadlock state and then detect and recover o Detection and Recovery Ignore problem: Most OSs choose to ignore deadlocks. Some reasons for this include: o handling deadlocks is costly in terms of performance. o Restrictive (can reduce CPU utilization) o Deadlocks occur infrequently. Kernel and application developers must write programs to handle deadlocks
True or False: Deadlock prevention by denying the mutual-exclusion condition is the simplest way to prevent deadlocks.
False
True or False: If a resource-allocation graph has a cycle, the system must be in a deadlocked state.
False
Resource-Allocation Graph Algorithm
Grant a request only if converting the request edge to assignment edge does not result in a cycle in the graph.
Deadlock Basics
If a graph contains no cycles => no deadlock If a graph contains a cycle => o if only one instance per resource type, then deadlock o if several instances per resource type, possibility of deadlock
Detection (Single Instance Resources)
Maintain wait-for graph. Nodes are processes. Periodically invoke an algorithm to search for a cycle in the graph. If there is a cycle, there exists a deadlock.
Safe State
No deadlock zone. There exists an execution sequence of all processes such that each of their requests can be satisfied.
Avoidance
Requires that the system has some additional a priori info. o Each process declares the maximum number of instances of each resource that it may need. Deadlock-avoidance algorithm dynamically examines resource-allocation state to ensure that the system stays in a safe state.
System Model
System consists of resources (R1, R2, ..., Rm). Each resource has Wi instances. Each process utilizes a resource as follows: o request (may cause the process to wait) o use o release
Bridge Crossing Example
Traffic in only in one direction. Each section of bridge is a resource. If a deadlock occurs, it can be resolved if one car backs up (preempt resources and rollback). Several cars may have to be backed up if deadlock occurs.
True or False: Both deadlock prevention and deadlock avoidance techniques ensure that the system will never enter a deadlocked state.
True
Recovery
Two methods: 1. Process termination o abort all deadlocked processes o abort one process at a time until the deadlock cycle is eliminated 2. Resource preemption
Banker's Algorithm
Used for multiple instances. Each process must a priori claim maximum use. Processes may have to wait. Running processes must return resources in a finite amount of time.
Resource-Allocation Graph Avoidance Scheme
Used for single instances. Include a third edge: claim edge - P-->R indicates that process P may request resource R in the future. Resources must be claimed a priori. o Claim edge is converted to request edge when a process requests a resource. o Request edge is converted to a assignment edge when a resource is allocated. o Assignment edge is converted to a claim edge when a process is terminated.
Data Structures for Banker's Algorithm
n = # of processes m = # of resource types Available: Array of length m. If available [j] = k, then 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 Allocation[i,j] = k, then Pi is currently allocated k instances of Rj Need: n x m matrix. If Need[i,j] = k, then Pi needs k more instances of Rj to finish
Safe State Basics
o Safe state => no deadlocks o Unsafe state => possibility of deadlock Avoidance algorithms ensure that a system will never enter an unsafe state. A process is waited if its request will make the system unsafe.