What is Deadlock in OS? Explain with the help of a diagram.

A deadlock is a condition where each of the processes is waiting for a resource which is allotted to other processes. In this case, the process gets computed after the resource it requires is controlled by other processes, which is also looking for some other resource to be issued.

For example, suppose two freight trains are arriving toward each other on a single track, and there is the only same track, no trains can travel once they are in front of each other. The same condition takes place in the operating system if there are two processes that keep some resources and wait for resources controlled by other processes. 

Deadlock in Operating System, Deadlock in OS
Deadlock in Operating System

There are some conditions for deadlock

  • Mutual exclusion: There must be a minimum of one resource that can be utilized by only one process at the same moment in the system. When there is no such resource exist, then the process never suffer from the deadlock situation.
  • Hold and wait: There should be a process that holds resources and waiting for other resources occupied by other operations.
  • No preemption: A process that, once planned, will be computed till the termination. No other process can be scheduled at the same time by the scheduler. 
  • Circular wait: Entire process must be waiting for resources in a cyclic way. So, the last process is waiting for the resource that is being occupied by the initial process. 

Methods for handling deadlock

  • Deadlock prevention or avoidance: Prevention is done by invalidating the necessary conditions of the deadlock. Avoidance is advanced in nature. We have to ensure that entire information about resources will require are well-known to us. To avoid deadlock, we use Banker’s algorithm
  • Deadlock detection and recovery: We do preemption to manage deadlock when it happens. 
  • Ignore the problem altogether: When deadlock takes place, then allow it to occur and restart the system. This procedure is used by Windows and UNIX.