Proving Deadlock Freedom with Four Identical Resources and Three Processes

Proving Deadlock Freedom with Four Identical Resources and Three Processes

Verified Sources
Sep 11, 2026

Consider a system with:

  • Four identical resource instances: R=4R = 4
  • Three processes: P1,P2,P3P_1, P_2, P_3
  • Each process requires at most two resource instances to complete: K=2K = 2

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

  1. 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:

ProcessResources currently heldAdditional resources needed
P1P_11111
P2P_21111
P3P_31111
Total3333

The three processes collectively hold only three resources. Because the system contains four resources,

43=14 - 3 = 1

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

  1. 1
    Step 1

    Suppose, for contradiction, that the three processes are deadlocked.

  2. 2
    Step 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.

  3. 3
    Step 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.

  4. 4
    Step 4

    The processes hold exactly three resources, while the system has four. Therefore, one resource is free.

  5. 5
    Step 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.

  6. 6
    Step 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.

  7. 7
    Step 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:

3×(21)=33 \times (2 - 1) = 3

resources: one less than the maximum requirement of each process.

The system has one additional resource:

43=14 - 3 = 1

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 nn processes, each requiring at most kk identical resources, is:

Rn(k1)+1R \geq n(k-1)+1

where:

  • RR is the number of resource instances,
  • nn is the number of processes,
  • kk is the maximum number of resources needed by any process.

For this problem:

R3(21)+1R \geq 3(2-1)+1 R4R \geq 4

The system has exactly four resources, so it satisfies the condition.

Footnotes

  1. Lec27 Deadlock | GATE Question Previous Year - Presents the identical-resource deadlock-free condition in the form Ri(Ai1)+1R \geq \sum_i (A_i-1)+1.

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 nn processes, each requiring at most kk instances of one identical resource type, then the system is deadlock-free whenever:

Rn(k1)+1R \geq n(k-1)+1

Proof

Assume the system has fewer than n(k1)+1n(k-1)+1 resources. In the worst case, every process holds k1k-1 resources and waits for one more. The total number of resources held is:

n(k1)n(k-1)

If only n(k1)n(k-1) 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:

Rn(k1)+1R \geq n(k-1)+1

Even if every process holds k1k-1 resources, at most n(k1)n(k-1) resources are occupied. Therefore, at least one resource remains free:

Rn(k1)1R-n(k-1)\geq 1

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

  1. 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:

ProcessAllocationMaximum claimRemaining need
P1P_1112211
P2P_2112211
P3P_3112211
Available11

The available vector contains one unit because:

Available=4(1+1+1)=1\text{Available}=4-(1+1+1)=1

Since each process has remaining need 11, any process can finish first. Choose P1P_1:

  1. Allocate the available resource to P1P_1.
  2. P1P_1 now holds two resources and completes.
  3. P1P_1 releases two resources.
  4. Available resources become:
11+2=21-1+2=2

Now either P2P_2 or P3P_3 can finish. For example:

P1,P2,P3\langle P_1,P_2,P_3\rangle

is a safe sequence. The existence of such a sequence proves that the state is safe.

Footnotes

  1. 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

  1. 1
    Step 1

    If each process holds one resource, then Available = 4 - 3 = 1.

  2. 2
    Step 2

    Each process needs at most one additional resource because its maximum claim is two and it already holds one.

  3. 3
    Step 3

    Any process whose remaining need is at most one can be selected; all three qualify.

  4. 4
    Step 4

    Give the selected process one resource. It reaches its maximum claim, completes, and releases both resources.

  5. 5
    Step 5

    After completion, the available count increases from one to two.

  6. 6
    Step 6

    Repeat the same procedure for the remaining processes. A complete safe sequence exists.

Deadlock-Freedom Flashcards

1 / 7
Question · Term

What is the maximum demand of each process?

Click to reveal
Answer · Definition

At most two instances of the single resource type.

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:

ProcessResources heldResources still needed
P1P_11111
P2P_21111
P3P_31111
Available00

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:

n(k1)+1=3(21)+1=4n(k-1)+1=3(2-1)+1=4

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:

  1. A deadlocked process must hold at least one resource.
  2. Since each process needs at most two resources, a process that is still waiting can hold at most one resource.
  3. Three processes can therefore hold at most three resources while all remain incomplete.
  4. The fourth resource is necessarily available.
  5. That resource lets one process obtain its second resource and complete.
  6. The completed process releases two resources.
  7. The same reasoning applies repeatedly to the remaining processes.

Therefore:

Four identical resources and three processes with maximum demand two are deadlock-free.\boxed{\text{Four identical resources and three processes with maximum demand two are deadlock-free.}}

Knowledge Check

Question 1 of 5
Q1Single choice

What is the only possible allocation pattern that could appear to cause deadlock in this system?

Explore Related Topics

1

Writing a C Program with `fork()` to Demonstrate the Parent-Child Relationship of Processes

The article shows how to write a C program that uses fork() to illustrate the parent‑child relationship of processes, their return values, IDs, concurrent execution, and proper synchronization.

  • fork() returns 0 in the child, the child’s PID in the parent, and -1 on error.
  • getpid() and getppid() reveal each process’s own and parent IDs; output order can vary because the two processes run concurrently.
  • The parent must call waitpid() (or wait()) to reap the child, prevent zombies, and decode its exit status with WIFEXITED/WEXITSTATUS.
  • Changing a variable (e.g., x) in the child demonstrates separate writable address spaces, thanks to copy‑on‑write.
  • Avoid placing fork() in an uncontrolled loop, as each successful fork doubles the process count from 11 to 22, then to 2n2^n, quickly exhausting system resources.
2

Round Robin Scheduling Analysis for Five Processes with Time Quantum 3 ms

3

The Banker's Algorithm: Deadlock Avoidance in Operating Systems

The Banker's Algorithm is a deadlock‑avoidance method that keeps a system in a safe state by checking each resource request against the maximum declared needs of processes.

  • Maintains Available, Max, Allocation, and Need matrices, where Need[i][j]=Max[i][j]Allocation[i][j]\text{Need}[i][j]=\text{Max}[i][j]-\text{Allocation}[i][j].
  • The Safety Algorithm uses vectors Work and Finish to find an execution order; if all processes finish, the state is safe.
  • The Resource‑Request Algorithm simulates allocation, runs the safety check, and commits only if the resulting state remains safe.
  • Time complexity of the safety check is O(m×n2)O(m \times n^{2}).
  • In practice the algorithm is rarely used because processes must predeclare maximum needs and the algorithm’s overhead is high.