Open In App

Deadlock Prevention And Avoidance

Last Updated : 23 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Deadlock prevention and avoidance are strategies used in computer systems to ensure that different processes can run smoothly without getting stuck waiting for each other forever. Think of it like a traffic system where cars (processes) must move through intersections (resources) without getting into a gridlock.

Necessary Conditions for Deadlock

  • Mutual Exclusion
  • Hold and Wait
  • No Preemption
  • Circular Wait

Please refer Conditions for Deadlock in OS for details.

Deadlock Prevention

We can prevent a Deadlock by eliminating any of the above four conditions. 

Eliminate Mutual Exclusion

It is not possible to violate mutual exclusion because some resources, such as the tape drive, are inherently non-shareable. For other resources, like printers, we can use a technique called Spooling (Simultaneous Peripheral Operations Online).

In spooling, when multiple processes request the printer, their jobs (instructions of the processes that require printer access) are added to the queue in the spooler directory. The printer is allocated to jobs on a First-Come, First-Served (FCFS) basis. In this way, a process does not have to wait for the printer and can continue its work after adding its job to the queue.

Eliminate Hold and Wait

Hold and wait is a condition in which a process holds one resource while simultaneously waiting for another resource that is being held by a different process. The process cannot continue until it gets all the required resources.

HoldWait
Hold & Wait

There are two ways to eliminate hold and wait:

  • By eliminating wait: The process specifies the resources it requires in advance so that it does not have to wait for allocation after execution starts.
    For Example, Process1 declares in advance that it requires both Resource1 and Resource2.
  • By eliminating hold: The process has to release all resources it is currently holding before making a new request.
    For Example: Process1 must release Resource2 and Resource3 before requesting Resource1.

Eliminate No Preemption

Preemption is temporarily interrupting an executing task and later resuming it. Two ways to eliminate No Preemption:

  • Processes must release resources voluntarily: A process should only give up resources it holds when it completes its task or no longer needs them.
  • Avoid partial allocation: Allocate all required resources to a process at once before it begins execution. If not all resources are available, the process must wait.

Eliminate Circular Wait

To eliminate circular wait for deadlock prevention, we can use order on resource allocation.

  • Assign a unique number to each resource.
  • Processes can only request resources in an increasing order of their numbers.

This prevents circular chains of processes waiting for resources, as no process can request a resource lower than what it already holds.

Detection and Recovery

Another approach to dealing with deadlocks is to detect and recover from them when they occur. This can involve killing one or more of the processes involved in the deadlock or releasing some of the resources they hold.

Deadlock Avoidance

Deadlock avoidance ensures that a resource request is only granted if it won't lead to deadlock, either immediately or in the future. Since the kernel can't predict future process behavior, it uses a conservative approach. Each process declares the maximum number of resources it may need. The kernel allows requests in stages, checking for potential deadlocks before granting them. A request is granted only if no deadlock is possible; otherwise, it stays pending. This approach is conservative, as a process may finish without using the maximum resources it declared.

Banker's Algorithm is the technique used for Deadlock Avoidance.

Banker's Algorithm

Bankers' Algorithm is a resource allocation and deadlock avoidance algorithm that tests all resource requests made by processes. It checks for the safe state, and if granting a request keeps the system in safe state, the request is allowed. However, if no safe state exists, the request is denied. 

Inputs to Banker's Algorithm

  • Max needs of resources by each process. 
  • Currently, allocated resources by each process. 
  • Max free available resources in the system.

The request will only be granted under the below condition

  • If the request made by the process is less than equal to the max needed for that process. 
  • If the request made by the process is less than equal to the freely available resource in the system.

Timeouts

To avoid deadlocks caused by indefinite waiting, a timeout mechanism can be used to limit the amount of time a process can wait for a resource. If the help is unavailable within the timeout period, the process can be forced to release its current resources and try again later.

Example

Below is an example of a Banker's algorithm

Total resources in system:

A

B

C

D

6

5

7

6

Available system resources are:

A

B

C

D

3

1

1

2

Processes (currently allocated resources):


A

B

C

D

P1

1

2

2

1

P2

1

0

3

3

P3

1

2

1

0

Maximum resources we have for a process:


A

B

C

D

P1

3

3

2

2

P2

1

2

3

4

P3

1

3

5

0

Need = Maximum Resources Requirement - Currently Allocated Resources


A

B

C

D

P1

2

1

0

1

P2

0

2

0

1

P3

0

1

4

0


Deadlock Prevention in Operating System
Visit Course explore course icon
Video Thumbnail

Deadlock Prevention in Operating System

Video Thumbnail

Deadlock | Deadlock Prevention in Operating System

Video Thumbnail

Deadlock | Deadlock Avoidance in Operating System

Next Article

Similar Reads