Deadlock in Operating System
What is Deadlock in OS?
All the processes in a system require some resources such as central processing unit(CPU), file storage, input/output devices, etc to execute it. Once the execution is finished, the process releases the resource it was holding. However, when many processes run on a system they also compete for these resources they require for execution. This may arise a deadlock situation.
A deadlock is a situation in which more than one process is blocked because it is holding a resource and also requires some resource that is acquired by some other process. Therefore, none of the processes gets executed.
Example Of Deadlock
In the above figure, there are two processes and two resources. Process 1 holds "Resource 1" and needs "Resource 2" while Process 2 holds "Resource 2" and requires "Resource 1". This creates a situation of deadlock because none of the two processes can be executed. Since the resources are non-shareable they can only be used by one process at a time(Mutual Exclusion). Each process is holding a resource and waiting for the other process the release the resource it requires. None of the two processes releases their resources before their execution and this creates a circular wait. Therefore, all four conditions are satisfied.
Neccessary Conditions for Deadlock
The four necessary conditions for a deadlock to arise are as follows.
- Mutual Exclusion: Only one process can use a resource at any given time i.e. the resources are non-sharable.
- Hold and wait: A process is holding at least one resource at a time and is waiting to acquire other resources held by some other process.
- No preemption: The resource can be released by a process voluntarily i.e. after execution of the process.
- Circular Wait: A set of processes are waiting for each other in a circular fashion. For example, lets say there are a set of processes {,,,} such that depends on , depends on , depends on and depends on . This creates a circular relation between all these processes and they have to wait forever to be executed.
Methods of Handling Deadlocks in Operating System
The first two methods are used to ensure the system never enters a deadlock.
Deadlock Prevention
This is done by restraining the ways a request can be made. Since deadlock occurs when all the above four conditions are met, we try to prevent any one of them, thus preventing a deadlock.
Deadlock Avoidance
When a process requests a resource, the deadlock avoidance algorithm examines the resource-allocation state. If allocating that resource sends the system into an unsafe state, the request is got granted.
Therefore, it requires additional information such as how many resources of each type is required by a process. If the system enters into an unsafe state, it has to take a step back to avoid deadlock.
Deadlock Detection and Recovery
We let the system fall into a deadlock and if it happens, we detect it using a detection algorithm and try to recover.
Some ways of recovery are as follows.
- Aborting all the deadlocked processes.
- Abort one process at a time until the system recovers from the deadlock.
- Resource Preemption: Resources are taken one by one from a process and assigned to higher priority processes until the deadlock is resolved.
Deadlock Ignorance
In the method, the system assumes that deadlock never occurs. Since the problem of deadlock situation is not frequent, some systems simply ignore it. Operating systems such as UNIX and Windows follow this approach. However, if a deadlock occurs we can reboot our system and the deadlock is resolved automatically.
Note: The above approach is an example of Ostrich Algorithm. It is a strategy of ignoring potential problems on the basis that they are extremely rare.
Difference between Starvation and Deadlocks
Deadlock | Starvation |
---|---|
A deadlock is a situation in which more than one process is blocked because it is holding a resource and also requires some resource that is acquired by some other process. | Starvation is a process in which the low priority processes are postponed indefinitely because the resources are never allocated. |
Resources are blocked by a set of processes in a circular fashion. | Resources are continuously used by high-priority resources. |
It is prevented by avoiding anyone necessary condition required for a deadlock or recovered using a recovery algorithm. | It can be prevented by aging. |
In a deadlock, none of the processes get executed. | In starvation, higher priority processes execute while lower priority processes are postponed. |
Deadlock is also called circular wait. | Starvation is also called lived lock. |
Conclusion
- A deadlock in OS is a situation in which more than one process is blocked because it is holding a resource and also requires some resource that is acquired by some other process.
- The four necessary conditions for a deadlock situation are mutual exclusion, no preemption, hold and wait and circular set.
- There are four methods of handling deadlocks - deadlock avoidance, deadlock prevention, deadline detection and recovery and deadlock ignorance.
- We can prevent a deadlock by preventing any one of the four necessary conditions for a deadlock.
- There are different ways of detecting and recovering a deadlock in a system.
- A starvation is a situation in which lower priority processes are postponed indefinitely while higher priority processes are executed.
- The advantages of deadlock handling methods are that no preemption is needed and it is good for activities that require a single burst of activity.
- The disadvantages of deadlock handling methods are it delays process initiation and preemptions are frequently encountered in it.