Proving Deadlock Freedom with Four Identical Resources and Three Processes
Consider a system with:
- Four identical resource instances:
- Three processes:
- Each process requires at most two resource instances to complete:
Assume resources are allocated one at a time, a process keeps any resources already allocated to it, and it releases all resources after completing. A deadlock requires processes to remain permanently unable to obtain the resources they need. Classical deadlock analysis identifies mutual exclusion, hold-and-wait, no preemption, and circular wait as necessary conditions.
The goal is to show that no possible allocation state can produce deadlock.
Footnotes
-
Operating Systems: Deadlocks - Defines the four necessary conditions for deadlock and explains resource-allocation concepts. ↩
Core Result
With four identical resources and three processes requiring at most two resources each, at least one resource is always available whenever all three processes hold one resource. Therefore, one process can always obtain its second resource, finish, and release both resources.
1. The Key Counting Argument
A deadlock could occur only if every process were waiting for one additional resource while already holding one. Since each process needs at most two resources, the most dangerous configuration is:
| Process | Resources currently held | Additional resources needed |
|---|---|---|
| Total |
The three processes collectively hold only three resources. Because the system contains four resources,
resource remains available.
Thus, at least one process can receive its remaining resource. It then holds two resources, reaches its maximum requirement, completes, and releases both resources. The released resources allow the remaining processes to complete in turn.
A safe state is therefore guaranteed.
2. Visualizing the Worst Case
The apparent worst case is that all three processes hold one resource and wait for one more. However, that state consumes only three of the four resources. The fourth resource breaks the potential waiting cycle.
A resource instance is interchangeable with every other instance, so no process needs a particular physical resource.
Direct Proof by Cases
- 1Step 1
Suppose, for contradiction, that the three processes are deadlocked.
- 2Step 2
Because every process needs at most two resources, a process that holds zero resources cannot be part of a hold-and-wait deadlock. A deadlocked process must therefore hold at least one resource and wait for another.
- 3Step 3
With three processes, each holding at least one resource, at least three resources are allocated. If any process held two resources, it would already have all resources it needs and could complete, so the only possible deadlock configuration is one resource held by each process.
- 4Step 4
The processes hold exactly three resources, while the system has four. Therefore, one resource is free.
- 5Step 5
The free resource can be allocated to any process holding one resource. That process now holds two resources, which is its maximum requirement, so it can complete and release both resources.
- 6Step 6
At least two resources are available after the first process releases its resources. The same reasoning allows a second process and then the third process to complete.
- 7Step 7
Since every process can complete, the assumed deadlock cannot exist. Therefore, the system is deadlock-free.
3. Why the Contradiction Works
The contradiction depends on the maximum requirement of two resources.
If all three processes are waiting, each must already hold one resource. That uses:
resources: one less than the maximum requirement of each process.
The system has one additional resource:
That extra resource is enough to satisfy one process's remaining need. Once that process completes, it returns two resources, making completion of the others even easier.
The general sufficient condition for processes, each requiring at most identical resources, is:
where:
- is the number of resource instances,
- is the number of processes,
- is the maximum number of resources needed by any process.
For this problem:
The system has exactly four resources, so it satisfies the condition.
Footnotes
-
Lec27 Deadlock | GATE Question Previous Year - Presents the identical-resource deadlock-free condition in the form . ↩
Resource Requirement Comparison
The system has one more resource than the maximum number that can be held while every process waits for one additional resource.
4. The General Theorem
Theorem
If a system has processes, each requiring at most instances of one identical resource type, then the system is deadlock-free whenever:
Proof
Assume the system has fewer than resources. In the worst case, every process holds resources and waits for one more. The total number of resources held is:
If only resources are available, every resource may be allocated while every process still needs one additional resource. No process can complete, so deadlock is possible.
Now suppose instead that:
Even if every process holds resources, at most resources are occupied. Therefore, at least one resource remains free:
That resource completes the remaining requirement of one process. The process finishes and releases all its resources. The argument can then be repeated for every remaining process. Hence, deadlock cannot occur.
Footnotes
-
Deadlock Avoidance in OS - Illustrates the resource-counting argument and the boundary between deadlock-free and potentially deadlocked allocations. ↩
Important Interpretations and Edge Cases
Do Not Confuse Deadlock with Starvation
Deadlock means that every process in a set is permanently blocked by the others. Starvation means that a process may wait indefinitely even though other processes continue to make progress. This counting proof rules out deadlock, but it does not by itself guarantee scheduling fairness.
5. Banker-Style Safety Interpretation
The same result can be expressed using the idea of a safe sequence.
Suppose the current allocation is the worst possible deadlock candidate:
| Process | Allocation | Maximum claim | Remaining need |
|---|---|---|---|
| Available |
The available vector contains one unit because:
Since each process has remaining need , any process can finish first. Choose :
- Allocate the available resource to .
- now holds two resources and completes.
- releases two resources.
- Available resources become:
Now either or can finish. For example:
is a safe sequence. The existence of such a sequence proves that the state is safe.
Footnotes
-
Chapter 7: Deadlocks - Describes safe states, remaining need, and the existence of a safe completion sequence in Banker-style reasoning. ↩
Safety Check for the Concrete System
- 1Step 1
If each process holds one resource, then Available = 4 - 3 = 1.
- 2Step 2
Each process needs at most one additional resource because its maximum claim is two and it already holds one.
- 3Step 3
Any process whose remaining need is at most one can be selected; all three qualify.
- 4Step 4
Give the selected process one resource. It reaches its maximum claim, completes, and releases both resources.
- 5Step 5
After completion, the available count increases from one to two.
- 6Step 6
Repeat the same procedure for the remaining processes. A complete safe sequence exists.
Deadlock-Freedom Flashcards
6. Why Three Resources Would Not Be Enough
The result is tight. If the system had only three resources, the following allocation would be possible:
| Process | Resources held | Resources still needed |
|---|---|---|
| Available |
Every process holds one resource and waits for a second. No resource is available, so no process can reach its maximum requirement or release its held resource.
Thus, with three resources, deadlock is possible. The fourth resource is not merely convenient; it is exactly the additional resource required by the theorem:
Three Resources versus Four Resources
Three resources permit a deadlocked allocation; four resources guarantee progress.
Exam Technique
For identical resources, identify the maximum number that can be held while every process is still incomplete: n(k−1). Add one resource. For this problem, 3(2−1)+1 = 4.
7. Final Conclusion
The system is deadlock-free because:
- A deadlocked process must hold at least one resource.
- Since each process needs at most two resources, a process that is still waiting can hold at most one resource.
- Three processes can therefore hold at most three resources while all remain incomplete.
- The fourth resource is necessarily available.
- That resource lets one process obtain its second resource and complete.
- The completed process releases two resources.
- The same reasoning applies repeatedly to the remaining processes.
Therefore:
Knowledge Check
What is the only possible allocation pattern that could appear to cause deadlock in this system?
Explore Related Topics
CPU Scheduling Case Study: FCFS vs SJF for a Five-Process Workload
The case study compares FCFS and non‑preemptive SJF scheduling for five processes that all arrive at time 0, showing their Gantt charts, individual waiting times, and average waiting times.
- FCFS order A → B → C → D → E; waiting times 0, 10, 11, 13, 14 ms; average ms.
- SJF order B → D → C → E → A (ties broken by arrival order); waiting times 0, 1, 2, 4, 9 ms; average ms.
- With simultaneous arrivals, each process’s waiting time equals the sum of burst times of all jobs scheduled before it.
- Non‑preemptive SJF is optimal for minimizing average waiting time when burst lengths are known.
- The priority column is irrelevant for this comparison.
Deadlock Avoidance: Identifying the Correct Algorithm Among Options
Contiguous Memory Allocation: Understanding the Correct Statement