You are on page 1of 33

Deadlock

 Deadlock is a situation where a set of processes are blocked because each process is holding a
resource and waiting for another resource acquired by some other process. 

 In this situation, none of the process gets executed since the resource it needs, is held by some
other process which is also waiting for some other resource to be released.

 Deadlock is a common problem in multi-processing where several processes share a specific type
of mutually exclusive resource.

 Every process needs some resources to complete its execution. However, the resource is granted in
a sequential order.
 The process requests for some resource.

 OS grant the resource if it is available otherwise let the process waits.

 The process uses it and release on the completion.


Example
Starvation vs Deadlock
Starvation Deadlock

When all the low priority processes got


blocked, while the high priority Deadlock is a situation that occurs when
processes execute then this situation is one of the processes got blocked.
termed as Starvation.

Starvation is a long waiting but it is not


Deadlock is an infinite process.
an infinite process.

It is not necessary that every starvation


There is starvation in every deadlock.
is a deadlock.

During deadlock, preemption and


Starvation is due to uncontrolled priority
circular wait does not occur
and resource management.
simultaneously.

The requested resource is continuously The requested resource is blocked by the


be used by the higher priority processes. other process.
Necessary conditions for Deadlocks
Mutual Exclusion:
 A resource can only be shared in mutually exclusive manner. It implies, if two process cannot use
the same resource at the same time.
 In the diagram below, there is a single instance of Resource 1 and it is held by Process 1 only.

Hold and Wait:


 A process can hold multiple resources and still request more resources from other processes which
are holding them.
 In the diagram given below, Process 2 holds Resource 2 and Resource 3 and is requesting the
Resource1 which is held by Process 1.
No preemption:

 Resources cannot be taken from the process because resources can be released only voluntarily by

the process holding them.

 In the diagram below, Process 2 cannot preempt Resource 1 from Process 1. It will only be

released when Process 1 releases it voluntarily after its execution is complete.


Circular Wait:

 All the processes must be waiting for the resources in a cyclic manner so that the last process is

waiting for the resource which is being held by the first process.

 For example: Process 1 is allocated Resource2 and it is requesting Resource 1. Similarly, Process 2

is allocated Resource 1 and it is requesting Resource 2. This forms a circular wait loop.
Resource-Allocation Graph(RAG)
 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 Pi  Rj

 assignment edge – directed edge Rj  Pi


 Process:

 Resource Type with 4 instances:

 Pi requests instance of Rj

 Pi is holding an instance of Rj
Example
Resource Allocation Graph With A Deadlock
Graph With A Cycle But No Deadlock
Deadlock handling Strategies
 Deadlock prevention
 Deadlock happens only when Mutual Exclusion, hold and wait, No preemption and circular
wait holds simultaneously.
 If it is possible to violate one of the four conditions at any time then the deadlock can never
occur in the system.
 Deadlock avoidance
 In deadlock avoidance, the operating system checks whether the system is in safe state or in
unsafe state at every step which the operating system performs.
 The process continues until the system is in safe state. Once the system moves to unsafe state,
the OS has to backtrack one step.
 Deadlock detection and recovery
 This approach let the processes fall in deadlock and then periodically check whether deadlock
occur in the system or not.
 If it occurs then it applies some of the recovery methods to the system to get rid of deadlock
Deadlock prevention

Deadlocks can be prevented by preventing at least one of the four required conditions:

Mutual Exclusion:

 Mutual Exclusion from the resource point of view is the fact that a resource can never be used by
more than one process simultaneously

 If a resource could have been used by more than one process at the same time then the process would
have never been waiting for any resource.

 if we can be able to violate resources behaving in the mutually exclusive manner then the
deadlock can be prevented.

 i.e. Shared resources such as read-only files do not lead to deadlocks.

 Unfortunately some resources, such as printers and tape drives, require exclusive access by a single
process.
Hold and Wait:

 Hold and wait condition lies when a process holds a resource and waiting for some other resource to complete its task.

 we have to find out some mechanism by which a process either doesn't hold any resource or doesn't wait.

 That means, a process must be assigned all the necessary resources before the execution starts.

 A process must not wait for any resource once the execution has been started.

 This can be wasteful of system resources if a process needs one resource early in its execution and doesn't need some
other resource until much later.

 Require that processes holding resources must release them before requesting new resources, and then re-acquire the
released resources along with the new ones in a single new request.

 This can be a problem if a process has partially completed an operation using a resource and then fails to get it re-
allocated after releasing it.

 Either of the methods described above can lead to starvation if a process requires one or more popular resources.
No Preemption:

 Deadlock arises due to the fact that a process can't be stopped once it starts.

 However, if we take the resource away from the process which is causing deadlock then we
can prevent deadlock.

 To ensure that this condition does not hold, we can use the following protocol.

 If a process is holding some resources and requests another resource that cannot be immediately
allocated to it, then all resources the process is currently holding are preempted.

 This is not a good approach at all since if we take a resource away which is being used by the
process then all the work which it has done till now can become inconsistent.
Circular Wait:

 One way to avoid circular wait is to number all resources, and to require that processes request

resources only in strictly increasing ( or decreasing ) order.

 In other words, in order to request resource Rj, a process must first release all Ri such that i >= j.

 One big challenge in this scheme is determining the relative ordering of the different resources

 Among all the methods, violating Circular wait is the only approach that can be implemented

practically.
Deadlock Avoidance

 The deadlock-avoidance algorithm helps you to dynamically assess the resource-allocation state so
that there can never be a circular-wait situation.

 When a process makes a request for a set of resources, –

 assume that the request is granted, – update the system state accordingly,

 Then determine if the result is a safe state. –

 If so, grant the request and, –

 if not, block the process until it is safe to grant the request.

 Resource-allocation state is defined by the number of available and allocated resources, and the
maximum demands of the processes
Banker's Algorithm FOR Deadlock Avoidance

Advantages
Following are the essential characteristics of the Banker's algorithm:
• It contains various resources that meet the requirements of each process.
• Each process should provide information to the operating system for
upcoming resource requests, the number of resources, and how long the
resources will be held.
• It helps the operating system manage and control process requests for each
type of resource in the computer system.
• The algorithm has a Max resource attribute that represents each process can
hold the maximum number of resources in a system.
Banker's Algorithm FOR Deadlock Avoidance

Disadvantages
• It requires a fixed number of processes, and no additional processes
can be started in the system while executing the process.
• The algorithm does no longer allows the processes to exchange its
maximum needs while processing its tasks.
• Each process has to know and state their maximum resource
requirement in advance for the system.
Banker's Algorithm for Deadlock Avoidance

When working with a banker's algorithm, it requests to know about


three things:
1. How much each process can request for each resource in the
system. It is denoted by the [MAX] request.
2. How much each process is currently holding each resource in a
system. It is denoted by the [ALLOCATED] resource.
3. It represents the number of each resource currently available in the
system. It is denoted by the [AVAILABLE] resource.
Following are the important data structures terms applied in the banker's algorithm as follows:
Suppose n is the number of processes, and m is the number of each type of resource used in a
computer system.

• Available: It is an array of length 'm' that defines each type of resource available in the system.
When Available[j] = K, means that 'K' instances of Resources type R[j] are available in the system.
• Max: It is a [n x m] matrix that indicates each process P[i] can store the maximum number of
resources R[j] (each type) in a system.
• Allocation: It is a matrix of m x n orders that indicates the type of resources currently allocated to
each process in the system. When Allocation [i, j] = K, it means that process P[i] is currently
allocated K instances of Resources type R[j] in the system.
• Need: It is an M x N matrix sequence representing the number of remaining resources for each
process. When the Need[i] [j] = k, then process P[i] may require K more instances of resources
type Rj to complete the assigned work.
Nedd[i][j] = Max[i][j] - Allocation[i][j].
• Finish: It is the vector of the order m. It includes a Boolean value (true/false) indicating whether
the process has been allocated to the requested resources, and all resources have been released
after finishing its task.
The Banker's Algorithm is the combination of the safety algorithm and the resource request
algorithm to control the processes and avoid deadlock in a system:
Safety Algorithm
It is a safety algorithm used to check whether or not a system is in a safe state or follows the safe
sequence in a banker's algorithm:
1. There are two vectors Work and Finish of length m and n in a safety algorithm.
Initialize: Work = Available
Finish[i] = false; for I = 0, 1, 2, 3, 4… n - 1.
2. Check the availability status for each type of resources [i], such as:
Need[i] <= Work
Finish[i] == false
If the i does not exist, go to step 4.
3. Work = Work +Allocation(i) // to get new resource allocation
Finish[i] = true
Go to step 2 to check the status of resource availability for the next process.
4. If Finish[i] == true; it means that the system is safe for all processes.
Resource Request Algorithm
A resource request algorithm checks how a system will behave when a process
makes each type of resource request in a system as a request matrix.

Let create a resource request array R[i] for each process P[i]. If the Resource
Requesti [j] equal to 'K', which means the process P[i] requires 'k' instances of
Resources type R[j] in the system.

1. When the number of requested resources of each type is less than


the Need resources, go to step 2 and if the condition fails, which means that the
process P[i] exceeds its maximum claim for the resource.
If Request(i) <= Need
Go to step 2;
2. And when the number of requested resources of each type is less than the
available resource for each process, go to step (3). As the expression suggests:
If Request(i) <= Available
Go to Step 3
Else
Process P[i] must wait for the resource since it is not available for use.
3. When the requested resource is allocated to the process by changing state:
Available = Available - Request
Allocation(i) = Allocation(i) + Request (i)
Needi = Needi - Requesti

Apply the Banker’s Safty Algorithm, When the resource allocation state is safe, its
resources are allocated to the process P(i). And if the new state is unsafe, the
Process P (i) has to wait for each type of Request R(i) and restore the old
resource-allocation state.
Deadlock Detection

 Deadlock Detection is an important task of OS.

 As the OS doesn’t take many precautionary means to avoid it.

 The OS periodically checks if there is any existing deadlock in the system and take measures to
remove the deadlocks.

 There are 2 different cases in case of Deadlock detection –

 If resource has single Instance

 We make a Wait- For graph.

 If resources have multiple instances

 We use banker’s algorithm.


Resources has Single Instance
 A wait-for graph is made.

 A Wait-for graph vertex denotes process.

 The edge implies process waiting for Process 2 to release resource.

 A deadlock is detected if one wait-for graph contains a cycle.

 Wait-for graph is made by looking at resource allocation graph.

 An edge between P1 to P2 exists if P1 needs some resource which P2 has.

 In the above wait-for graph P1 is waiting for resource currently in use by P2 and P2 is waiting for
resource currently in use by P3 and so on… P5 is waiting for resource currently in use by P2.

 Which creates a cycle thus deadlock is confirmed.


multiple instances of resources 
 Wait for graph is not applicable.

 In this, a new algorithm is used.

 It too uses 3 data structures –

 Available

 Vector of length m

 Indicates number of available resources of each type.

 Allocation

 Matrix of size n*m

 A[i,j] indicates the number of j th resource type allocated to i th process.

 Request

 Matrix of size n*m

 Indicates request of each process.


Recovery From Deadlock

There are three basic approaches to recovery from deadlock:

 Inform the system operator, and allow him/her to take manual intervention.

 Terminate one or more processes involved in the deadlock

 Preempt resources.
Process Termination:

 Abort all deadlocked processes:

 This method clearly will break the deadlock cycle.

 the deadlocked processes may have computed for a long time, and the results of these partial
computations must be discarded

 Abort one process at a time until the deadlock cycle is eliminated:

 after each process is aborted, a deadlock-detection algorithm must be invoked to determine


whether any processes are still deadlocked.
 Aborting a process may not be easy. If the process was in the midst of updating a file,
terminating it will leave that file in an incorrect state
Many factors may affect which process is chosen:

 What the priority of the process is

 How long the process has computed and how much longer the process will compute before

completing its designated task

 How many and what types of resources the process has used (for example, whether the resources

are simple to preempt)

 How many more resources the process needs in order to complete

 How many processes will need to be terminated

You might also like