Sub topic 14
GATE Problem Patterns and Shortcuts
Assumptions and a fast decision process
This chapter assumes you know processes, locks or semaphores, directed cycles, and element-wise vector comparison. For a GATE question, first identify whether it asks about prevention, avoidance, detection, or recovery. Then check whether each resource type has one instance or multiple instances.
| Question pattern | Fast test |
|---|---|
| Single instance per resource | A cycle in the resource-allocation graph means deadlock |
| Multiple instances | A cycle may not mean deadlock; use a matrix algorithm |
| Banker's algorithm | Grant only if the resulting state is safe |
| Minimum resources for a guarantee | Ensure at least one process can obtain its final resource |
The minimum-resource shortcut
Suppose there are n processes. Every process needs at most k identical resource instances to finish, and each process releases all its resources after finishing.
A deadlock can occur only if every process holds at least one resource and waits for another. Before any process can finish, the maximum number of resources that can be held without allowing completion is:
n × (k - 1)
Therefore, the minimum number that guarantees freedom from deadlock is:
n(k - 1) + 1
The extra resource lets at least one process reach its maximum claim. That process finishes and releases its resources. The same reasoning then applies to the remaining processes.
If process Pi has maximum claim Mi, the corresponding bound is:
Σ(Mi - 1) + 1
These formulas require identical resources and the stated maximum claims. Do not apply them to several resource types without checking the exact assumptions.
Graph shortcuts
For one instance of every resource type, convert the resource-allocation graph into a wait-for graph. Replace:
Pi → Rj → PkwithPi → Pk
A cycle in the wait-for graph is both necessary and sufficient for deadlock.
With multiple instances, a cycle is only a warning. One process may still obtain a free instance and break the cycle. Use the safety or detection matrix instead.
Safety and request questions
For a safety question, calculate:
Need = Max - Allocation
Set Work = Available. Repeatedly find an unfinished process whose Need is less than or equal to Work. Pretend it finishes, then update:
Work = Work + Allocation
If every process can finish, the state is safe. The order found is a safe sequence. It is not necessarily the only sequence.
For a resource request by process Pi, check these conditions in order:
Requesti ≤ Needi. Otherwise the process exceeded its declared maximum.Requesti ≤ Available. Otherwise the process must wait immediately.- Tentatively grant the request and run the safety test. Grant it only if the new state is safe.
A safe state is not the same as a completed state. It means that some completion order exists. An unsafe state is not always currently deadlocked, but granting more resources without care could lead to deadlock.
> callout note > In matrix questions, compare vectors component by component. A single resource type that fails the comparison is enough to reject a candidate process.
Common GATE traps
- A cycle in a graph with multiple resource instances does not prove deadlock.
Maxis not the current request. The remaining claim isNeed.- In the safety algorithm, add a finishing process's
Allocationback toWork, not itsNeed. - A safe sequence is an order of possible completion, not an execution schedule forced by the operating system.
- Prevention and avoidance differ: prevention breaks a necessary condition, while avoidance examines each tentative allocation.
Exercise
Three processes each need at most four identical resource instances. What is the minimum number of instances that guarantees no deadlock under the standard maximum-claim assumptions?
Show answer
Use n(k - 1) + 1: 3(4 - 1) + 1 = 10 instances.