Sub topic 1
The resource model: processes, resources, and instances
Every deadlock question in GATE starts from the same model. A set of processes competes for a set of resources, and each resource type has a fixed number of interchangeable units. This chapter defines that model and the notation the later chapters use for the safety algorithm, the Banker's algorithm, and detection. Learn the vocabulary and the invariants here, because most numerical mistakes in this topic come from misreading the setup.
Prerequisites
This chapter assumes you know the following:
- What a process and a thread are, and why shared data needs a critical section.
- How a lock or semaphore enforces mutual exclusion, including that a second acquirer blocks until the holder releases.
- Directed graphs: nodes, edges, and what a cycle is.
- Matrix and vector notation. In particular, you should be able to compare two vectors element by element.
The three kinds of objects
The model has three ingredients.
- Processes are the active parties: P0, P1, ..., P(n-1). In deadlock questions, "process" and "thread" are interchangeable. Both ask for resources and hold them.
- Resource types are the categories of things that can be requested: R0, R1, ..., R(m-1). Examples are CPU cycles, memory pages, files, printers, database locks, and semaphores.
- Instances are the identical units within one resource type. If a system has three tape drives, the type "tape drive" has three instances.
The key assumption is that instances of one type are interchangeable. A request for one tape drive is satisfied by any free tape drive. The process never names a particular unit. This is why a resource is described by a count and not by a list of individual objects.
| Situation | Resource types | Instances |
|---|---|---|
| One mutex protecting a queue | 1 | 1 |
| Two printers and one scanner | 2 | 2 and 1 |
| A pool of 10 memory frames | 1 | 10 |
| A, B, C with 10, 5, 7 units | 3 | 10, 5, 7 |
A type with a single instance behaves like a lock. A type with many instances behaves like a counting semaphore. Later chapters treat the two cases differently. A cycle in a graph is enough to prove deadlock only in the single-instance case, so keep the instance count in view whenever you read a question.
Request, use, release
A process interacts with a resource in exactly three steps, always in this order.
- Request. The process asks for some number of instances. If they are not free, the process waits, blocked until they become free.
- Use. The process operates on the resource, for example printing or updating a shared structure.
- Release. The process gives the instances back so other processes can use them.
In real systems these steps are system calls or library calls: open and close, malloc and free, wait and signal on a semaphore, lock and unlock on a mutex.
The "Waiting" state is where deadlock lives. A process in that state holds every resource it has acquired so far and stays blocked. If everyone who could release what it needs is also blocked, nothing moves.
Kinds of resources
Questions sometimes say "reusable resource", so the distinction is worth knowing.
- Reusable resources are used by one process at a time and are not destroyed by use. Examples are processors, memory, files, devices, and locks. A process acquires them and later releases them. Deadlock theory in this topic is built around reusable resources.
- Consumable resources are created and destroyed, such as messages, signals, and interrupts. A producer creates one and a consumer destroys it. Deadlock can happen here too, for example when two processes each wait to receive a message from the other. This topic mostly ignores it.
A second axis is whether a resource can be taken away from its holder.
- A preemptable resource can be taken away and returned later without harming the holder. CPU registers and memory pages (through swapping) are examples.
- A non-preemptable resource cannot be taken away without making the computation fail. A printer halfway through a job and a lock held inside a critical section are examples.
This matters because the Coffman conditions in the next chapters need non-preemptable resources. If every resource could be taken back safely, deadlock could not persist.
The state of the system in matrix form
Algorithms need a compact description of "who holds what". With n processes and m resource types, the state is a handful of vectors and matrices. These names appear in every Banker's-algorithm question.
| Symbol | Shape | Meaning |
|---|---|---|
| Total | vector of length m | Instances of each type that exist |
| Available | vector of length m | Instances of each type that are free now |
| Max | n × m matrix | Most of each type process i may ever need |
| Allocation | n × m matrix | Instances of each type process i holds now |
| Need | n × m matrix | Instances process i may still request |
| Request | vector of length m | A new request made by one process |
Three relationships always hold in a consistent state.
- Need = Max − Allocation, element by element.
- Available = Total − (sum of all rows of Allocation). The sum is taken per column, one column for each resource type.
- Allocation ≤ Max for every process. A process cannot hold more than it declared it would ever need.
Max is also called the claim of a process. It is a promise: the process says in advance the most it will ever request. Avoidance algorithms rely on this promise, and prevention and detection do not.
Comparing vectors
The algorithms compare vectors, and the comparison is not the one you use for numbers. For vectors X and Y of equal length, X ≤ Y means X[j] ≤ Y[j] for every j. Two consequences matter.
- If any single position fails, then X ≤ Y is false.
- Two vectors can be incomparable. For (2, 0, 1) and (1, 3, 0), neither is ≤ the other. So "X is not ≤ Y" does not mean "Y < X".
Suppose Need for a process is (1, 2, 2) and Available is (3, 3, 2). Then Need ≤ Available, because 1 ≤ 3, 2 ≤ 3, and 2 ≤ 2. If Available were (3, 3, 1), the test would fail because of the last column alone.
Worked example: building the state
Take the usual textbook setting with 5 processes and three types A, B, C having 10, 5, and 7 instances. Allocation and Max are given, and we derive Available and Need. The code checks the invariants before computing, so it also catches inconsistent data.
def derive_state(total, allocation, max_need):
n = len(allocation)
m = len(total)
# Invariant: no process holds more than it declared
for i in range(n):
for j in range(m):
if allocation[i][j] > max_need[i][j]:
raise ValueError(f"P{i} holds more of type {j} than its Max")
# Invariant: no process may claim more than the system owns
for i in range(n):
for j in range(m):
if max_need[i][j] > total[j]:
raise ValueError(f"P{i} claims more of type {j} than exists")
# Available = Total - column sums of Allocation
available = [total[j] - sum(allocation[i][j] for i in range(n)) for j in range(m)]
if any(a < 0 for a in available):
raise ValueError("allocated more instances than exist")
# Need = Max - Allocation
need = [[max_need[i][j] - allocation[i][j] for j in range(m)] for i in range(n)]
return available, need
total = [10, 5, 7]
allocation = [[0, 1, 0], [2, 0, 0], [3, 0, 2], [2, 1, 1], [0, 0, 2]]
max_need = [[7, 5, 3], [3, 2, 2], [9, 0, 2], [2, 2, 2], [4, 3, 3]]
available, need = derive_state(total, allocation, max_need)
print("Available:", available)
for i, row in enumerate(need):
print(f"Need P{i}:", row)Running it prints:
Available: [3, 3, 2]
Need P0: [7, 4, 3]
Need P1: [1, 2, 2]
Need P2: [6, 0, 0]
Need P3: [0, 1, 1]
Need P4: [4, 3, 1]You can verify Available by hand. Column A of Allocation sums to 0 + 2 + 3 + 2 + 0 = 7, so 10 − 7 = 3. Column B sums to 2, so 5 − 2 = 3. Column C sums to 5, so 7 − 5 = 2. This state is reused in the chapters on the safety algorithm and the Banker's algorithm.
Common errors
- Row and column sums mixed up. Available subtracts the sum of each column of Allocation, because each column is one resource type. Summing a row gives the total held by one process, which is a different quantity.
- Treating Max as what remains. Max is the lifetime ceiling. What remains is Need. Using Max in place of Need in a safety check makes safe states look unsafe.
- Ignoring the instance count. "A cycle exists" proves deadlock only when each type has one instance. With several instances, a cycle is necessary but not sufficient. The chapter on resource-allocation graphs makes this precise.
- Treating instances as distinguishable. If a process needs "a printer", any free printer works. A process that insists on one named printer is better modeled as a separate resource type with one instance.
- Forgetting that a waiting process still holds resources. Blocking does not release anything. This is the root of the hold-and-wait behavior discussed later.
- Declaring Max above Total. A process that may need more than the system owns can never finish, so the data are invalid. The code above rejects it.
Why GATE leans on this model
GATE questions give you counts and matrices, then ask for a safe order, a minimum count, or whether a request can be granted. If you build Available and Need correctly first, the later steps become routine comparisons. If you get them wrong, every later answer is wrong. Practice deriving both quickly and checking that each column of Allocation plus Available does not exceed Total.
Exercise
A system has two resource types, X with 6 instances and Y with 4 instances. Three processes hold Allocation rows P0 = (1, 1), P1 = (2, 0), P2 = (1, 2), and declare Max rows P0 = (3, 2), P1 = (4, 1), P2 = (2, 3). Compute Available and the Need matrix.
Show answer
Column X of Allocation sums to 1 + 2 + 1 = 4, so Available X = 6 − 4 = 2. Column Y sums to 1 + 0 + 2 = 3, so Available Y = 4 − 3 = 1. Available = (2, 1). Need = Max − Allocation gives P0 = (2, 1), P1 = (2, 1), P2 = (1, 1).
Exercise
Available is (2, 1) and the Need rows are P0 = (2, 1), P1 = (2, 1), P2 = (1, 1). Which processes satisfy Need ≤ Available right now? Is (1, 3) ≤ (3, 1)?
Show answer
P0 passes because 2 ≤ 2 and 1 ≤ 1. P1 passes for the same reason. P2 passes because 1 ≤ 2 and 1 ≤ 1. So all three can currently be satisfied. For (1, 3) and (3, 1), the first position holds (1 ≤ 3) but the second fails (3 ≤ 1 is false), so (1, 3) ≤ (3, 1) is false. The reverse comparison also fails, so the two vectors are incomparable.
Exercise
A table shows Total = (5, 3), Allocation rows (1, 0), (2, 1), (1, 1), and Available = (2, 1). Is this state consistent?
Show answer
No. The Allocation column sums are 1 + 2 + 1 = 4 for the first type and 0 + 1 + 1 = 2 for the second. Available should be (5 − 4, 3 − 2) = (1, 1). The given (2, 1) means one instance of the first type is missing from the accounting, so the data are inconsistent.