Home » Operating Systems

Deadlock in Operating System

Learn: In this article, we are going to study about the deadlock in the operating system. What is a deadlock? Why deadlock occurs? What are the various methods to avoid deadlock in the operating system?
Submitted by Amit Shukla, on August 02, 2017

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.

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

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.

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



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© some rights reserved.