GM-3 2BCS303
GM-3 2BCS303
GM-3 2BCS303
Chapter 7: Deadlocks
• Deadlock Prevention
• Deadlock Avoidance
• Deadlock Detection
• Recovery from Deadlock
Deadlock
● In a multiprogrammed environment, several processes
compete for a finite number of resources.
● A process requests resources and if the resources are not
available the process enters into waiting state.
● The waiting process is never again able to change state
because the resource it has requested are held by other waiting
processes. This situation is called a deadlock
The Deadlock Problem
● A set of blocked processes each holding a resource and
waiting to acquire a resource held by another process in the
set.
● Example
○ System has 2 semaphores A and B
○ P1 and P2 each hold one semaphore and each needs
another one.
● Example
○ semaphores A and B, initialized to 1
P0 P1
wait (A); wait(B)
wait (B); wait(A)
Bridge Crossing Example
• Process
Pi
• Pi requests instance of Rj Rj
Pi
• Pi is holding an instance of Rj Rj
Example of a Resource Allocation Graph
Resource Allocation Graph With A Deadlock
Graph With A Cycle But No Deadlock
Basic Facts
• If graph contains no cycles no deadlock
• System is in safe state if there exists a sequence <P1, P2, …, Pn> such
that for each Pi, the resources that Pi request can be satisfied by
currently available resources + resources held by all the Pj
• That is:
• If Pi resource needs are not immediately available, then Pi can
wait until all Pj have finished
• When Pj is finished, Pi can obtain needed resources, execute,
return allocated resources, and terminate
• When Pi terminates, Pi +1 can obtain its needed resources, and so
on
Basic Facts
• If a system is in safe state no deadlocks
• Multiple instances
4. If Finish [i] == true for all i, then the system is in a safe state
Resource-Request Algorithm for Process Pi
Need
ABC
P0 743
P1 122
P2 600
P3 011
P4 431
• The system is in a safe state since the sequence < P1, P3, P4, P2, P0>
satisfies safety criteria
Example: P1 Request (1,0,2)
• Executing safety algorithm shows that sequence < P1, P3, P4, P0, P2> satisfies
safety requirement
• Detection algorithm
• Recovery scheme
Single Instance of Each Resource Type
• Maintain wait-for graph
• Nodes are processes
• Pi Pj if Pi is waiting for Pj
Resource-Allocation Graph
Corresponding wait-for graph
Several Instances of a Resource Type
• Sequence <P0, P2, P3, P1, P4> will result in Finish[i] = true for all i
Example (Cont.)
• P2 requests an additional instance of type C
Request
ABC
P0 000
P1 202
P2 001
P3 100
P4 002
• State of system?
• Can reclaim resources held by process P0, but insufficient resources to fulfill other
processes; requests
• Deadlock exists, consisting of processes P1, P2, P3, and P4
Recovery from Deadlock:
Process Termination