DEADLOCK
WHAT IS DEADLOCK
• A deadlock in an Operating System (OS) occurs when a group
of processes are stuck in a state where each is waiting for a
resource that another process holds, and none can proceed.
This results in a situation where the system cannot make
further progress.
SYSTEM MODELS
Each process utilizes a resource as follows:
request
use
release
CONDITIONS FOR DEADLOCK
• Mutual Exclusion
Only one process can use a resource at any given
time i.e. the resources are non-sharable.
• Hold and Wait
A process holding a resource can request additional
resources without releasing the ones it already holds.
• No Preemption
Resources cannot be forcibly taken from a process; they must
be released voluntarily.
Circular Wait –
A closed chain of processes exists, where each process is
waiting for a resource held by the next process in the chain.
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 the processes
in the system.
• R = {R1, R2, …, Rm}, the set consisting of all resource
types in the system.
• request edge – directed edge P1 Rj
• assignment edge – directed edge Rj Pi
Operating System Concepts
Resource-Allocation Graph (Cont.)
Process
Resource Type with 4 instances
Pi requests instance of Rj
Pi
Rj
Pi is holding an instance of Rj
Pi
Rj
Operating System Concepts 8.11 Silberschatz, Galvin and Gagne 2002
Example of a Resource Allocation Graph
Operating System Concepts 8.12 Silberschatz, Galvin and Gagne 2002
Banker’s Algorithm
Multiple instances.
Each process must a priori claim maximum use.
When a process requests a resource, it may have to
wait.
When a process gets all its resources it must return
them in a finite amount of time.
Operating System Concepts 8.13 Silberschatz, Galvin and Gagne 2002
Data Structures for the Banker’s Algorithm
Let n = number of processes, and m = number of resources types.
Available: Vector of length m. If available [j] = k, there are k
instances of resource type Rj available.
Max: n x m matrix. If Max [i,j] = k, then process Pi may
request at most k instances of resource type Rj.
Allocation: n x m matrix. If Allocation[i,j] = k then Pi is
currently allocated k instances of Rj.
Need: n x m matrix. If Need[i,j] = k, then Pi may need k more
instances of Rj to complete its task.
Need [i,j] = Max[i,j] – Allocation [i,j].
Operating System Concepts 8.14 Silberschatz, Galvin and Gagne 2002
Safety Algorithm
1. Let Work and Finish be vectors of length m and n,
respectively. Initialize:
Work = Available
Finish [i] = false for i - 1,3, …, n.
2. Find and i such that both:
(a) Finish [i] = false
(b) Needi Work
If no such i exists, go to step 4.
3. Work = Work + Allocationi
Finish[i] = true
go to step 2.
4. If Finish [i] == true for all i, then the system is in a
safe state.
Operating System Concepts 8.15 Silberschatz, Galvin and Gagne 2002
Resource-Request Algorithm for Process Pi
Request = request vector for process Pi. If Requesti [j] = k then
process Pi wants k instances of resource type Rj.
1. If Requesti Needi go to step 2. Otherwise, raise error
condition, since process has exceeded its maximum claim.
2. If Requesti Available, go to step 3. Otherwise Pi must wait,
since resources are not available.
3. Pretend to allocate requested resources to Pi by modifying the
state as follows:
Available = Available - Requesti;
Allocationi = Allocationi + Requesti;
Needi = Needi – Requesti;;
• If safe the resources are allocated to Pi.
• If unsafe Pi must wait, and the old resource-allocation
Operating System Concepts state is restored 8.16 Silberschatz, Galvin and Gagne 2002
Deadlock Avoidance
Consider the following snapshot of a system
PROCESS ALLOCATION MAX AVAILABLE
A B C D A B C D ABCD
P0 2 0 1 2 2 0 1 2 2 4 2 1
P1 1 0 0 0 2 7 5 0
P2 1 3 5 4 2 3 5 6
P3 0 6 3 2 0 7 5 2
P4 0 0 1 4 0 7 5 6
i) What is the Content of need Matrix?
ii) Is the System in Safe state? If Safe write the Sequence. If the
system is Unsafe explain how deadlock might occur.
iii) If a request from process P1 arrives (1,4,2,0) can the request be granted?
If yes, write the sequence of allocation
8.17 Silberschatz, Galvin and Gagne 2002