Lecture 14: Deadlocks Flashcards
Deadlock Can Arise if the Following 4 Conditions Hold:
Deadlock Can Arise if the Following 4 Conditions Hold:
- No Preemption: resources only released by process voluntarily when finished using
e. g., none of G, M, or K will give up the spots they have - Mutual Exclusion: only 1 process can use a resource at a time
e. g., only 1 car can use given spot at a time - Hold & Wait: process holding 1+ resources is waiting to acquire others held by other process
e. g., each car has some of the spots it needs but is waiting for others - Circular Wait: exists a set of waiting processes, {P0, …, Pn} s.t. P0 waits for P1 waits for … waits for Pn waits for P0
e. g., G waits for M waits for K waits for G
Deadlock:
“Deadlock: Both threads blocked waiting for other to finish
• possible with any synchronization approach involving locking (semaphores, monitors, …)
Assumed Model
“• Resource types: R1, R2, …, Rm
(CPU cycles, memory space, I/O devices, locks, …)
• Each resource type, Ri, has Wi, instances
• Each process utilizes a resource as follows:
1. Request the resource – if not granted wait until acquired
2. Use the resource
3. Release the resource
• System table recording records if a resource is free or not, process it allocated, and queue of processes waiting for each resource”
” Resource Allocation Graph: “
“V = Set of vertices
E = Set of edges
1. Request Edge: Pi -> Rj
2. Assignment Edge: Rj -> Pi”
“P = {P1,” “…, Pn}:” “processes in system”
“R = {R1,” “…, Rm}:” “resource types in system”
Interpretation:
“• No cycles Þ no deadlock
• Cycle and n instances per resource type Þ possible deadlock
• Cycle and 1 instance per resource type Þ deadlock”
Prevention
”= breaking one of the conditions above”
…well, except for mutual exclusion
” Hold & Wait: “
“Defined: Process holding a resource waiting to acquire
others held by other process
Breaking? Ensure that when process requests resource, does not hold another resource
• require process to request & be allocated all its resources before begins execution
• poor resource utilization, starvation is possible”
” No Preemption: “
“Defined: Resources only released voluntarily
Breaking? If process holding resources requests another it can’t get, must release all
• released resources added to those processes waiting for them
• process only restarts when it can get old and requested resources are available”
” Deadlock Prevention “
” Remove one of the 4 preconditions for deadlock
Deadlock Avoidance:
“• Similar to Deadlock Prevention
à stops deadlock before it occurs
• Unlike deadlock prevention, does not prevent one of the 4 deadlock conditions
instead: monitors the system (resource requests, holds, releases) continuously
disallows requests that lead to “unsafe states”
Unsafe state: Deadlock may occur Safe state: Deadlock won’t occur”
Safe States:
“1. Safe state = no deadlock
- Unsafe state = danger of deadlock
- Avoidance = never enter an unsafe state”
Banker’s Algorithm
“Banker’s Algorithm: A Deadlock Avoidance Algorithm
• taken from approach used by banks to deal with credit lines
à to grant credit, must guarantee that every customer can have credit lines met by available funds
à can delay granting credit or refuse to grant some credit requests
à may delay to wait for another customer to repay”
” Deadlock Detection “
“Allow system to enter deadlocked state
Periodically run deadlock detection algorithm
If deadlock detected, run recovery scheme to break”
” Cost: “
“O (m n2)
• n = number of processes
• m = number of resources
Expensive!!! Therefore, don’t run after every resource allocation”
“Frequency of Running Detection?
Many factors:
1. How often system deadlock is likely to occur
2. How many processes will need to be rolled back? (One per disjoint cycle)”
Once Deadlock Discovered, Then What?
"A: Process termination 1. Kill all deadlocked processes 2. Kill 1 process at a time until deadlock cycle is eliminated In what order? 1. Based on priority 2. Based on completion percentage 3. Based on resources held 4. Based on resources requested 5. Interactive vs. Batch"