Deadlocks
Necessary conditions for deadlock¶
When these conditions are fulfilled, there is a possibility of deadlocks. If any one is violated, deadlock cannot happen
- Mutex
- Hold and wait
- No pre-emption: a process cannot be forced to release a resource
- Circular wait: A cycle in the resource allocation graph
Resource Allocation Graph¶
Table:
If no cycle, no deadlock
If cycle, and cycle has a resource with single instance, deadlock
If cycle, but all res have multiple instances then deadlock may happen. Need to analyse
Deadlock Prevention¶
Mutex prevention¶
Not possible for certain types of res
Hold and wait prevention¶
Don't hold on to a resource while requesting/waiting on another resource
Protocol 1: acquire everything before starting execution (suffers from idle utilisation) Protocol 2: Only request for new resources when no resources are already acquired (suffers from acquire/release overhead)
Allowed Pre-emption¶
This is commonly used with resources whose states can be easily saved/restored, like CPU regs.
Protocol 1: forcefully release and acquire resources held by a process implicitly Protocol 2: release and acquire resources only if the process which holds them is waiting
Circular Wait Prevention¶
Ensure that the resources are requested in a particular order. If not, earlier acquired resources must be released first.
Deadlock Avoidance¶
Banker's Algorithm¶
let there me M
resources and N
processes
- Saftey Algorithm
- Resource Allocation Algorithm
State variables:
available[i]
# of resi
available.max[i][j]
max # of simultaneous requests for resourcej
by proci
allocation[i][j]
current # of resourcej
alloc-ed to proci
need[i][j]
how many more # of resourcej
are needed by proci
max[i][j] = need[i][j] + allocation[i][j]
Resource Allocation Algorithm¶
returns: boolean to indicate request accepted or rejected
- if
any(request[i] > need[i])
returnFalse
(reject) - if
any(request[i] > available[i])
returnFalse
- deepcopy
available
,allocation
,need
and pass to saftey check algo. - If the outcome of the saftey check is
True
thenavailable -= request[i]
need[i] -= request[i]
allocation[i] += request[i]
False
then- Try again
Safety Algorithm¶
- create an array
finish = [False]*N
. - Hypothetically grant the request:
available -= request[i]
need[i] -= request[i]
allocation[i] += request[i]
- find a process
i
for which!finish[i]
- if
all(need[i] < available[i])
(proc can finish even after req granted)available += allocation[i]
finish[i] = True
- repeat
- if
all(finish)
returnTrue
elseFalse
\(\mathcal{O}(MN^2)\) time complexity
Deadlock Detection¶
Same as safety algorithm but performed on actual system state, not on a hypothetical state.
How often to check for deadlock?
- depends on how often it can occur and
- how many procs can be affected by it. it's an expensive algo!
Resolution options:
- Release all held resources from all deadlocked processes
- Release all held resources from deadlocked processes one at a time.
Questions:
- Order of pre-emption
- Cost. Which process will have longest rollback?
- Starvation should not occur