Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
In an operating system, a deadlock is a state where multiple processes get stuck indefinitely, waiting for resources held by each other. This occurs when processes form a circular dependency, preventing further execution.
In this blog, we will explore more about Deadlock in Operating System (OS).
What is Deadlock in OS?
A deadlock in an operating system is a condition where two or more processes are waiting for some event, but the event does not occur; in this case, those two processes are stuck in a deadlock state.
Deadlock occurs when a process cannot change its state indefinitely due to other processes using the resources it requests. Technically, there is no synchronization between the processes.
Consider two processes, Process_1 and Process_2. At the current moment, process_1 is waiting for Resource_2 while holding Resource_1.
While this is happening, Process_2 has been allocated to the resource Resource_2 and is waiting for Resource_1.
Fig. 1
As a result, Process_P1 awaits the release of its resource while Process_2 waits for Resource_1 allocated to Process_1 for release.
However, no resource is released. Therefore, both are waiting for each other to release the resource. There is no work done here because neither process releases the resource. This is referred to as a deadlock.
Examples of Deadlock
Deadlocks can occur in various real-world and computing scenarios where multiple processes compete for limited resources. Below are some common examples:
Traffic Gridlock: Imagine four vehicles at an intersection, each blocking the other’s path. Since every vehicle waits for the other to move, none can proceed, creating a deadlock.
Dining Philosophers Problem: In this classic synchronization problem, philosophers sit around a table with limited chopsticks. If each philosopher picks up one chopstick and waits for the second, no one can eat, leading to a deadlock.
File Locking in OS: If Process A locks File X and waits for File Y, while Process B locks File Y and waits for File X, both processes remain in a deadlock.
Database Transactions: When two transactions hold locks on different database rows and wait for each other’s locked resources, neither transaction can proceed, causing a deadlock.
Resource Allocation in Multithreading: In a multithreaded program, if Thread A holds a mutex lock required by Thread B, and vice versa, both threads remain stuck, leading to deadlock.
Necessary Conditions for deadlock
There are four necessary conditions for a deadlock.
Mutual Exclusion
Any resources you are using should be used mutually exclusively, meaning both processes cannot use the same resource simultaneously.
The short form of Mutual Exclusion is Mutex, which is a binary semaphore that is a type of communication method that manages access to shared resources. It uses inheritance to avoid the problem of extended priority inversion. By applying this method, tasks with higher priority can be blocked as long as possible.
Deadlocks never occur with shared resources like read-only files but with exclusive access to resources like tape drives and printers.
Fig.2
No Preemption
When a process completes its task, it can release a resource voluntarily. A process that holds some resources requests another that cannot be allocated to it immediately and, in that case, all resources will become available.
There is a process that is waiting for the list of preempted resources. If the process can recover its old resources and request a new one, it will be restarted.
Whenever another resource is available, it is given to the process requesting it. We release it and give it to the requesting process if it was being held by another process waiting for another resource.
Hold and wait
When this condition exists, the process must be stopped if it simultaneously holds one or more resources while waiting for another resource. In the below diagram, Process_1 currently holding Resource_1, is waiting for additional Resources_2.
Fig.3
Circular wait
In Fig 1, a Circular wait is occurring. It is a condition when the first process awaits a resource held by the second process, which waits for the resource held by the third process, etc. In the end, the last process is waiting for the resource that the first process holds. Therefore, every process is waiting for the other processes to release the resources, and no one is releasing them. But no one is releasing any resources. This is known as a circular wait.
Resource Utilization
Resources are utilized in the following ways:
Request: If the requested resources are unavailable, the process must wait until they become available.
Use: During the process, resources are used.
Release: By releasing the resource, the process relinquishes control. As a result, other processes can use it.
In this method, it is assumed that there will never be a deadlock. It is best suited to single-user systems where users merely browse the internet and do regular tasks. Most Windows and Linux users use this approach.
Deadlock prevention
WhenMutual Exclusion, hold and wait, and no preemption all have simultaneously, a deadlock occurs. There can never be a deadlock in a system if one of the four conditions can be violated at any time.
Deadlock Avoidance
During deadlock avoidance, the operating system tests each step it performs to see if the system is in a safe or unsafe state. Once a safe state is reached, the process continues. The OS must take one step back when the system is dangerous. One of the methods for deadlock avoidance is the Deadlock Avoidance Algorithm,which uses the Banker's algorithm to avoid a deadlock state.
Deadlock Detection and Recovery
Processes can get stuck in a deadlock with this approach and then periodically check if a deadlock has occurred in the system. A deadlock can be removed by applying recovery methods to the system.
Advantages and Disadvantages of using the methods to handle deadlocks
Advantages
It does not require preemption.
This is a convenient way to save and restore the state of resources whose state can be easily preserved and restored.
It can also be feasible at compile-time checks.
It does not require run-time computation as the problem is resolved during the design phase.
Disadvantages
The initiation of the process is delayed.
Future resource requirements should be known.
It frequently preempts the situation.
Don't allow incremental resource requests.
Inherent preemption losses.
Difference between Deadlock and Starvation
Deadlock
Starvation
This occurs when a process is blocked.
A process with a high priority is allowed to run while a low priority process is blocked.
It suffers from starvation.
Not every starvation suffers from a deadlock.
It is an infinite process.
It is not an infinite process.
After a deadlock, there might be the mutual exclusion and hold and wait. Preemption and circular wait do not take place simultaneously.
This is due to uncontrolled priorities and resource management.
Frequently Asked Questions
What is Deadlock and Its 4 Types?
A deadlock in OS occurs when processes get stuck, waiting for resources held by each other. The four types are Mutual Exclusion, Hold and Wait, No Preemption, and Circular Wait.
How to resolve Deadlock in OS?
Deadlocks can be resolved using deadlock prevention, avoidance (Banker’s algorithm), detection, and recovery (resource preemption, process termination) to break the circular wait and free resources.
What causes deadlock?
Two processes attempt to access a resource and cannot do so because the other prevents them from obtaining exclusive access to it. The two processes are thus unable to proceed. Deadlocks can only be broken by terminating one of the processes.
How do you avoid “hold and wait” conditions?
To avoid a "hold and wait" situation, a process should request all its required resources simultaneously and be blocked until all requests are granted simultaneously.
How can a system recover from deadlocks?
Resource Preemption: The basic principle of preemption is to remove deadlocks by sequentially transferring resources from one process to another until the deadlock cycle is broken. Three issues that need to be addressed are: 1) Selecting a victim 2) Rollback 3) Starvation Process Termination: By calling exit() on the operating system, a process ends after executing its final statement and requesting deletion from the OS. When this occurs, its parent process may receive a status value from the child process (via the wait() system call). Operating systems deallocate all of the resources -such as physical memory, virtual memory, open files, and buffers -of a process.
Conclusion
Deadlocks in operating systems can severely impact performance by causing processes to remain indefinitely blocked. Understanding the causes, types, and resolution strategies—such as prevention, avoidance, detection, and recovery—is crucial for system stability.
So its time for you now to refer to other articles based on a similar topic, i.e., deadlockinoperating system-