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

OS Module3.1 Deadlocks

This document discusses deadlocks in operating systems. It begins by defining deadlocks and describing the conditions required for deadlocks to occur. It then presents several methods for handling deadlocks, including prevention, avoidance, detection, and recovery. For deadlock avoidance and detection, it discusses using resource allocation graphs to model resource usage and identify potential or existing deadlocks. It provides examples of single and multiple instance resource allocation graphs. Overall, the document provides an overview of the deadlock problem and different approaches used in operating systems to prevent or resolve deadlock situations.

Uploaded by

David
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
192 views

OS Module3.1 Deadlocks

This document discusses deadlocks in operating systems. It begins by defining deadlocks and describing the conditions required for deadlocks to occur. It then presents several methods for handling deadlocks, including prevention, avoidance, detection, and recovery. For deadlock avoidance and detection, it discusses using resource allocation graphs to model resource usage and identify potential or existing deadlocks. It provides examples of single and multiple instance resource allocation graphs. Overall, the document provides an overview of the deadlock problem and different approaches used in operating systems to prevent or resolve deadlock situations.

Uploaded by

David
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 80

Chapter 7: Deadlocks

Chapter 7: Deadlocks
The Deadlock Problem
System Model
Deadlock Characterization
Methods for Handling Deadlocks
Deadlock Prevention
Deadlock Avoidance
Deadlock Detection
Recovery from Deadlock
To develop a description of deadlocks, which prevent
sets of concurrent processes from completing their
tasks.

To present a number of different methods for preventing


or avoiding deadlocks in a computer system.
A process in operating system uses resources in the following way.
1) Requests a resource
2) Use the resource
3) Releases the resource

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.

Consider an example when two trains are coming toward each other on the same
track and there is only one track, none of the trains can move once they are in front
of each other.

A similar situation occurs in operating systems when there are two or more
processes that hold some resources and wait for resources held by other(s).
 Traffic only in one direction.
 Each section of a bridge can be viewed as a resource.
 If a deadlock occurs, it can be resolved if one car backs up (preempt
resources and rollback).
 Several cars may have to be backed up if a deadlock occurs.
 Starvation is possible.
 Note – Most OSes do not prevent or deal with deadlocks.
For example, in the below diagram, Process 1 is holding Resource 1 and waiting
for resource 2 which is acquired by process 2, and process 2 is waiting for
resource 1.
Let us assume that there are three processes P1, P2 and P3. There are three
different resources R1, R2 and R3. R1 is assigned to P1, R2 is assigned to P2 and
R3 is assigned to P3.
After some time, P1 demands for R2 which is being used by P2. P1 halts its
execution since it can't complete without R2. P2 also demands for R3 which is being
used by P3. P2 also stops its execution because it can't continue without R3. P3
also demands for R1 which is being used by P1 therefore P3 also stops its
execution.
In this scenario, a cycle is being formed among the three processes. None of the
process is progressing and they are all waiting. The computer becomes
unresponsive since all the processes got blocked.
Deadlock happens only when Mutual Exclusion, hold and wait, No
preemption and circular wait holds simultaneously.

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

2.Hold and Wait


A process waits for some resources while holding another resource at the same
time.

3.No preemption
The process which once scheduled will be executed till the completion. No other
process can be scheduled by the scheduler meanwhile.

4.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.
Strategies for handling Deadlock

1. Deadlock Ignorance

• Deadlock Ignorance is the most widely used approach among all the
mechanism.

• This is being used by many operating systems mainly for end user uses.

• In this approach, the Operating system assumes that deadlock never occurs.

• It simply ignores deadlock.

• This approach is best suitable for a single end user system where User uses
the system only for browsing and all other normal stuff.

• Windows and Linux are mainly using this approach.


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

But this prevention does not yield good result because :

long waiting time required.

In efficient use of allocated resource.

A process may not know all the required resources in advance.


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

It allows more concurrency than avoidance detection.

A decision is made dynamically whether the current resource allocation request


will, if granted, potentially lead to deadlock.

It requires the knowledge of future process requests. Two techniques to avoid


deadlock :

Process initiation denial.

Resource allocation denial.


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

Resource allocation graph is used to detect the deadlock.

The detection is either single instanced or multiple instanced.

Detection of the cycle helps to determine whether deadlock happened or not in a


single instance.

The resource allocation graph is converted into an allocation and request matrix to
apply a safety algorithm to the system.

Resources or processes are considered for recovery.


A deadlock is resolved by aborting and restarting a process, relinquishing all
the resources that the process held.

This technique does not limit resources access or restrict process action.

Requested resources are granted to processes whenever possible.

It never delays the process initiation and facilitates online handling.

The disadvantage is the inherent pre-emption losses.


The resource allocation graph is the pictorial representation of the state of a
system.
The resource allocation graph is the complete information about all the processes
which are holding some resources or waiting for some resources.

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 P1 → Rj

 assignment edge – directed edge Rj → Pi


 Process

 Resource Type with 4 instances

 Pi requests instance of Rj
Pi
Rj
 Pi is holding an instance of Rj

Pi
Rj
 If graph contains no cycles  no deadlock

 If graph contains a cycle 

◦ if only one instance per resource type, then deadlock

◦ if several instances per resource type, possibility of deadlock


 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 sequence <P1, P2, …, Pn> of ALL the
processes is the systems such that for each Pi, the resources that Pi can still
request can be satisfied by currently available resources + resources held by all
the Pj, with j < i
 That is:
◦ If Pi resource needs are not immediately available, then 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, and so on .


 If a system is in safe state  no deadlocks

 If a system is in unsafe state  possibility of deadlock

 Avoidance  ensure that a system will never enter an unsafe state.


 Single instance of a resource type
◦ Use a resource-allocation graph

 Multiple instances of a resource type


◦ Use the banker’s algorithm
Single Instance RAG Example
Suppose there are Four Processes P1, P2, P3, P4, and two resources R1 and R2,
where P1 is holding R1 and P2 is holding R2, P3 is waiting for R1 and R2 while P4
is waiting for resource R1.
Single Instance RAG Example
Multiple Instance RAG Example
Suppose there are four processes P1, P2, P3, P4 and there are two instances of
resource R1 and two instances of resource R2:

One instance of R2 is assigned to process P1 and another instance of R2 is


assigned to process P4, Process P1 is waiting for resource R1.

One instance of R1 is assigned to Process P2 while another instance of R1 is


assigned to process P3, Process P3 is waiting for resource R2.
Multiple Instance RAG Example
Multiple Instance RAG Example

From the above example, it is not possible to say the RAG is in a safe state or in
an unsafe state. So to see the state of this RAG, let’s construct the allocation
matrix and request matrix.
Multiple Instance RAG Example

•The total number of processes are three; P1, P2 & P3 and the total number of
resources are two; R1 & R2.

Allocation matrix –
•For constructing the allocation matrix, just go to the resources and see to which
process it is allocated.

•R1 is allocated to P1, therefore write 1 in allocation matrix and similarly, R2 is


allocated to P2 as well as P3 and for the remaining element just write 0.

Request matrix –
•In order to find out the request matrix, you have to go to the process and see the
outgoing edges.

•P1 is requesting resource R2, so write 1 in the matrix and similarly, P2


requesting R1 and for the remaining element write 0.

So now available resource is = (0, 0).


Multiple Instance RAG Example

Checking deadlock (safe or not) –

So, there is no deadlock in this RAG. Even though there is a cycle, still there is no
deadlock. Therefore in multi-instance resource cycle is not sufficient condition for
deadlock.
Multiple Instance RAG Example
Multiple Instance RAG Example
Above example is the same as the previous example except that, the process P3 requesting
for resource R1.
So the table becomes as shown in below.
Multiple Instance RAG Example

So, the Available resource is = (0, 0), but requirement are (0, 1), (1, 0) and (1,
0).So you can’t fulfill any one requirement. Therefore, it is in deadlock.

Therefore, every cycle in a multi-instance resource type graph is not a deadlock,


if there has to be a deadlock, there has to be a cycle.

So, in case of RAG with multi-instance resource type, the cycle is a necessary
condition for deadlock, but not sufficient.
 Claim edge Pi → Rj indicated that process Pj may request
resource Rj; represented by a dashed line

 Claim edge converts to request edge when a process


requests a resource.

 Request edge converted to an assignment edge when


the resource is allocated to the process.

 When a resource is released by a process, assignment


edge reconverts to a claim edge

 Resources must be claimed a priori in the system.


 Suppose that process Pi requests a resource Rj

 The request can be granted only if converting the request edge to an


assignment edge does not result in the formation of a cycle in the resource
allocation graph.
• Banker algorithm used to avoid deadlock and allocate resources safely to
each process in the computer system.

• The 'S-State' examines all possible tests or activities before deciding whether the
allocation should be allowed to each process.

• It also helps the operating system to successfully share the resources between all
the processes.

• The banker's algorithm is named because it checks whether a person should be


sanctioned a loan amount or not to help the bank system safely simulate allocation
resources.
• Suppose the number of account holders in a particular bank is 'n', and the total
money in a bank is 'T’.

• If an account holder applies for a loan; first, the bank subtracts the loan amount
from full cash and then estimates the cash difference is greater than T to approve
the loan amount.

• These steps are taken because if another person applies for a loan or withdraws
some amount from the bank, it helps the bank manage and operate all things
without any restriction in the functionality of the banking system.
• Similarly, it works in an operating system.

• When a new process is created in a computer system, the process must provide
all types of information to the operating system like upcoming processes, requests
for their resources, counting them, and delays.

• Based on these criteria, the operating system decides which process sequence
should be executed or waited so that no deadlock occurs in a system.

• Therefore, it is also known as deadlock avoidance algorithm or deadlock


detection in the operating system.
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 indicates each
process can hold the maximum number of resources in a system.
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.

• The number of resource requests can be granted in a finite time, but the time
limit for allocating the resources is one year.
When working with a banker's algorithm, it requests to
know about three things:

How much each process can request for each resource in the system. It is denoted
by the [MAX] request.

How much each process is currently holding each resource in a system. It is


denoted by the [ALLOCATED] resource.

It represents the number of each resource currently available in the system. It is


denoted by the [AVAILABLE] resource.
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] n the
system.

Need: It is a 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.

Need[i][j] = Max[i][j] - Allocation[i][j].


Example of Banker’s algorithm
Assume that we have the following resources:

5 Pen drives
2 Printers
4 Scanners
3 Hard disks
Here, we have created a vector representing total resources:
Available = (5, 2, 4, 3).

Assume there are four processes. The available resources are already allocated as
per the matrix table below.

Here, the allocated resources is the total of these columns:

Allocated = (4, 2, 2, 3).


We also create a Matrix to display the number of each resource required for all
the processes. This matrix is called Need=(3,0,2,2)

Process Pen Hard


Printer Scanner
Name Drives disk

P 1 1 0 0
Q 0 1 1 2
R 2 1 0 0
S 0 0 1 0

The available vector will be :

Available=Available- Allocated

= (5, 2, 4, 3) -(4, 2, 2, 3)

=(1, 0, 2, 0)
Banker’s algorithm comprises of two algorithms:
1.Safety algorithm
2.Resource request algorithm

1. Safety algorithm
The safety algorithm is used to check the system state means whether the
system is in a safe state or not.

2. Resource Request Algorithm


Let Requesti is the request array for the process ‘Pi’. Requesti [j] = k means
‘Pi’ process needs k instances of Rj resource type. At the time when a
process Pi demands resources,
Safety Algorithm
1. Let Work and Finish be vectors of length m and n, respectively.
Initialize:
Work = Available
Finish [i] = false for i = 0, 1, …, n- 1

2. Check the availability status for each type of resources [i], such as:
(a) Finish [i] == false
(b) Needi  Work

If no such i exists, go to step 4

3. Work = Work + Allocationi,


Finish[i] = true

go to step 2

4. If Finish [i] == true for all i, then the system is in a safe state
Request = request vector for process Pi. If Requesti [j] = k then
process Pi wants k instances of resource type Rj

1. If Requesti  Needi go to step 2. Otherwise, raise error condition,


since process has exceeded its maximum claim.

2. If Requesti  Available, go to step 3. Otherwise Pi must wait,


since resources are not available.

3. Pretend to allocate requested resources to Pi by modifying the state


as follows:
Available = Available – Request;
Allocationi = Allocationi + Requesti;
Needi = Needi – Requesti;

If safe  the resources are allocated to Pi.

If unsafe  Pi must wait, and the old resource-allocation state


is restored.
Considering a system with five processes P0 through P4 and three resources
of type A, B, C. Resource type A has 10 instances, B has 5 instances and
type C has 7 instances. Suppose at time t0 following snapshot of the system
has been taken:
Question1. What will be the content of the Need matrix?

Need [i, j] = Max [i, j] – Allocation [i, j]

So, the content of Need Matrix is:

Question2. Is the system in a safe state? If Yes, then what is the safe
sequence?
Applying the Safety algorithm on the given system,
Question3. What will happen if process P1 requests one additional instance of resource
type A and two instances of resource type C?
We must determine whether this new system state is safe. To do so, we again
execute Safety algorithm on the above data structures.
Hence the new system state is safe, so we can immediately
grant the request for process P1 .
Example of Banker’s Algorithm
Consider the following snapshot of a system:

Available
Allocation Max
Processes A B C
A B C A B C

P0 1 1 2 4 3 3 2 1 0

P1 2 1 2 3 2 2

P2 4 0 1 9 0 2

P3 0 2 0 7 5 3

P4 1 1 2 1 1 2

Calculate the content of the need matrix?


Is the system in a safe state?
Determine the total amount of resources of each type?
Solution:

1. Content of the need matrix can be calculated by using the below formula

Need = Max – Allocation


Solution:
2. Now, we check for a safe state

Safe sequence:
1. For process P0, Need = (3, 2, 1) and
Available = (2, 1, 0)
Need ? Available = False
So, the system will move for the next process.

2. For Process P1, Need = (1, 1, 0)


Available = (2, 1, 0)
Need ? Available = True
Request of P1 is granted.
? Available = Available +Allocation
= (2, 1, 0) + (2, 1, 2)
= (4, 2, 2) (New Available)

3. For Process P2, Need = (5, 0, 1)


Available = (4, 2, 2)
Need ? Available = False

So, the system will move to the next process.


Solution:
4. For Process P3, Need = (7, 3, 3)
Available = (4, 2, 2)
Need ? Available = False
So, the system will move to the next process.

5. For Process P4, Need = (0, 0, 0)


Available = (4, 2, 2)
Need ? Available = True
Request of P4 is granted.
? Available = Available + Allocation
= (4, 2, 2) + (1, 1, 2)
= (5, 3, 4) now, (New Available)

6. Now again check for Process P2, Need = (5, 0, 1)


Available = (5, 3, 4)
Need ? Available = True
Request of P2 is granted.
? Available = Available + Allocation
= (5, 3, 4) + (4, 0, 1)
= (9, 3, 5) now, (New Available)
Solution:
7. Now again check for Process P3, Need = (7, 3, 3)
Available = (9, 3, 5)
Need ? Available = True
Request of P3 is granted.
? Available = Available +Allocation
= (9, 3, 5) + (0, 2, 0) = (9, 5, 5)

8. Now again check for Process P0, = Need (3, 2, 1)


= Available (9, 5, 5)
Need ? Available = True
So, the request will be granted to P0.

Safe sequence: < P1, P4, P2, P3, P0>

The system allocates all the needed resources to each process. So, we can say
that system is in a safe state.

3. The total amount of resources = sum of columns of allocation + Available


= [8 5 7] + [2 1 0] = [10 6 7]
Consider the table below. We are interested in finding whether every process
can be allocated the instances of resources it is seeking and also
determining the safe sequence.

We are interested in finding two things:


1.The need matrix.

2.Whether the system is in a safe state or not? If yes, then we need to


find the safe sequence.
1.We start with calculating the total number of resources in the system. It
can be represented as:

For resource A: One resource is allocated to each P1 and P2. One resource is
available. Hence the total number of instances of A in the system is 3.

Likewise, we determine the same for resources B, C, and D.


2.We now determine the need matrix for each process. It tells us the amount
of the remaining instances of the resource that is required by a process for
its execution
3. We consider the need matrix of every process and check whether any of
them can be allocated the required resources from the available resources.

From the above table, we can observe that P0 requires 0 instances of A, 0


instances of B, 0 instances of C, and 0 instances of D. So we can say that the
requirement of P0 is fulfilled. All the resources held by P0, will now be added to the
available vector.

Safe sequence till now: P0

Available vector: Available= Available + Allocation


=(1,5,2,0) + (0,0,1,2)
From the remaining processes i.e., P1, P2, P3, and P4, P2, and P3 can be
satisfied, because for the other processes, one or more resources are unavailable.

We can pick either of the processes.

Let P2 be provided the resources. P2 requires 1 instance of A, and 2 instances of


D only which is available. After P2 is allocated the resources, it will release all the
resources it was holding since process P2 is complete now.

Safe sequence: P0, P2

available resources will now be increased since the resources held by two are now
free.

Available vector:
From the remaining processes, i.e. (P1, P3, and P4) , we can observe that the
available resources are more than the needed resources.

Hence any of the processes can now be allocated the resources. Let P3 be
allocated the resources.

P3 requires 2 instances of C, hence it will take it and, after completion, release


the resources it was holding.

All the resources released will add up to the available resources vector.

Safe sequence: P0, P2, P3

available vector:
Only P1 and P4 are left now.

Available resources exceed the required limit of P1 and P4.

Let P4 be allocated the resources. P4 requires only 1 instance of C and 4


instances of D.

After allocating resources, the safe sequence and available vector will be as
follow:

Safe sequence: P0, P2, P3, P4

Available vector:
At last we can comfortably allocate the resources to P1.

Safe sequence: P0, P2, P3, P4, P1

Available vector:

We can observe here that all five processes have been allocated the
resources without any disruption. Hence the system is said to be in a safe
state.
Resource Request Algorithm
Before we move to the algorithm part, let us discuss the importance of the resource
request algorithm. Consider the table below:
Suppose the process P1 makes a new request from the operating system for the
resources such that it requires 1 instance of resource A, 1 instance of resource B.

It does not require any other extra resource. These resources are the extra
resources sought by P1 and should not be considered as a completely new
request.

Should the operating system allocate the resources to P1?

Actually, the operating system does not allocate resources to the P1 even if it is
available at first. There may be chance that deadlock may occur in a later phase.

The operating system will then try to first detect whether allocating the resources
will lead to the system in any unsafe state or not without actually allocating
resources to P1.
Request can only be granted ( resource can only be allocated to a process) if the
request for the instances are less than the need vector.

Remember need[][] describes the maximum instance of resources that a process


would require.

Also we can only allocate the resources to a process if the instances requested is
less than or equal to the the available resources.

If a request for instances more than the available instances is made, it would not be
possible to allocate the resources.

In simple term, the resource request algorithm as the name suggests determine
whether a request for instances of resources can be granted or not.
Deadlock Detection
• The OS doesn't apply any mechanism to avoid or prevent the deadlocks.

• Therefore the system considers that the deadlock will definitely occur.

• In order to get rid of deadlocks, The OS periodically checks the system for
any deadlock.

• In case, it finds any of the deadlock then the OS will recover the system using
some recovery techniques.

• The main task of the OS is detecting the deadlocks.

• The OS can detect the deadlocks with the help of Resource allocation
graph.
Deadlock Detection
Recovery From Deadlock

When a detection algorithm determines that a deadlock exists then there are
several available alternatives.

There one possibility and that is to inform the operator about the deadlock and let
him deal with this problem manually.

Another possibility is to let the system recover from the deadlock automatically.
These are two options that are mainly used to break the deadlock.

In order to recover the system from deadlocks, either OS considers


resources or processes.
Recovery From Deadlock
Resource Preemption
In order to eliminate the deadlock by using resource preemption,

we will successively preempt some resources from processes and will give these
resources to some other processes until the deadlock cycle is broken and there is a
possibility that the system will recover from deadlock.

But there are chances that the system goes into starvation.

The operating system can rollback the system to the previous safe state. For this
purpose, OS needs to implement check pointing at every state.

The moment, we get into deadlock, we will rollback all the allocations to get into the
previous safe state.
Process Termination
In order to eliminate deadlock by aborting the process, we will use one of two methods
given below. In both methods, the system reclaims all resources that are allocated to
the terminated processes.

•Aborting all deadlocked Processes Clearly, this method is helpful in breaking the
cycle of deadlock, but this is an expensive approach.

This approach is not suggestable but can be used if the problem becomes very
serious.

If all the processes are killed then there may occur insufficiency in the system and all
processes will execute again from starting.

•Abort one process at a time until the elimination of the deadlock cycle This
method can be used but we have to decide which process to kill and this method
incurs considerable overhead. The process that has done the least amount of work is
killed by the Operating system firstly.
QUESTION BANK

1. What are deadlocks? What are its characteristics? Explain the necessary
conditions forits occurrence.
2. Explain the process of recovery from deadlock.
3. Describe RAG:
i) With deadlock
ii) With a cycle but no deadlock
4. What is Resource Allocation Graph (RAG)? Explain how RAG is very useful in
describing deadly embrace (dead lock ) by considering your own example.
5. With the help of a system model, explain a deadlock and explain the
necessary conditions that must hold simultaneously in a system for a deadlock
to occur.
6. Explain how deadlock can be prevented by considering four necessary
conditions cannot hold.
7. Using Banker's algorithm determines whether the system is in a safe state.
8. How is a system recovered from deadlock? Explain the different methods used
to recover from deadlock.
9. Explain deadlock detection with algorithm and example
10. Define the terms: safe state and safe sequence. Give an algorithm to find
whether or not a system is in a safe state.

You might also like