Thursday 22 October 2020

Deadlock in OS | Conditions for Deadlock

 


Deadlock in OS-

Two or more processes are said to be in deadlock if they are waiting for the happing of an event, which would never happen.

The execution of two or more processes is blocked because each process holds some resource and waits for another resource held by some other process.

Example-



Here


  • Process P1 holds resource R1 and waits for resource R2 which is held by process P2.
  • Process P2 holds resource R2 and waits for resource R1 which is held by process P1.
  • None of the two processes can complete and release their resource.
  • Thus, both the processes keep waiting infinitely.

Conditions For Deadlock-

There are following 4 necessary conditions for the occurrence of deadlock-

  • Mutual Exclusion
  • Hold and Wait
  • No - Preemption
  • Circular Wait

1. Mutual Exclusion-


  • There must exist at least one resource in the system which can be used by only one process at a time.
  • If there exists no such resource, then deadlock will never occur.
  • Printer is an example of a resource that can be used by only one process at a time


2. Hold and Wait-
 

There must exist a process which holds some resource and waits for another resource held by some other process.

3. No Preemption-
 

  • Once the resource has been allocated to the process, it can not be preempted.
  • It means resource can not be snatched forcefully from one process and given to the other process.
  • The process must release the resource voluntarily by itself.


4. Circular Wait-
 
All the processes must wait for the resource in a cyclic manner where the last process waits for the resource held by the first process.





Here,

  • Process P1 waits for a resource held by process P2.
  • Process P2 waits for a resource held by process P3.
  • Process P3 waits for a resource held by process P4.
  • Process P4 waits for a resource held by process P1.
 

Important Note-

 
  • All these 4 conditions must hold simultaneously for the occurrence of deadlock.
  • If any of these conditions fail, then the system can be ensured deadlock free.

No comments:

Post a Comment