Deadlock
Deadlock
BCSC 0004
Deadlocks
1
Deadlock
In a multiprogramming system, a number of process compete for limited number of
resources and if a resource is not available at that instance then process enters into
waiting states.
If a process unable to change its waiting state indefinitely because the resources
required by it are held by another waiting process then system is said to be in
deadlock.
R2
P1 P2
R1
System Model
• Mutual exclusion: At least one resource type in the system which can be used in
non- shareable mode i.e mutual exclusion (one at a time/ one by one) e. g. CPU time,
printer.
• Hold and wait: A process is currently holding at least one resource and requesting
additional resources which are being held by other processes.
• Circular wait: Each process must be waiting for a resource which is being held
by another process, which in turn is waiting for the first process to release the
resources.
P1 P2 P3
Deadlock handling Methods
Prevention: Means design such a system which violate at least one of four
necessary conditions of deadlock and ensure independence from deadlock.
Avoidance: system maintains a set of data using which it takes a decision weather
to entertain a new request or not, to be in safe state.
Detection and recovery: Here we wait until deadlock occurs and once we detect it
then recover from it.
Do not hold: Process will acquire only desired resources, but before making any fresh
request it must release all the resources that it currently hold. (Efficient, implementable)
Wait timeouts: we place a maximum time upto which a process can wait. After which
process must release all the holding resources & exit.
Deadlock Prevention
• Violate No- Preemption
Forcefully preemption: we allow a process to forcefully preempt the resources holding
by other process.
The process which are in waiting state must be selected as a victim instead of process
in the running state.
Deadlock Prevention
F : N R
Allow every process to either only in the increasing or decreasing order of the
resource number.
If a process require a lesser number (in any of increasing order), then it must first
release all the resources larger than required number.
Deadlock Avoidance
The resource allocation state is defined by the number of available and allocated
resources and the maximum demands of the processes before allowing that request first.
We check, if there exist “ some sequence in which we can satisfies demand of every
process without going into deadlock, if yes the sequence is called safe sequence” and
request can be allowed. Otherwise there is a possibility of going into deadlock.
Banker’s Algorithm
The banker’s algorithm is
a resource allocation and
deadlock avoidance
algorithm that tests for
safety.
P0 0 3 0 7 2 3 2 1 0
P1 3 0 2 0 2 0
P2 3 0 2 8 0 0
P3 2 1 1 0 1 1
P4 0 0 2 4 3 1
All five processes are in waiting state as none is able to satisfy the condition
Needi < Available
Deadlock detection and recovery
Here we do not check safety and where any process request for some resources then
these resources are allocated immediately, if available.
Active approach: here we simply invoke the algorithm at defined intervals for
example once per hour or whenever CPU utilization drop below 40%.