Sub topic 14
GATE problem patterns and shortcuts
This chapter collects the question types that appear most often in GATE and the fastest reliable method for each. It assumes you know the four Coffman conditions, resource-allocation graphs, the safety algorithm, the Banker's algorithm and the detection algorithms from the earlier chapters. You should also be comfortable comparing vectors element by element. The aim is speed without losing correctness, so each pattern comes with its trap.
Pattern 1: the minimum number of resources that guarantees no deadlock
Typical wording: "A system has n processes, each needing at most k units of a single resource type. What is the minimum number of units that guarantees freedom from deadlock?"
Use the worst case. Deadlock is possible when every process holds one unit fewer than its maximum and no process can finish. A deadlock-free system must have at least one more unit than that worst case.
For processes with maximum claims m1, m2, ..., mn:
- Worst-case units held while deadlocked: (m1 − 1) + (m2 − 1) + ... + (mn − 1).
- Minimum units that guarantees no deadlock: that sum plus 1.
- For equal claims k: R ≥ n(k − 1) + 1.
def min_resources_no_deadlock(max_claims):
# Worst case: every process holds (claim - 1) units and waits for one more.
return sum(c - 1 for c in max_claims) + 1
print(min_resources_no_deadlock([2, 2, 2])) # 4
print(min_resources_no_deadlock([3, 4, 5])) # 10The reverse question asks for the maximum number of processes that can run without deadlock for a given R. Solve n(k − 1) + 1 ≤ R, so n ≤ (R − 1)/(k − 1), and round down. With R = 6 and k = 3, n ≤ 2.5, so the answer is 2. Check: n = 3 needs 7 units, which is more than 6.
A related question asks for the largest number of units for which deadlock is still possible. That is the worst-case sum, without the +1.
Pattern 2: is this state safe, or which sequence is safe?
Always compute Need = Max − Allocation first. Most wrong answers come from comparing Max against Available by mistake.
Use the state from the safety-algorithm chapter. Total resources are (10, 5, 7).
| 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 is (3, 3, 2). Work through the sequence P1, P3, P4, P0, P2:
- P1: Need (1,2,2) ≤ (3,3,2). Work becomes (5,3,2).
- P3: Need (0,1,1) ≤ (5,3,2). Work becomes (7,4,3).
- P4: Need (4,3,1) ≤ (7,4,3). Work becomes (7,4,5).
- P0: Need (7,4,3) ≤ (7,4,5). Work becomes (7,5,5).
- P2: Need (6,0,0) ≤ (7,5,5). Work becomes (10,5,7), which equals the total.
Shortcuts for this pattern:
- The final Work must equal the total resources. If it does not, you made an arithmetic error.
- When a question offers several candidate sequences, test each one by simulation. Reject a sequence at the first process whose Need does not fit the current Work.
- A safe sequence is not unique. P3, P1, P4, P0, P2 also works here. Do not conclude a sequence is wrong just because it differs from your own.
- To find the minimum Available that makes a state safe, look at the process with the smallest Need. Available must cover it, and the released Allocations must then cover the next smallest Need, and so on. Increase Available one unit at a time and rerun the check.
Pattern 3: can this request be granted?
The Banker's algorithm uses three checks, in order:
- Request ≤ Need. If not, the process has exceeded its declared claim, which is an error.
- Request ≤ Available. If not, the process waits.
- Pretend to grant the request and run the safety algorithm. Grant only if the new state is safe.
Take P1 requesting (1, 0, 2) in the state above. Check 1: (1,0,2) ≤ (1,2,2). Check 2: (1,0,2) ≤ (3,3,2). After the tentative grant, Available is (2,3,0), P1 holds (3,0,2) and P1's Need is (0,2,0). P1 can finish, so Work becomes (5,3,2). From there the earlier sequence continues, so the state is safe and the request is granted.
Now consider P4 requesting (3, 3, 0) in the original state. Check 1 passes, since (3,3,0) ≤ (4,3,1). Check 2 passes, since (3,3,0) ≤ (3,3,2). But the new Available is (0,0,2). P0 needs (7,4,3), P1 needs (1,2,2), P2 needs (6,0,0), P3 needs (0,1,1) and P4 needs (1,0,1). None fits (0,0,2) except P4, which needs (1,0,1) and has one unit too few in the first position. No process can start, so the state is unsafe and the request is delayed.
Pattern 4: detection with multiple instances
Detection uses the same loop as the safety algorithm, with two differences:
- It compares the current Request matrix against Work, not Need.
- It starts with finish = true for any process holding no resources, because such a process cannot be part of a deadlock.
Processes left unfinished at the end are the deadlocked ones.
def deadlocked(available, allocation, request):
n, m = len(allocation), len(available)
work = list(available)
finish = [all(a == 0 for a in allocation[i]) for i in range(n)]
changed = True
while changed:
changed = False
for i in range(n):
if not finish[i] and all(request[i][j] <= work[j] for j in range(m)):
for j in range(m):
work[j] += allocation[i][j]
finish[i] = True
changed = True
return [i for i in range(n) if not finish[i]]
available = [0, 0]
allocation = [[1, 0], [0, 1], [0, 0]]
request = [[0, 1], [1, 0], [0, 0]]
print(deadlocked(available, allocation, request)) # [0, 1]P2 holds nothing and requests nothing, so it finishes at once but releases nothing. P0 and P1 each wait for the unit the other holds, so both are deadlocked.
Pattern 5: reading graphs
The cycle rule is the most frequent conceptual question:
| Graph situation | A cycle means |
|---|---|
| Resource-allocation graph, one instance per type | Deadlock. The cycle is necessary and sufficient. |
| Resource-allocation graph, multiple instances | Deadlock is possible, not certain. The cycle is necessary only. |
| Wait-for graph, one instance per type | Deadlock. |
For a multi-instance graph with a cycle, look for a process outside the cycle that holds an instance of a cycle resource and is not itself waiting. When that process finishes, it releases the instance, and the cycle may break. If every process that could release one of those instances is itself in the cycle, the cycle is a deadlock.
To build a wait-for graph, remove the resource nodes. Draw an edge Pi → Pj when Pi waits for a resource that Pj holds.
Pattern 6: conditions and prevention statements
Statement questions test whether you can tie each technique to the condition it breaks.
| Technique | Condition broken | Cost |
|---|---|---|
| Make resources sharable (spooling) | Mutual exclusion | Impossible for some resources, such as a printer mid-job |
| Request all resources at start | Hold and wait | Low utilization, possible starvation |
| Release held resources before requesting more | Hold and wait | Work may be lost |
| Allow preemption | No preemption | Only works if state can be saved and restored |
| Global resource ordering | Circular wait | Programmers must follow the order |
Ordering is also the standard answer for lock-based code. If every thread takes lock_a before lock_b, no cycle can form. Remember these points:
- Prevention breaks a condition ahead of time. Avoidance allows all four conditions but refuses unsafe requests. Detection allows everything and cleans up afterwards.
- An unsafe state is not a deadlock. It only means deadlock is possible. A deadlocked state is always unsafe.
- Starvation means a process waits indefinitely while others progress. Livelock means processes keep changing state without progressing. Neither is deadlock.
- Avoidance requires each process to declare its maximum claim in advance.
Common errors
- Using Max in place of Need in the safety check.
- Forgetting to add the process's whole Allocation back to Work after it finishes.
- Treating a cycle in a multi-instance graph as certain deadlock.
- Granting a request after checking only Available.
- Using Need rather than Request in detection, or forgetting that zero-allocation processes finish.
- Applying the n(k − 1) + 1 formula to several resource types.
Exercise
Five processes each need at most 4 units of one resource type. What is the minimum number of units that guarantees deadlock cannot occur?
Show answer
Use n(k − 1) + 1 = 5 × 3 + 1 = 16 units. With 15 units, each process could hold 3 and all would wait for a fourth.
Exercise
A single-type system has 3 free units. P0 holds 2 and needs at most 5. P1 holds 3 and needs at most 7. P2 holds 1 and needs at most 4. Is the state safe? Give a safe sequence if one exists.
Show answer
Needs are P0 = 3, P1 = 4, P2 = 3. Available is 3. P0 fits, so Work becomes 3 + 2 = 5. P1 fits (4 ≤ 5), so Work becomes 8. P2 fits, so Work becomes 9. The state is safe, with sequence P0, P1, P2. Total is 9 = 3 + 2 + 3 + 1, which matches.