Deadlock in Operating System is a situation that arises when a process in the computer waits for the resources to be released that are assigned to some other process. When such a situation arises, no process gets executed as the resources it requires are being occupied by another process and another process also waiting for the other process to release the resources it requires. This is the most common problem in multi-processing systems where the processes share mutually exclusive resources.
A process in the Operating system uses the resources as follows:
- The process requests the resource
- It utilizes the resource
- After that release the resource
Analogy to Deadlock in Operating Systems
Let’s assume you are in college and it’s time for your classes now and you want to take a bath but you only have balti but not a mug while your friend has a mug but the problem is that he also wants to take a bath and he does not have balti. You both want to bath but both do not have a necessary item needed to take a bath. This situation is very much similar to a Deadlock in Operating System.
Assume that Process 1 (P1) is holding Resource 1(R1) and waiting for Resource 2 (R2) which is acquired by process 2(P2), and process 2(P2) is waiting for resource 1(R1).
A deadlock can be detected with the help of a resource scheduler. It helps the OS to keep track of the resources allocated to the various processes.
Necessary Conditions that lead to a Deadlock
A deadlock occurs if the following four conditions happen simultaneously
1. Mutual Exclusion: When two or more resources cannot be shared. It is used for controlling access to shared resources. It causes tasks that have a higher priority to be in the blocked state for a short period of time. i.e only one process can use at a time.
2. Hold and wait: A process is holding one or more resources and simultaneously waiting for other resources.
3. No Preemption: A resource is not taken from the process until and unless the process releases the resources.
4. Circular wait: Here a set of processes are waiting for each other in a circular form.
How to handle a deadlock in Operating System?
There are 3 different ways in which we can handle deadlock in Operating System
- Deadlock prevention or avoidance: In such case, we does not allow the system to move into a deadlock state. In deadlock prevention, we have to make sure that at least one of the above-mentioned necessary conditions doesn’t occur for deadlock. In deadlock avoidance, we have to make sure that the resources of the process are known to us before the execution. Use bankers algorithm
- Deadlock detection and recovery: Here we allow deadlock to occur and do the preemption to handle the deadlock.
- Ignore the problem altogether: It allows the deadlock to occur and reboots the system if the deadlock is rare.
Difference Between Deadlock and Starvation
|Deadlock is Infinite waiting||Starvation is not infinite but long waiting|
|Every Deadlock is always starvation||Every starvation is not necessarily a deadlock|
|The resource that is requested is blocked by another process||The requested resource is continuously used by higher-priority resources|
|Deadlock situations arise when no process gets blocked and proceeds||Starvation arises when low priority process gets blocked and higher priority process proceeds|
|It happens when Mutual exclusion, no preemption, hold and wait and circular wait occurs simultaneously||It happens due to uncontrolled priority and resource management|
Advantages of Deadlock
- No preemption is required for deadlock.
- It is good for those processes which can perform different activities at a time.
- It does not require run-time computation as the problem gets solved via system design.
Disadvantages of Deadlock
- It preempts more often than it is required.
- It makes delays in the process initiation.
- We have to make sure that the process must know what is future resources needed.
- It doesn’t allow increasing resource requests.
- Inherent preemption causes losses.