Deadlock Prevention And Avoidance
Last Updated :
23 Jul, 2025
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.
Hold & WaitThere 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:
Available system resources are:
Processes (currently allocated resources):
Maximum resources we have for a process:
Need = Maximum Resources Requirement - Currently Allocated Resources
Deadlock Prevention in Operating System
Deadlock | Deadlock Prevention in Operating System
Deadlock | Deadlock Avoidance in Operating System
Similar Reads
Operating System Tutorial An Operating System(OS) is a software that manages and handles hardware and software resources of a computing device. Responsible for managing and controlling all the activities and sharing of computer resources among different running applications.A low-level Software that includes all the basic fu
4 min read
OS Basics
Process & Threads
CPU Scheduling
Deadlock
Memory & Disk Management
Memory Management in Operating SystemMemory is a hardware component that stores data, instructions and information temporarily or permanently for processing. It consists of an array of bytes or words, each with a unique address. Memory holds both input data and program instructions needed for the CPU to execute tasks.Memory works close
7 min read
Fixed (or static) Partitioning in Operating SystemFixed partitioning, also known as static partitioning, is one of the earliest memory management techniques used in operating systems. In this method, the main memory is divided into a fixed number of partitions at system startup, and each partition is allocated to a process. These partitions remain
8 min read
Variable (or Dynamic) Partitioning in Operating SystemIn operating systems, Memory Management is the function responsible for allocating and managing a computerâs main memory. The memory Management function keeps track of the status of each memory location, either allocated or free to ensure effective and efficient use of Primary Memory. Below are Memo
4 min read
Paging in Operating SystemPaging is the process of moving parts of a program, called pages, from secondary storage (like a hard drive) into the main memory (RAM). The main idea behind paging is to break a program into smaller fixed-size blocks called pages.To keep track of where each page is stored in memory, the operating s
8 min read
Segmentation in Operating SystemA process is divided into Segments. The chunks that a program is divided into which are not necessarily all of the exact sizes are called segments. Segmentation gives the user's view of the process which paging does not provide. Here the user's view is mapped to physical memory. Types of Segmentatio
4 min read
Segmentation in Operating SystemA process is divided into Segments. The chunks that a program is divided into which are not necessarily all of the exact sizes are called segments. Segmentation gives the user's view of the process which paging does not provide. Here the user's view is mapped to physical memory. Types of Segmentatio
4 min read
Page Replacement Algorithms in Operating SystemsIn an operating system that uses paging for memory management, a page replacement algorithm is needed to decide which page needs to be replaced when a new page comes in. Page replacement becomes necessary when a page fault occurs and no free page frames are in memory. in this article, we will discus
7 min read
File Systems in Operating SystemA computer file is defined as a medium used for saving and managing data in the computer system. The data stored in the computer system is completely in digital format, although there can be various types of files that help us to store the data.File systems are a crucial part of any operating system
8 min read
File Systems in Operating SystemA computer file is defined as a medium used for saving and managing data in the computer system. The data stored in the computer system is completely in digital format, although there can be various types of files that help us to store the data.File systems are a crucial part of any operating system
8 min read
Advanced OS
Practice