Methods For Handling Deadlocks
Methods For Handling Deadlocks
thus neither
ever does . 2 A deadlock is a situation in which two computer programs sharing the same resource are effectively preventing
each other from accessing the resource, resulting in both programs ceasing to function.
Deadlock Avoidance.
o Do not grant a resource request if this allocation have the potential to lead to a deadlock.
Deadlock Detection.
o Always grant resource request when possible. Periodically check for deadlocks. If a deadlock exists,
recover from it.
Deadlock Detection
Permit first three necessary conditions for deadlock
For greater efficiency, tolerate occasional deadlock
Require early detection to avoid 'doing nothing'
Take recovery action
Deadlock Avoidance:
o The system dynamically considers every request and decides whether it is safe to grant it at this point,
o The system requires additional apriori information regarding the overall potential use of each resource
for each process.
o Allows more concurrency.
Difference from avoidance is that here, the system itself is build in such a way that there are no deadlocks.
This may however be even more conservative than deadlock avoidance strategy.
Resources numbered 1 ... n. Resources can be requested only in increasing order. ie. you cannot
request a resource whose no is less than any you may be holding.
Deadlock Avoidance
Think of it as a state machine moving from 1 state to another as each instruction is executed.
Safe State
To avoid deadlocks, we try to make only those transitions that will take you from one safe state to
another. We avoid transitions to unsafe state (a state that is not deadlocked, and is not safe)
e.g.
Total of instances of resource = 12
(Max, Allocated, Still Needs)
P0 (10, 5, 5) P1 (4, 2, 2) P2 (9, 2, 7) Free = 3 - Safe
The sequence is a reducible sequence, the first state is safe.
What if P2 requests 1 more and is allocated 1 more instance?
- Results in Unsafe state
So do not allow P2's request to be satisfied.
’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’
When a request is made, check to see if after the request is satisfied, there is a (at least one!)
sequence of moves that can satisfy all the requests. that is, the new state is safe. If so, satisfy the
request, else make the request wait
m resources, n processes
Max resources in existence = [E1, E2, E3, .... Em]
Current Allocation = C1-n,1-m
Resources currently Available = [A1, A2, ... Am]
Request matrix = R1-n,1-m
Invariant = Sum(Cij) + Aj = Ej
Define A <= B for 2 vectors, A and B, if Ai <= Bi for all i
Overview of deadlock detection algorithm,
[ Basic idea, is that there is at least 1 execution which will un-deadlock the system ]
Recovery:
Through pre-emption
Rollback
keep check-pointing periodically
when a deadlock is detected, see which resource is needed.
Take away the resource from the process currently having it.
Later on, you can restart this process from a check pointed state where it may need to reacquire the
resource.
Killing processes
where possible, kill a process that can be rerun from the beginning without ill-effects
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Deadlock Prevention
Elimination of “Mutual Exclusion” Condition, Hold and Wait.......
Deadlock prevention means that what things (steps) we need to do to avoid deadlock condition.
There are four conditions which causes a deadlock. If we handle these situations or if we get solution
to resolve all these conditions (Mutual Exclusion, Hold and Wait, No Preemption, Circular Wait)
then we can prevent the occurrence of a deadlock.
* Mutual Exclusion: This condition occurs when multiple processes tries to access a non sharable
resource.
For example only one user can use a printer at a time and if more than one user will try access it then
deadlock may occur. But on the other hand if many users are accessing a read-only file at a time,
then they can access it without generating deadlock condition because read-only file is a sharable
resource.
* Hold and Wait: We can avoid this condition by using a suitable method. As if we make it possible
that a process cannot access two resources at a time then we can avoid this condition.
* No Preemption: A resource will not be immediately available to a process if that process already
hold a resource.
Similarly we can check whether a resource is available and if it is available then we can allocate it to
that process.
* Circular Wait: We can use a method to avoid this condition. In this method, we assign a number to
resources as well as processes. For example, if there are ten processes in waiting queue which want
to hold resource number 7 then the process with the integer number 1 can use that because it was the
first process that wanted to access resource number 7 and was entered in queue. Anonymous
one process holds a resource, other processes requesting that resource must wait until the process releases it.is mutual exclusionIt is one of thecharacteristics
of deadlock Programming requirement: prevent two
processes from executing code in a “critical section” at the same time.
Centralized algorithm
Distributed algorithm
Centralized algorithm:at start one of processes elected as the coordinator(hignest process no)
Distributed algorithm: Ricart and Agrawala’s algo:requires total ordering of all events in the system, knowing which event happened first