Operating Systems · Deadlocks
Resources, Request-Use-Release and Resource Allocation Cycle
Overview
A resource is anything a process needs to complete its execution, and every process uses resources in a fixed pattern: request, use, release.
When a requested resource is busy, the process is blocked; if the wait is finite it is starvation, and if the wait never ends it is deadlock.
Deadlock is illustrated by a set of processes each holding one resource while waiting for a resource held by another, forming a cycle of waiting.
Key concepts
- Resource: anything in the computer that a process needs in order to complete its execution — a file, a variable, a semaphore, a register, the CPU, a printer; anything and everything in the computer is a resource.
A process must obtain the resources it needs from the operating system. - Request–Use–Release: the three operations a process performs on a resource. The process requests the resource from the OS, uses it, then releases it back to the OS.
- Blocked state: if the requested resource is not available, the OS delays granting it and the requesting process waits in the blocked state.
- Starvation: the process waits in the blocked state only for some time; it will eventually come out of the blocked state and run.
- Deadlock: every process in a set is holding some resources and waiting for other resources held by another process in the same set, so none of them can ever proceed — the waiting is forever.
- Cycle of waiting: the wait-for relations among the processes form a cycle (P1 → R2 → P2 → R3 → P3 → R1 → P1); this kind of waiting is called deadlock.
How it works
- A process requests a resource from the operating system.
- If the resource is available, the OS immediately grants it to the process.
- The process uses the resource.
- The process releases the resource and returns it back to the operating system.
- The process then completes its execution.
When the resource is not available:
- The resource may be busy because someone else is already using it.
For a mutex/semaphore, if another process has already performed adownon the mutex, the semaphore is not available and the requester must wait until it becomes available again.
For a printer, if there is only one printer and another process is already using it, the printer cannot be given until that process finishes printing. - The OS delays granting the resource, and the requesting process goes into the blocked state and waits for some time.
- Waiting in the blocked state only for some time (the process will eventually get out) is starvation; waiting in the blocked state forever (the process will never get out) is deadlock. The main difference between the two is the amount of time spent in the blocked state.
Resource allocation example with three processes P1, P2, P3 and three resources R1, R2, R3 (the resources may be printers, registers, files — anything in a computer):
| Process | Holds | Requests (waits for) |
|---|---|---|
| P1 | R1 | R2 |
| P2 | R2 | R3 |
| P3 | R3 | R1 |
- P1 asks for R1 and R1 is assigned to it → R1 is with P1.
- P2 wants R2 and gets it → R2 is with P2.
- P3 wants R3 and gets it → R3 is with P3.
- After some time, P1 asks for R2. R2 is under the control of P2, so P1 must wait until R2 becomes available.
- At the same time, P2 asks for R3. R3 is already with P3, so P2 must wait.
- At the same time, P3 asks for R1. R1 is with P1, so P3 must wait.
Every process has taken some resource and is waiting for another resource so that it can complete execution, but no other process is going to release what it holds. The wait-for relations form a cycle (P1 → R2 → P2 → R3 → P3 → R1 → P1); this kind of waiting is called deadlock.
Before declaring such a scenario a deadlock, the types of resources R1, R2 and R3 must be examined. The resource types must be examined first, and only then can the situation be confirmed as a deadlock.
Comparison
| Point of comparison | Starvation | Deadlock |
|---|---|---|
| State of the process | Blocked state | Blocked state |
| Duration of waiting | Only for some time | Forever |
| Does the process come out? | Yes — it eventually comes out of the blocked state and runs | No — it never comes out of the blocked state |
| Distinguishing factor | Amount of time in the blocked state | Amount of time in the blocked state (infinite) |
Revision summary
- A resource is anything a process needs: file, variable, semaphore, register, CPU, printer — anything in the computer.
- Every process performs exactly three operations on a resource: request → use → release, returning the resource to the OS.
- If the resource is free, the OS grants it immediately; if it is busy (mutex already
downed, single printer in use), the OS delays the grant and the process enters the blocked state. - Starvation = blocked for a finite time;
deadlock = blocked forever.
The only difference stated is the amount of time. - In the 3-process / 3-resource example, P1 holds R1 and wants R2, P2 holds R2 and wants R3, P3 holds R3 and wants R1 — a cycle of waiting in which no process will release what it holds.
- Trap: a cycle of waiting is called deadlock here, but the resource types must be examined first before declaring the situation a deadlock.
Previous-year exam insights
- A resource allocation graph uses circles for processes and rectangles for resource types, with dots for instances; a request edge Pi → Rj and an assignment edge Rj → Pi.
A cycle is necessary but not sufficient for deadlock: with single instances of each resource a cycle is necessary and sufficient, but with multiple instances a cycle does not imply deadlock.
Asked inISRO 2020UGC NET 2016GATE 2009 - The four necessary (Coffman) conditions for deadlock are mutual exclusion, hold-and-wait, no preemption, and circular wait; all four must hold simultaneously, and breaking any one prevents deadlock.
Asked inGATE 2009UGC NET 2016 - Deadlock handling strategies: prevention (break a Coffman condition), avoidance (Banker's algorithm / safe state using maximum claims), detection (wait-for graph or RAG reduction), and recovery (process termination or resource preemption).
Asked inGATE 2009 - A state is safe if there exists a sequence of processes (a safe sequence) such that each process's remaining maximum demand can be met by currently available resources plus those released by earlier processes; an unsafe state is not necessarily deadlocked but may lead to deadlock.
Asked inGATE 2009 - In a system with multiple instances, deadlock detection uses the available, allocation and request matrices and repeatedly finds a process whose request can be satisfied by available resources, releasing its allocation; if all processes can be finished there is no deadlock, otherwise the unfinished processes are deadlocked.
Asked inGATE 2009UGC NET 2016 - Starvation (indefinite blocking) can occur without deadlock, e.g. a low-priority process repeatedly bypassed by higher-priority ones, or in priority-based or shortest-job-first scheduling; aging is the standard remedy.
Asked inUGC NET 2016
Deadlock wait-for cycle
Drawing the diagram…
Shows the cycle of waiting P1 to R2 to P2 to R3 to P3 to R1 in which each process holds one resource and waits for another.