Professional Documents
Culture Documents
Operating Systems
Deadlocks
Brief Contents
Basic concept from real world
Deadlock
System Model
Deadlock Characterization
Four conditions
Methods for handling deadlocks
Deadlock Prevention
Deadlock Avoidance
Deadlock Detection and Recovery
Ignorance
The Deadlock Problem
Resource
commodity required by a process to execute
E.g: Tape Drive, Printer, Disk Drive Memory, CPU
Types of Resources
Pre-emptible Resources
Non Pre-emptible Resources
Resources
Shareable Resource
Watch a movie on YouTube
All process can view at the same time and no deadlock
Non Shareable Resource
Depend on H/W property
For Instance Printer, Scanner, etc.
Process
Pi requests instance of Rj
Pi is holding an instance of Rj
Graph with no cycles
R1 R2
P1 P2 P3
R3 R4
Graph with cycles
R1 P2
P1 P3
R2 P4
R1 R2
P1 P2 P3
R3 R4
Forcefully Preemption
We allow a process to forcefully preempt the resource
holding by other processes.
This method may be used by high priority process or
system process
Process which are in waiting state must be selected as a
victim instead of process in the running state
Prevention (contd…
Circular Wait
Circular wait can be eliminated by first giving a natural
number of every resource
f:N R
For instance R1,R2,….RN
Impose a ordering of all resource types.
Allow every process either in increasing order or decreasing order of
the resource number
If a process require a less number then it must first release the
resource larger than request
For instance
P2 request R2 and can not request for R1 because of low order,
However P2 can request >R2 resource like R3 R4
Avoidance (Deadlock Handling Methods)
Basic Facts
If a system is in a safe state no deadlocks.
If a system is in unsafe state possibility of
deadlock.
Avoidance ensure that a system will never
reach an unsafe state.
Avoidance (Contd…
Requires that the system has some additional
apriori information available.
Simplest and most useful model requires that each process
declare the maximum number of resources of each type
that it may need.
The deadlock-avoidance algorithm dynamically examines
the resource-allocation state to ensure that there can never
be a circular-wait condition.
Resource allocation state is defined by the number of
available and allocated resources, and the maximum
demands of the processes.
Avoidance (Safe state)
When a process requests an available resource, system must
decide if immediate allocation leaves the system in a safe
state.
System is in safe state if there exists a safe sequence of all
processes.
Sequence <P1, P2, …Pn> is safe, if for each Pi, the resources
that Pi can still request can be satisfied by currently available
resources + resources held by Pj with j<i.
If Pi resource needs are not available, 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...
Avoidance (I-Resource Allocation Graph (RAG))
RAG: Used when there is only one instance of each resource
System Model
1.Request
Process is assigned resource 2.Use
3.release
Request
Claim
1 2
Possible Deadlock!!
Allocate 3
5
4
Avoidance (II-Bankers Approach)
Set of resources, set of customers, banker
Rules
Each customer tells banker maximum number of resources
it needs.
Customer borrows resources from banker.
Customer returns resources to banker.
Customer eventually pays back loan.
Condition:
Request Matrix (need matrix) <= Available Matrix
(must be true to proceed any process in Banker Algorithm or to ensure
safe state)
Check
Safe (if solve all process in any sequence) /un safe sequence/state (if all
process request not satisfy)
Example of Banker’s Algorithm
5 processes
P0 - P4;
3 resource types
A(10 instances), B (5 instances), C (7 instances)
Snapshot at time T0
P3 2 1 1 0 1 1 7 4 3
P4 0 0 2 4 3 1 10 4 7
Safe Sequence (P1, P3, P2, P4, P0)
Note: There may be more than one safe sequence
Example (cont.)
P0
P1
P2
P3
Allocate 1 units to A
6
Detection and Recovery (Deadlock Methods)