Assumptions:
Predeclear Resources:
Proceed to Completion:
Safe Execution Sequence:
Definition: P_1 \to P_2 \to ... \to P_n is safe if P_i can be satisfied by current free resources F and resources held by P_1, P_2, ..., P_{i-1} for all i.
Properties: P_i's waiting is bounded. No deadlock.
Safe State:
Definition: at least one safe execution sequence exists in the system.
Worst Case: all processes need all resources (serial execution). In this case safe execution sequence still exist
Resource Manager:

For above image, P_1, P_0, or P_2 might not ask for "Room" resources, but we need to prepare for the worst case.


Implementation:
Cycle Detection: pretend we grant an request, we check if dependency graph has cycle. If yes, wait. (Graph is computational expensive)
Banker's Algorithm: pretend allocate resources greedily so long as one process can finish. Continue for all process. If all processes can finish, grant request. Otherwise, wait. (Greedy algorithm works since system's resource linearly increases when process finish.)
Avoidance
more flexible than prevention
rejecting all unsafe states can be inefficient (since system can enter unsafe state and return safely in some chance)
process must pre-declare max resource need
system throughput is reduced compared to "detection and recovery" approach
Detection:
method 1: occasionally lock entire system, and scan graph O(n^2) and see if cycle exists. If yes, we have deadlock.
method 2: banker's algorithm for detection
how often: can't scan too often or too rarely (clock time interval? CPU idle?)
Recovery:
method 1: kill all processes.
method 2: kill one process
method 3: send EDEADLOCK to process, wanting them to voluntarily release resources ("please release before you retry")
Table of Content