Deadlocks
Prepared By: Mr. Sangram A. Patil
Assistant Professor PVPIT,Budhgaon
 Definition
Deadlock is a situation where a set of processes are blocked because each
process is holding a resource and waiting for another resource acquired by some
process.
 Example
 System has 2 disk drives.
 P1 and P2 each hold one disk drive and each needs another one.
 Example
 semaphores A and B, initialized to 1
P0
wait (A);
wait (B);
P1
wait(B)
wait(A)
System Model
 System consists of
1. Finite number of resources shared between number of competing processes.
Resources are partitioned into several types such as CPU cycles, files and IO
devices(printers and drives) .
Each resource type consisting of several instances.
2. process must request a resource before using it and release a resource after using it.
process can request as many as resources required to it to carry out designated
task
number of resource request cannot exceed.
Process request a resource allocation of any instance of resource satisfies the
request.
 Each process utilizes a resource as follows:
 request
 use
 release
Deadlock Characterization
 Deadlock can arise if four conditions hold simultaneously.
1. Mutual exclusion: only one process at a time can use a resource.
2. Hold and wait: a process holding at least one resource is waiting to
acquire additional resources held by other processes.
3. No preemption: resources can not be preempted a resource can be
released only voluntarily by the process holding it, after that process has
completed its task.
4. Circular wait: there exists a set {P0, P1, …, Pn} of waiting processes such
that P0 is waiting for a resource that is held by P1, P1 is waiting for a resource
that is held by P2, …, Pn–1 is waiting for a resource that is held by Pn, and Pn is
waiting for a resource that is held by P0.
Resource-Allocation Graph
 A set of vertices V and a set of edges E.
 V is partitioned into two types:
 P = {P1, P2, …, Pn}, the set consisting of all active processes in the
system.
 R = {R1, R2, …, Rm}, the set consisting of all resource types in the system.
 request edge – directed edge Pi  Rj
 assignment edge – directed edge Rj  Pi
Resource-Allocation Graph (Cont.)
Example of a Resource Allocation Graph
• Consists of sets P,R,E
P={P1,P2,P3}
R= {R1,R2,R3,R4}
E={P1->R1,P2->R3,R1->P2,R2->P2,R2->P1,
R3->P3}
• Resource instances:
• Process states
If graph contains no cycle then no process in the
system is deadlocked
If graph does contain a cycle then deadlock may
exists.
Resource Allocation Graph With A
Deadlock
 Two cycles
P1->R1->P2->R3->P3->R2->P1
P2->R3->P3->R2->P2
Graph With A Cycle But No Deadlock
 There is cycle but no deadlock
P1->R1->P3->R2->P1
Basic Facts
 If graph contains no cycles  no deadlock.
 If graph contains a cycle 
 if only one instance per resource type, then deadlock.
 if several instances per resource type, possibility of deadlock.

Deadlock

  • 1.
    Deadlocks Prepared By: Mr.Sangram A. Patil Assistant Professor PVPIT,Budhgaon
  • 2.
     Definition Deadlock isa situation where a set of processes are blocked because each process is holding a resource and waiting for another resource acquired by some process.  Example  System has 2 disk drives.  P1 and P2 each hold one disk drive and each needs another one.  Example  semaphores A and B, initialized to 1 P0 wait (A); wait (B); P1 wait(B) wait(A)
  • 3.
    System Model  Systemconsists of 1. Finite number of resources shared between number of competing processes. Resources are partitioned into several types such as CPU cycles, files and IO devices(printers and drives) . Each resource type consisting of several instances. 2. process must request a resource before using it and release a resource after using it. process can request as many as resources required to it to carry out designated task number of resource request cannot exceed. Process request a resource allocation of any instance of resource satisfies the request.
  • 4.
     Each processutilizes a resource as follows:  request  use  release
  • 5.
    Deadlock Characterization  Deadlockcan arise if four conditions hold simultaneously. 1. Mutual exclusion: only one process at a time can use a resource. 2. Hold and wait: a process holding at least one resource is waiting to acquire additional resources held by other processes. 3. No preemption: resources can not be preempted a resource can be released only voluntarily by the process holding it, after that process has completed its task. 4. Circular wait: there exists a set {P0, P1, …, Pn} of waiting processes such that P0 is waiting for a resource that is held by P1, P1 is waiting for a resource that is held by P2, …, Pn–1 is waiting for a resource that is held by Pn, and Pn is waiting for a resource that is held by P0.
  • 6.
    Resource-Allocation Graph  Aset of vertices V and a set of edges E.  V is partitioned into two types:  P = {P1, P2, …, Pn}, the set consisting of all active processes in the system.  R = {R1, R2, …, Rm}, the set consisting of all resource types in the system.  request edge – directed edge Pi  Rj  assignment edge – directed edge Rj  Pi
  • 7.
  • 8.
    Example of aResource Allocation Graph • Consists of sets P,R,E P={P1,P2,P3} R= {R1,R2,R3,R4} E={P1->R1,P2->R3,R1->P2,R2->P2,R2->P1, R3->P3} • Resource instances: • Process states If graph contains no cycle then no process in the system is deadlocked If graph does contain a cycle then deadlock may exists.
  • 9.
    Resource Allocation GraphWith A Deadlock  Two cycles P1->R1->P2->R3->P3->R2->P1 P2->R3->P3->R2->P2
  • 10.
    Graph With ACycle But No Deadlock  There is cycle but no deadlock P1->R1->P3->R2->P1
  • 11.
    Basic Facts  Ifgraph contains no cycles  no deadlock.  If graph contains a cycle   if only one instance per resource type, then deadlock.  if several instances per resource type, possibility of deadlock.