Sub topic 7
Safe and unsafe states
This chapter assumes you know the resource model (processes, resource types, instances, request-use-release) and the four Coffman conditions. It also assumes you can compare two vectors element by element, for example [2, 1, 0] <= [3, 1, 2]. We stay with small examples and a hand check here. The next chapter turns the check into the full safety algorithm.
Why avoidance needs a new idea
Prevention removes a Coffman condition permanently, and it often wastes resources. Avoidance keeps all four conditions possible. Before it grants any request, it asks whether granting leaves the system able to finish every process in some order, even if every process then asks for its full declared maximum.
To answer that question, the operating system needs advance information. Each process declares its maximum claim: the most instances of each resource type it will ever hold at once. The system also tracks what each process holds now and what is free. From these it derives one more quantity:
- Need = Max − Allocation. This is the most a process could still ask for.
Definitions
A safe sequence is an ordering of all processes <P1, P2, ..., Pn> with the following property. For each Pi, the resources it might still request (its Need) can be satisfied by the currently free resources plus the resources held by all earlier processes Pj with j < i. Think of it as a plan in which each process in turn runs to completion and hands back everything it holds.
A state is:
- Safe if at least one safe sequence exists.
- Unsafe if no safe sequence exists.
- Deadlocked if some set of processes is already waiting in a cycle for resources that only other members of the set can release.
The relationships are strict:
- A safe state cannot lead to deadlock, provided processes stay within their declared maximums and the system only moves to states it has checked as safe.
- A deadlocked state is always unsafe.
- An unsafe state is not necessarily deadlocked. It means deadlock is possible, depending on how the processes behave.
Point 3 is the one GATE questions test most often. An unsafe state is a state the avoidance algorithm refuses to enter. Whether a deadlock would really have followed depends on future requests, and the algorithm cannot know them. It plans for the worst case.
A single-resource example
Take one resource type with 12 instances and three processes.
| Process | Max | Allocation | Need |
|---|---|---|---|
| P0 | 10 | 5 | 5 |
| P1 | 4 | 2 | 2 |
| P2 | 9 | 2 | 7 |
Instances held: 5 + 2 + 2 = 9, so Available = 3.
Try to build a sequence by hand. Keep a running value called Work, which starts at Available.
- P0 needs 5, and Work is 3. P0 cannot go first.
- P2 needs 7, and Work is 3. P2 cannot go first.
- P1 needs 2, and Work is 3. P1 can go first. After P1 finishes, it returns its 2 instances, so Work = 3 + 2 = 5.
- Now P0 needs 5, and Work is 5. P0 can go. After P0 finishes, Work = 5 + 5 = 10.
- P2 needs 7, and Work is 10. P2 can go. Work = 12.
The state is safe, and <P1, P0, P2> is a safe sequence. After P1 the order <P1, P2, P0> fails, because P2 needs 7 and only 5 are free. So this state has exactly one safe sequence. A state can have several safe sequences or just one. It needs only one to be safe.
A grant that makes the state unsafe
Suppose P2 now requests one more instance, and the system grants it. The new state is:
| Process | Max | Allocation | Need |
|---|---|---|---|
| P0 | 10 | 5 | 5 |
| P1 | 4 | 2 | 2 |
| P2 | 9 | 3 | 6 |
Available is now 2. Only P1 can proceed (Need 2 ≤ 2), and Work becomes 4. After that, P0 needs 5 and P2 needs 6, and neither fits in 4. No safe sequence exists, so the state is unsafe.
Nothing is deadlocked yet. P1 can still finish. But if P0 and P2 each ask for their full remaining need while P1 holds on, the system gets stuck. The Banker's algorithm would have delayed P2's request, and P2 would have waited even though one instance was free.
Checking by brute force
For tiny cases, the definition can be coded directly: try every ordering and see whether any one works. This is far too slow for real use, because there are n! orderings. The next chapter replaces it with a linear-time-per-pass method. It is still useful here, because it matches the definition word for word.
from itertools import permutations
def safe_sequences(available, allocation, max_claim):
"""Single resource type. Return every safe sequence (list of process indices)."""
n = len(allocation)
need = [max_claim[i] - allocation[i] for i in range(n)]
result = []
for order in permutations(range(n)):
work = available
ok = True
for p in order:
if need[p] > work:
ok = False
break
work += allocation[p]
if ok:
result.append(list(order))
return result
# Safe state from the table above
print(safe_sequences(3, [5, 2, 2], [10, 4, 9])) # [[1, 0, 2]]
# After granting P2 one more instance
print(safe_sequences(2, [5, 2, 3], [10, 4, 9])) # []An empty list means the state is unsafe. A non-empty list means it is safe, and each entry is one valid sequence.
Why unsafe does not mean deadlocked
Safety is judged against the worst case. The check assumes every process may ask for its whole Need at the worst moment. Real processes often do not. In the unsafe state above, P1 might finish and release its 2 instances, and P0 might finish before P2 asks for more. Then the system recovers without any deadlock.
This explains the real cost of avoidance. The system refuses some requests that would have been harmless. Resource utilization drops, and processes may wait while resources sit free. In return, the system never needs to detect or break a deadlock.
Multiple resource types
With several resource types, Need, Work and Available become vectors with one entry per type. The comparison "Need fits in Work" requires every component to fit:
Need[i] <= WorkmeansNeed[i][j] <= Work[j]for every resource typej.- It is not enough that the total number of instances is sufficient. A process that needs 2 of type B cannot proceed on 5 spare instances of type A.
When a process finishes in the hand check, add its whole Allocation row back to Work, component by component.
Common mistakes
- Using Max instead of Need. The test compares Need (Max − Allocation) against Work. Using Max makes almost every state look unsafe.
- Forgetting to add the allocation back. Work must grow after each process finishes. Without it, you stop too early.
- Testing only the first process. The sequence must include all processes. One stuck process makes the state unsafe, even if the others finish.
- Assuming one sequence is the only one. Different valid orders exist. When a question asks "which of these is a safe sequence", test each listed order against the definition in turn.
- Confusing a safe sequence with the order processes actually run. The sequence is a proof that a plan exists. The actual execution can differ.
Summary of the idea
Avoidance replaces the question "is there a deadlock now?" with "is there still a guaranteed way to finish?" A safe state answers yes. An unsafe state answers "not guaranteed", and the system declines to enter it. The next chapter turns this definition into the safety algorithm with the Available, Allocation, Max and Need matrices.
Exercise
A system has one resource type with 10 instances. Process P0 has Max 9 and holds 3. P1 has Max 4 and holds 2. P2 has Max 7 and holds 2. (a) Is the state safe? Give a safe sequence and say how many safe sequences exist. (b) P2 requests one more instance. Would granting it keep the state safe?
Show answer
Available = 10 − (3 + 2 + 2) = 3. Needs are P0: 6, P1: 2, P2: 5. (a) P0 and P2 cannot go first (6 > 3 and 5 > 3), but P1 can (2 ≤ 3). After P1, Work = 5. P0 still cannot go (6 > 5), but P2 can (5 ≤ 5). After P2, Work = 7. P0 then goes (6 ≤ 7). The state is safe, with safe sequence <P1, P2, P0>. It is the only one, so there is exactly one safe sequence. (b) After the grant, P2 holds 3 with Need 4, and Available = 2. P1 can go (2 ≤ 2), giving Work = 4. P2 can go (4 ≤ 4), giving Work = 7. P0 can go (6 ≤ 7). The sequence <P1, P2, P0> still works, so the state is safe and the request can be granted.