Sub topic 7
Safe and Unsafe States
Assume you know processes, resource instances, mutual exclusion, directed graphs, and element-wise comparison of vectors. This chapter uses the Banker's model, where each process declares its maximum possible demand before requesting resources.
What a State Contains
An operating-system resource state records:
- Available: resource instances not currently allocated.
- Allocation: resources currently held by each process.
- Max: the maximum resources each process may need.
- Need: the remaining resources a process may still request.
For each process and resource type:
Need[i][j] = Max[i][j] - Allocation[i][j]A state is safe if the operating system can arrange for every process to finish, in some order, without entering deadlock. That order is called a safe sequence.
A state is unsafe if no such sequence can be found. Unsafe does not mean that deadlock has already occurred. It means that the current allocation gives no guarantee that all processes can finish.
| State | Meaning |
|---|---|
| Safe | At least one order lets every process finish |
| Unsafe | No completion order can be guaranteed |
| Deadlocked | Processes are currently permanently waiting |
Every deadlocked state is unsafe, but an unsafe state may still avoid deadlock if processes make smaller requests than their declared maximum claims.
Finding a Safe Sequence
The safety algorithm simulates completion without changing the real system.
- Set
WorktoAvailable. - Find an unfinished process whose
Needis less than or equal toWorkin every resource type. - Pretend that process finishes. Add its
AllocationtoWork. - Mark it finished and repeat.
- If all processes finish, the state is safe. If no unfinished process fits, the state is unsafe.
When a process finishes, it releases all resources it holds. Therefore, the algorithm adds Allocation, not Need, to Work.
Consider this state:
| Process | Allocation | Max | Need |
|---|---|---|---|
| P0 | (0, 1, 0) | (7, 5, 3) | (7, 4, 3) |
| P1 | (2, 0, 0) | (3, 2, 2) | (1, 2, 2) |
| P2 | (3, 0, 2) | (9, 0, 2) | (6, 0, 0) |
| P3 | (2, 1, 1) | (2, 2, 2) | (0, 1, 1) |
| P4 | (0, 0, 2) | (4, 3, 3) | (4, 3, 1) |
Available = (3, 3, 2), so initially P1 and P3 can finish. Choose P1:
Work = (3, 3, 2) + (2, 0, 0) = (5, 3, 2)
Then P3 can finish:
Work = (5, 3, 2) + (2, 1, 1) = (7, 4, 3)
Now P4, P0, and P2 can finish. One safe sequence is:
P1 → P3 → P4 → P0 → P2The exact sequence may differ. The important condition is that every selected process must have Need ≤ Work at the time it is selected.
def is_safe(available, allocation, need):
work = list(available)
finished = [False] * len(allocation)
sequence = []
while True:
for i in range(len(allocation)):
if not finished[i] and all(
need[i][j] <= work[j] for j in range(len(work))
):
work = [
work[j] + allocation[i][j]
for j in range(len(work))
]
finished[i] = True
sequence.append(i)
break
else:
break
return all(finished), sequence
available = [3, 3, 2]
allocation = [[0, 1, 0], [2, 0, 0], [3, 0, 2], [2, 1, 1], [0, 0, 2]]
need = [[7, 4, 3], [1, 2, 2], [6, 0, 0], [0, 1, 1], [4, 3, 1]]
print(is_safe(available, allocation, need))Exercise
Available is (1, 0), and one unfinished process has Need (2, 0). Can that process be selected next by the safety algorithm?
Show answer
No. Its first remaining requirement, 2, exceeds Work's first value, 1.