Unit 3 Process Deadlocks
Unit 3 Process Deadlocks
DEADLOCK
Process Deadlocks (6 hrs)
• 3.1. Introduction,
• Deadlock Characterization,
• Preemptable and Non-preemptable Resources,
• Resource – Allocation Graph,
• Conditions for Deadlock (Mutual Exclusion, Hold and Wait, No preemption, Circular
wait)
• 3.2.Handling Deadlocks: Ostrich Algorithm,
• Deadlock prevention,
• Deadlock Avoidance,
• Deadlock Detection (For Single and Multiple Resource Instances),
• Recovery From Deadlock (Through Preemption and Rollback)
Introduction
• System Model:
• System consists of resources
• Resource types R1, R2, . . ., Rm
CPU cycles, memory space, I/O devices
• Each resource type Ri has Wi instances.
• Each process utilizes a resource as follows:
• request
• use
• release
Introduction (contd.)
• A process in a multiprogramming system is said to be in dead lock if it is waiting
for a particular event that will never occur
• Example:
All automobiles trying to cross
Traffic Completely stopped
Not possible without backing some
Resource Deadlock
• A process request a resource before using it, and release after using it
• If the resource is not available when it is requested, the requesting process is force
to wait
• Process P1 holds resource R1 and needs resource R2 to continue; Process P2 holds
resource R2 and needs resource R1 to continue – deadlock
Deadlock Characterization
• Deadlock can arise if four conditions hold simultaneously.
Mutual exclusion: only one process at a time can use a resource
Hold and wait: a process holding at least one resource is waiting to acquire
additional resources held by other processes
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 = {P , P , …, P }, the set consisting of all the processes in the system
1 2 n
• R = {R1, R2, …, Rm}, the set consisting of all resource types in the system
•P requests instance of Rj Pi
i
Rj
•P is holding an instance of Rj Rj
i
Pi
Example of a Resource Allocation Graph
Resource Allocation Graph With A Deadlock
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
Methods for Handling Deadlocks
• Ensure that the system will never enter a deadlock state:
• Deadlock prevention
• Deadlock avoidance
• “To stick one’s head in the sand and pretend there is no problem”
• Used when it is cost-effective to allow the problem rather than its prevention
Deadlock Detection
• The system does not attempt to prevent deadlocks from occurring
• Instead, it lets them occur, tries to detect when this happens, and then takes some action to
recover after the fact
• Different approaches:
Deadlock Detection with One Resource of Each Type
Deadlock Detection with Multiple Resource of Each Type
Deadlock Detection with One
Resource of Each Type
• If resource graph consists one or more cycle then deadlock occurs
• If no cycle then no deadlock
• Here, one resource of each type exists (one scanner, one printer, etc.)
• Is this System Deadlocked? If so which process are involved?
Process A holds R and wants S
Process B holds nothing but wants T
Process C holds nothing but wants S
Process D holds U and wants S and T
Process E holds T and wants V
Process F holds W and wants S
Process G holds V and wants U
Deadlock Detection-One Resource
(contd.)
Deadlock Detection-One Resource
(contd.)
• After Extraction:
Deadlock Detection with Multiple Resources of Each Type
Po 0 0 1 2 0 0 1 2 1 5 20
P1 1 0 0 0 1 7 5 0
P2 1 3 5 4 2 3 5 6
P3 0 6 3 2 0 6 5 2
P4 0 0 1 4 0 6 5 6