What deadlock means, why it occurs, the four necessary conditions, Resource Allocation Graph, deadlock prevention and avoidance, Banker’s Algorithm, safety algorithm, resource-request algorithm, deadlock detection and recovery, along with a complete solved numerical example and exam-oriented questions.
Modern operating systems allow many processes to execute concurrently. During execution, processes may require resources such as memory, files, devices, locks, printers, database objects, or other system resources. Since these resources are limited, the operating system has to decide when a resource can be assigned to a process.
Most resource requests are handled normally. A process receives the required resource, performs its operation, and eventually releases that resource. However, a special situation can occur when a group of processes starts waiting for resources held by one another.
If none of those processes can continue because each is waiting for another process to release a resource, the system may enter a deadlock.
Deadlock is therefore an important Operating System concept because it connects process management, resource allocation, synchronization, scheduling, and system performance.
A deadlock is a situation in which a set of processes becomes permanently blocked because every process in that set is waiting for a resource or event that can only become available after another process in the same set makes progress.
The important word is dependency. A process waiting for a resource is not automatically deadlocked. Waiting is normal in a multitasking operating system. Deadlock occurs when the waiting relationships form a situation from which the involved processes cannot progress under the current resource-allocation state.
Suppose P1 has acquired resource R1 and is waiting for R2. At the same time, P2 has acquired R2 and is waiting for R1. If both resources are exclusive and neither process can release what it holds until it gets the other resource, P1 and P2 remain blocked. This is a classic deadlock situation.
Consider two processes and two resources:
| Process | Resource Held | Resource Requested | Status |
|---|---|---|---|
| P1 | R1 | R2 | Waiting |
| P2 | R2 | R1 | Waiting |
P1 cannot proceed because R2 is held by P2. P2 cannot proceed because R1 is held by P1. Therefore, neither process can reach the point where it releases its current resource.
This circular dependency is the basic idea behind the classical deadlock example.
A classical deadlock requires four conditions to exist simultaneously. These are commonly referred to as the Coffman conditions. If at least one of these conditions can be prevented, the classical deadlock situation can be prevented.
Mutual exclusion means that a resource can be assigned to only one process at a particular time when the resource requires exclusive access.
For example, an exclusive lock cannot normally be owned by two processes simultaneously. While one process owns it, another process requesting the same lock must wait.
Hold and wait occurs when a process already holding one or more resources requests additional resources while continuing to hold the resources it already owns.
For example, P1 may hold R1 and request R2. If R2 is unavailable, P1 continues holding R1 while waiting.
No preemption means that a resource cannot simply be taken away from a process by the operating system while it is being used. The process normally has to release the resource voluntarily or reach an appropriate point where controlled preemption is safe.
Circular wait occurs when a sequence of processes exists in which every process is waiting for a resource held by the next process in the sequence.
The last process eventually waits for a resource held by the first process, completing the cycle.
Remember the four conditions as: Mutual Exclusion → Hold and Wait → No Preemption → Circular Wait.
A Resource Allocation Graph (RAG) is a graphical representation used to show the relationship between processes and resources.
It is particularly useful when studying resource requests, resource assignments, and possible circular dependencies.
Resource Allocation Graph representing a circular resource dependency.
A request edge is drawn from a process to a resource when the process is waiting for that resource.
An assignment edge is drawn from a resource to a process when the resource has already been allocated to that process.
A cycle is an important indication of circular dependency. However, its interpretation depends on the number of instances of each resource type.
With a single instance of each resource type, a cycle in the appropriate Resource Allocation Graph indicates deadlock. With multiple instances, a cycle alone does not necessarily prove deadlock; additional resource-state analysis is required.
An operating system can deal with deadlocks using different strategies. The major approaches are prevention, avoidance, detection and recovery, and in some systems, simply ignoring the possibility of deadlock.
| Approach | Basic Idea | Main Focus |
|---|---|---|
| Deadlock Prevention | Ensure that at least one necessary condition for deadlock cannot occur. | Prevent formation |
| Deadlock Avoidance | Grant resources only when the resulting state remains safe. | Safe allocation |
| Detection & Recovery | Allow allocation, detect deadlock later, and recover. | Find and break deadlock |
| Ignorance | Do not maintain a dedicated mechanism for handling every possible deadlock. | Low overhead |
Deadlock prevention works by ensuring that at least one of the four necessary conditions cannot be satisfied.
If a resource can safely be shared instead of being exclusively owned, sharing can reduce the possibility of deadlock.
However, many resources require exclusive access. Therefore, this method cannot be applied universally.
One approach is to require a process to request all the resources it needs before starting its execution phase that requires those resources.
Another approach is to make a process release its currently held resources before requesting additional resources.
The disadvantage is that resources may remain reserved even when the process is not currently using them.
If a resource can safely be preempted, the operating system may take it away from a process under suitable conditions.
This technique is difficult for resources whose state cannot be safely interrupted or restored.
A common strategy is to assign an ordering to resource types and require processes to request resources according to that order.
Suppose the resource order is R1 < R2 < R3. A process that has already requested R2 cannot later request R1. By enforcing the same ordering rule for all processes, circular waiting can be prevented.
Deadlock avoidance does not necessarily eliminate the four necessary conditions. Instead, it evaluates resource requests and grants them only when doing so keeps the system in a state from which all processes can potentially complete.
The central concepts in deadlock avoidance are safe state and unsafe state.
A state is safe if there exists at least one possible sequence in which all processes can obtain their remaining resources, complete their execution, and release the resources they hold.
An unsafe state means that the operating system can no longer guarantee such a completion sequence using the current resource information.
Unsafe does not automatically mean that deadlock has already occurred. It means the system has lost the guarantee provided by a safe state.
Banker’s Algorithm is a classical deadlock-avoidance technique. It checks whether granting a resource request can leave the system in a safe state.
The algorithm assumes that the maximum possible resource requirement of each process is known. Before granting selected requests, the operating system performs a safety check.
| Term | Meaning |
|---|---|
| Available | Number of currently available instances of each resource type. |
| Max | Maximum number of resources of each type that a process may require. |
| Allocation | Resources currently allocated to the process. |
| Need | Remaining resources that the process may still require. |
Consider five processes P1, P2, P3, P4 and P5 and three resource types A, B and C.
| Process | Allocation (A B C) | Max (A B C) |
|---|---|---|
| P1 | 0 1 0 | 7 5 3 |
| P2 | 2 0 0 | 3 2 2 |
| P3 | 3 0 2 | 9 0 2 |
| P4 | 2 1 1 | 2 2 2 |
| P5 | 0 0 2 | 4 3 3 |
Available = (3, 3, 2)
Use the formula:
| Process | Max | Allocation | Need |
|---|---|---|---|
| P1 | (7, 5, 3) | (0, 1, 0) | (7, 4, 3) |
| P2 | (3, 2, 2) | (2, 0, 0) | (1, 2, 2) |
| P3 | (9, 0, 2) | (3, 0, 2) | (6, 0, 0) |
| P4 | (2, 2, 2) | (2, 1, 1) | (0, 1, 1) |
| P5 | (4, 3, 3) | (0, 0, 2) | (4, 3, 1) |
Initially:
P1 requires:
Compare:
Therefore P1 cannot be selected at this point.
P2 requires:
Since:
P2 can complete.
After P2 completes, its allocated resources are released:
P4 requires:
Since:
P4 can complete.
P5 requires:
Since:
P5 can complete.
P1 requires:
Current Work is (7, 4, 5). Therefore:
P1 can complete.
P3 requires:
Since:
P3 can also complete.
Since all five processes can be marked as completed in a valid order, the given state is a safe state.
The safety algorithm determines whether the current resource allocation state is safe.
In the safety algorithm, after assuming that a process completes, add its Allocation to Work. Do not add its Max or Need matrix.
The resource-request algorithm determines whether a particular process request can be granted without making the system unsafe.
If this condition fails, the process is requesting more resources than its declared maximum remaining requirement.
If sufficient resources are not currently available, the process must wait.
If both conditions are satisfied, temporarily update the resource values:
Run the safety algorithm on the temporary state. If the state remains safe, the request can be granted. Otherwise, the temporary allocation must be cancelled and the process waits.
In deadlock detection, the system does not necessarily prevent every potentially unsafe allocation. Instead, it allows resource allocation according to its normal policy and periodically or when appropriate checks whether a deadlock has formed.
When each resource type has only one instance, a Wait-For Graph can be used.
In a wait-for graph, only processes are represented. An edge Pi → Pj means that Pi is waiting for a resource currently held by Pj.
In the single-instance case, such a cycle represents a deadlock among those processes.
When resource types have multiple instances, cycle detection alone is insufficient. The available resource vector, current allocations, and outstanding requests must also be considered.
The system can simulate completion of processes whose remaining requests can currently be satisfied. When a process is assumed to finish, its allocated resources are returned to the available pool.
Once a deadlock has been detected, the operating system needs to break the dependency so that useful work can continue.
One or more processes involved in the deadlock may be terminated. Terminating a process releases the resources held by that process.
The operating system may consider process priority, amount of work already completed, number of resources held, restart cost, and importance of the process before selecting a victim.
If the resource supports safe preemption, a resource can be removed from one process and assigned to another process.
The affected process may need to wait, restart, or be rolled back depending on the resource and application.
Rollback restores a process to an earlier safe state, often using checkpoint information. After releasing selected resources, the system can allow other processes to complete.
Some operating systems or environments may choose not to maintain a comprehensive deadlock-handling mechanism for every possible situation. This approach is sometimes described using the Ostrich approach.
The reasoning is that the cost of continuously preventing or detecting every possible deadlock may be greater than the expected benefit for a particular environment.
The disadvantage is that an actual deadlock may require an application restart, process termination, or user intervention.
| Basis | Prevention | Avoidance | Detection & Recovery |
|---|---|---|---|
| Main Idea | Break at least one necessary condition. | Keep allocation in a safe state. | Detect deadlock after it forms and recover. |
| Typical Technique | Resource ordering, release policies. | Banker’s Algorithm. | Detection algorithm and recovery. |
| Timing | Before deadlock formation. | Before selected allocations. | After possible deadlock formation. |
| Advantage | Deadlock can be prevented by design. | More flexible than strict prevention. | Normal allocation can continue. |
| Limitation | Can reduce flexibility. | Needs additional information. | Recovery may lose work. |
Deadlock and starvation both involve processes waiting, but they are different problems.
| Basis | Deadlock | Starvation |
|---|---|---|
| Meaning | Processes are stuck because of a dependency cycle. | A process waits for an unusually long time because other processes repeatedly receive the required resource or CPU. |
| Circular Wait | Important condition in classical deadlock. | Not required. |
| Progress | Involved processes cannot progress without breaking the dependency. | The waiting process may eventually progress. |
| Typical Solution | Prevention, avoidance, detection and recovery. | Fair scheduling and aging. |
Deadlock is a situation in which a group of processes remains blocked because each process is waiting for a resource or condition that depends on another process in the same group.
The four necessary conditions are mutual exclusion, hold and wait, no preemption and circular wait.
A Resource Allocation Graph is a graphical representation of processes, resources, resource requests and resource assignments. It helps visualize resource dependencies.
Banker’s Algorithm is a classical deadlock-avoidance technique that checks whether granting resources can leave the system in a safe state.
The formula is: Need = Max − Allocation. The subtraction is performed independently for each resource type.
A safe sequence is an ordering of processes in which each process can obtain its remaining required resources, complete its work, and release its allocated resources so that the next process can continue.
No. An unsafe state means that the system cannot guarantee a safe completion sequence under the given assumptions. It does not by itself prove that deadlock has already occurred.
Deadlock can be prevented by ensuring that at least one of the four necessary conditions cannot occur. Resource ordering, avoiding hold-and-wait, controlled preemption and safe sharing are examples of prevention strategies.
Deadlock involves a dependency in which processes prevent one another from progressing. Starvation occurs when a process waits for a long time because the scheduling or allocation policy keeps favoring other processes.
Common recovery techniques include terminating selected processes, preempting resources where safe, and rolling processes back to an earlier recoverable state.
| Topic | Important Point |
|---|---|
| Deadlock | Processes remain blocked because of unresolved resource dependencies. |
| Four Conditions | Mutual Exclusion, Hold and Wait, No Preemption, Circular Wait. |
| RAG | Represents process-resource requests and assignments. |
| Prevention | Break at least one necessary condition. |
| Avoidance | Grant resources while maintaining a safe state. |
| Banker’s Algorithm | Uses Available, Max, Allocation and Need. |
| Safety Algorithm | Determines whether a safe completion sequence exists. |
| Detection | Identifies deadlock after resource allocation. |
| Recovery | Terminates processes, preempts resources or rolls back processes where appropriate. |
Deadlock is one of the most important topics in Operating Systems because it demonstrates how processes and limited resources can interact in unexpected ways. The key to understanding the topic is not simply memorizing its definition but learning how resource dependencies are created and how those dependencies can be controlled.
The four necessary conditions provide the foundation for deadlock analysis. Resource Allocation Graphs help visualize the relationships, while prevention and avoidance provide ways of reducing the risk of deadlock. Banker’s Algorithm is particularly important for numerical questions because it provides a systematic way to determine whether a state is safe.
For exam problems involving Banker’s Algorithm, remember the practical sequence: calculate Need, set Work equal to Available, find an eligible process, add its Allocation to Work, and repeat until a safe sequence is obtained or no eligible process remains.
Understanding these steps makes deadlock questions much easier to solve in semester examinations, competitive examinations, viva, interviews and Operating System revision.