0% found this document useful (0 votes)
57 views

Methods For Handling Deadlocks

A deadlock is a situation where two or more competing processes are blocked waiting for resources held by each other in a cyclic manner. There are four necessary conditions for deadlock to occur: mutual exclusion, hold and wait, no preemption, and circular wait. Deadlock prevention aims to satisfy at least one of these conditions by constraining the system design, such as not allowing exclusive access to resources. Deadlock avoidance dynamically monitors the system state and avoids actions that could lead to unsafe states where deadlock may occur in the future.

Uploaded by

Sachin More
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views

Methods For Handling Deadlocks

A deadlock is a situation where two or more competing processes are blocked waiting for resources held by each other in a cyclic manner. There are four necessary conditions for deadlock to occur: mutual exclusion, hold and wait, no preemption, and circular wait. Deadlock prevention aims to satisfy at least one of these conditions by constraining the system design, such as not allowing exclusive access to resources. Deadlock avoidance dynamically monitors the system state and avoids actions that could lead to unsafe states where deadlock may occur in the future.

Uploaded by

Sachin More
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

1A deadlock is a situation where in two or more competing actions are each waiting for the other to finish, and

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 is permanent blocking of the set processes that


either compete for system resources or communicate each
other.
to avoid deadlock following
cases must be avoided.

Mutual exclusion -a resource that cannot be used by


more than one process at a time. (one resource to one process)
• Hold and Wait - processes already holding resources
may request new resources
• No preemption - Once a process holds a resource, it
cannot be taken away by another process or the kernel.
• Circular wait - Each process is waiting to obtain a
resource which is held by another process.

Methods for Handling Deadlocks


 Deadlock Prevention.
o Disallow one of the four necessary conditions for deadlock.

 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.

 Ignore the problem...


o Makes sense if the likelihood is very low.

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

Algorithm for this :


1. Maintain information on current allocation of resources to processes + outstanding resource requests
2. Check for circular wait via resource allocation graphs, petri nets etc

Invoking the algorithm :

 Each time resource is requested - may lead to very high overheads


 Periodically
 When resource utilisation drops below a limit

The Difference Between Deadlock Prevention and


Deadlock Avoidance
 Deadlock Prevention:
o Preventing deadlocks by constraining how requests for resources can be made in the system and how
they are handled (system design).
o The goal is to ensure that at least one of the necessary conditions for deadlock can never hold.

 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.

Similar to the difference between a traffic light


and a police officer directing traffic.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

‘’’’’’’’’’’’’’ Deadlock Prevention

Difference from avoidance is that here, the system itself is build in such a way that there are no deadlocks.

Make sure at least one of the 4 deadlock conditions is never satisfied.

This may however be even more conservative than deadlock avoidance strategy.

o Attacking Mutual condition


 never grant exclusive access. but this may not be possible for several resources.
o Attacking pre-emption
 not something you want to do.
o Attacking hold and wait condition
 make a process hold at the most 1 resource at a time.
 make all the requests at the beginning. All or nothing policy. If you feel, retry. eg. 2-phase locking
o Attacking circular wait
 Order all the resources.
 Make sure that the requests are issued in the correct order so that there are no cycles present in the
resource graph.

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

Avoid actions that may lead to a deadlock.

Think of it as a state machine moving from 1 state to another as each instruction is executed.

Safe State

Safe state is one where

 It is not a deadlocked state


 There is some sequence by which all requests can be satisfied.

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.
’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’

Banker's Algorithm for Deadlock Avoidance

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

Detection and Recovery process


Is there a deadlock currently?
One resource of each type (1 printer, 1 plotter, 1 terminal etc.)
 check if there is a cycle in the resource graph. for each node N in the graph do DFS (depth first search) of the
graph with N as the root In the DFS if you come back to a node already traversed, then there is a cycle. }
Multiple resources of each type:

 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,

Check R matrix, and find a row i such at Ri < A.


If such a process is found, add Ci to A and remove process i from the system.
Keep doing this till either you have removed all processes, or you cannot remove any other process.
Whatever is remaining is deadlocked.

          [ 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.

 To achieve Mutual Exclusion Algorithms

 Centralized algorithm

 Distributed algorithm

 Token ring 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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy