Operating System Tutorial

Operating System Practice

Deadlock in Operating System

Operating System | Deadlock: In this tutorial, we will learn about the deadlock in operating system, deadlock example, reasons for arising deadlock, and methods to avoid deadlock. By Amit Shukla Last updated : May 07, 2023

What is Deadlock in Operating System (OS)?

Deadlock is a condition that occurs when multiple processes wait for each other to free up resources and as a result, all processes remains halted. In other words, deadlocks are the set of processes which are blocked. In this, each process is holding a resource and waiting to acquire a resource held by another process.

In a multiprogramming environment, there may be several processes with a finite number of resources. A process may request another resource while still holding some of the other resources with itself. If these requested resources are not available at the time, the process may enter into a waiting state. This waiting state may never end if the resources requested by this process are held by other waiting processes. This situation is called "Deadlock" where none of the processes are ready to release their resources and are also waiting indefinitely for other waiting processes to release their resources.

Deadlock Example

Let us understand the concept of deadlock with the help of an example. Suppose there are two processes Pros 1 and Pros 2. Both processes Pros 1 and Pros 2 are running in a computer system. Process Pros 1 request for a resource, such as a printer that is being utilized by the process Pros 2. As a result, the process Pros 1 has to wait until the time Pros 2 complete its processing and free the resource. At the same time Pros 2 process request for a resource, such as shared data that has been locked by the process Pros 1. Thus, both the processes Pros 1 and Pros 2 ends up waiting for each other to free up the required resources. This special situation is known as deadlock of the operating system.

The following figure shows the occurrence of deadlock:

occurrence of deadlock

What are the reasons for deadlock in OS?

The following are the various reasons of arise of deadlock in an operating system:

1. Mutual Exclusion

In mutual exclusion, processes are not allowed to share resources with each other. In other words, if one process has control over a resource, then that resource cannot be used by another process until the first process releases the resource.

2. Hold and wait

In hold and wait, a process takes control of a resource and waits for some other resource or activity to complete.

3. No Preemption

In this condition, a process is not allowed to force some other process to release the resource.

To study more about Preemption you can refer: Process scheduling in operating system.

What are the methods to avoid deadlock conditions?

There are a number of methods through which the deadlock conditions can be avoided. Some of these methods are given as follow:

1. Ignore Deadlock

In this method, it is assumed that deadlock would never occur. There is a good chance that deadlock may not occur in a computer system. For a long period of time. As a result, the ignore deadlock method can be useful in many cases. But still, it is not a perfect solution to remove deadlock in the operating system.

2. Detect and Recover from Deadlock

In this method, the deadlock is first detected by using some algorithms of allocation/request graph. This graph is used to represent the allocations of various resources to different processes. After a deadlock has been detected, a number of methods can be used to recover from that deadlock. One way is preemption in which a resource held by one process is provided to another process. The second way is to roll back in which operating system keeps a record of the process state and makes a process roll back to its previous state. Thus, eliminating the deadlock situation. The third way is to kill one or more processes to overcome the deadlock situation.

To study more about Process state you can refer: Process management of operating system.

3. Avoid Deadlock

In this method, a process requesting a resource is allocated the resource only if there is no possibility of deadlock occurrence.

Comments and Discussions!

Load comments ↻

Copyright © 2024 www.includehelp.com. All rights reserved.