0% found this document useful (0 votes)
23 views24 pages

Unit 3B (Kca 203)

Uploaded by

Virat Kohli
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views24 pages

Unit 3B (Kca 203)

Uploaded by

Virat Kohli
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 24

Deadlock

The Deadlock problem


 In a computer system A 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.
Examples
 A process in operating system uses
resources in the following way.
1. request the resource
2. use the resource
3. release the resource
Necessary Conditions for
deadlocks
Deadlock can arise if the following four conditions hold
simultaneously
Mutual exclusion. No resource can be shared by more than
one process at a time.
Hold and wait. There must exist a process that is holding at
least one resource and is waiting to acquire additional
resources that are currently being held by other processes.
No preemption. A resource cannot be preempted.
Circular wait. There is a cycle in the wait-for graph.
Graph-theoretic models for
deadlock
 Wait-for graph.

 Resource-allocation graph.
Wait-for graph: .A wait-for graph in computer science is a
directed graph used for deadlock detection in operating systems . In this algorithm, we only
have processes as vertices in the graph. If the Wait-for-Graph contains a cycle then we can say
the system is in a Deadlock state.
Resource allocation graph(RAG)
 Resource allocation graph explain to us what is the state of the system in terms
of processes and resources.
 RAG contains vertices and edges. In RAG vertices are two type –
 1. Process vertex – Every process will be represented as a process vertex and is represented
with a circle.
2. Resource vertex – Every resource will be represented as a resource vertex and represents
as a box. It is also two type –
 Single instance type resource – It represents as a box, inside the box, there will be one
dot.So the number of dots indicate how many instances are present of each resource type.
 Multi-resource instance type resource – It also represents as a box, inside the box, there
will be many dots present.
 Now coming to the edges of RAG.There are two types of edges in RAG –
 1. Assign Edge – If you already assign a resource to a process then it is called Assign edge.
2. Request Edge – It means in future the process might want some resource to complete the
execution, that is called request edge.
Resource allocation graph
P1 P2 P1 P2

r1 r2

P3 P3
Resource allocation graph With deadlock
Without deadlock
Wait-for graph and Resource-
allocation graph conversion
 Any resource allocation graph with a single
copy of resources can be transferred to a
wait-for graph.
P1 P1

P3 P2 P3 P2
Strategies for handling deadlocks
 Deadlock prevention. Prevents deadlocks by restraining requests made to ensure
that at least one of the four deadlock conditions cannot occur.

 Deadlock avoidance. Dynamically grants a resource to a process if the resulting


state is safe. A state is safe if there is at least one execution sequence that allows
all processes to run to completion. (Banker's Algorithm)

 Deadlock detection and recovery. Allows deadlocks to form; then finds and
breaks them. Recovery is done through preemption, checkpoint and rollback , by
abort or killing the process.

 Deadlock ignorance. (Ostrich Method):In the Deadlock ignorance method the OS


acts like the deadlock never occurs and completely ignores it even if the deadlock
occurs. This method only applies if the deadlock occurs very rarely. The
algorithm is very simple. It says ” if the deadlock occurs, simply reboot the
system and act like the deadlock never occurred.” That’s why the algorithm is
called the Ostrich Algorithm.
Deadlock avoidance
 In deadlock avoidance, the request for any resource will
be granted if the resulting state of the system doesn't
cause deadlock in the system. The state of the system
will continuously be checked for safe and unsafe states.
 The simplest and most useful approach states that the
process should declare the maximum number of
resources of each type it may ever need.
 The resource allocation state of a system can be defined
by the instances of available and allocated resources, and
the maximum instance of the resources demanded by the
processes.
Deadlock detection
 In single instanced resource types, if a cycle is
being formed in the system then there will
definitely be a deadlock.
 On the other hand, in multiple instanced resource
type graph, detecting a cycle is not just enough.
We have to apply the safety algorithm on the
system by converting the resource allocation
graph into the allocation matrix and request
matrix.
Bankers Algorithm:
 Banker’s algorithm is named so because it is used in the banking system to check
whether a loan can be sanctioned to a person or not.
 In other words, the bank would never allocate its money in such a way that it can
no longer satisfy the needs of all its customers. The bank would try to be in a safe
state always.
 Characteristics of Banker's Algorithm
 If any process requests for a resource, then it has to wait.
 This algorithm consists of advanced features for maximum resource
allocation.
 There are limited resources in the system we have.
 In this algorithm, if any process gets all the needed resources, then it is that it
should return the resources in a restricted period.
 Various resources are maintained in this algorithm that can fulfill the needs
of at least one client.
 Data Structures used to implement the Banker’s Algorithm
Some data structures that are used to implement the banker's algorithm are:
1. Available
It is an array of length m. It represents the number of available resources of each
type. If Available[j] = k, then there are k instances available, of resource
type Rj.
2. Max
It is an n x m matrix which represents the maximum number of instances of each
resource that a process can request. If Max[i][j] = k, then the process Pi can
request atmost k instances of resource type Rj.
3. Allocation
It is an n x m matrix which represents the number of resources of each type
currently allocated to each process. If Allocation[i][j] = k, then process Pi is
currently allocated k instances of resource type Rj.
4. Need
It is a two-dimensional array. It is an n x m matrix which indicates the remaining
resource needs of each process. If Need[i][j] = k, then process Pi may
need k more instances of resource type Rj to complete its task.
 Banker’s algorithm comprises of two algorithms:
1. Safety algorithm
2. Resource request algorithm
Available=total resource-total allocation
Need= max-allocation
Safety Algorithm
 A safety algorithm is an algorithm used to find whether or not a system is in its
safe state. The algorithm is as follows:
1. Let Work and Finish be vectors of length m and n, respectively. Initially,Work =
Available Finish[i] =false for i = 0, 1, ... , n - 1.
This means, initially, no process has finished and the number of available
resources is represented by the Available array.
2. Find an index i such that both Finish[i] ==false Needi <= Work If there is no such i
present, then proceed to step 4.
It means, we need to find an unfinished process whose needs can be satisfied by
the available resources. If no such process exists, just go to step 4.
3. Perform Work + Allocationi Finish[i] = true Go to step 2.
When an unfinished process is found, then the resources are allocated and the
process is marked finished. And then, the loop is repeated to check the same for
all other processes.
4. If Finish[i] == true for all i, then the system is in a safe state.
That means if all processes are finished, then the system is in safe state.
Resource Request Algorithm
It is mainly used to determine whether requests can be safely granted or
not.
Let Requesti be the request vector for the process Pi. If Requesti[j]==k, then process Pi
wants k instance of Resource type Rj.When a request for resources is made by the
process Pi, the following are the actions that will be taken:
1. If Requesti <= Needi, then go to step 2;else raise an error condition, since the
process has exceeded its maximum claim.
2.If Requesti <= Availablei then go to step 3; else Pi must have to wait as resources are
not available.
3.Now we will assume that resources are assigned to process Pi and thus perform the
following steps:
Available= Available-Requesti;
Allocationi=Allocationi +Requesti;
Needi =Needi - Requesti;
If the resulting resource allocation state comes out to be safe, then the transaction is
completed and, process Pi is allocated its resources. But in this case, if the new state is
unsafe, then Pi waits for Requesti, and the old resource-allocation state is restored.
Consider a system that contains five processes P1, P2, P3, P4, P5 and the
three resource types A, B and C. Following are the resources types: A has 10,
B has 5 and the resource type C has 7 instances.

Process Allocation Max Available


A B C A B C A B C

P1 0 1 0 7 5 3 3 3 2
P2 2 0 0 3 2 2
P3 3 0 2 9 0 2
P4 2 1 1 2 2 2
P5 0 0 2 4 3 3

Answer the following questions using the banker's algorithm:


1.What is the reference of the need matrix?
2.Determine if the system is safe or not.
3.What will happen if the resource request (1, 0, 0) for process P1 can the system accept this
request immediately?
Ans. 2: Context of the need matrix is as follows:
Need [i] = Max [i] - Allocation [i]
Need for P1: (7, 5, 3) - (0, 1, 0) = 7, 4, 3
Need for P2: (3, 2, 2) - (2, 0, 0) = 1, 2, 2
Need for P3: (9, 0, 2) - (3, 0, 2) = 6, 0, 0
Need for P4: (2, 2, 2) - (2, 1, 1) = 0, 1, 1
Need for P5: (4, 3, 3) - (0, 0, 2) = 4, 3, 1
Hence, we created the context of need matrix.

Process Need
A B C

P1 7 4 3
P2 1 2 2
P3 6 0 0
P4 0 1 1
P5 4 3 1
Ans. 2: Apply the Banker's Algorithm:
Available Resources of A, B and C are 3, 3, and 2.
Now we check if each type of resource request is available for each process.
Step 1: For Process P1: Need <= Available
7, 4, 3 <= 3, 3, 2 condition is false.
So, we examine another process, P2.
Step 2: For Process P2: Need <= Available
1, 2, 2 <= 3, 3, 2 condition true
New available = available + Allocation
(3, 3, 2) + (2, 0, 0) => 5, 3, 2
Similarly, we examine another process P3.
Step 3: For Process P3: Need <= Available
6, 0, 0 < = 5, 3, 2 condition is false.
Similarly, we examine another process, P4.
Step 4: For Process P4: Need <= Available
0, 1, 1 <= 5, 3, 2 condition is true
New Available resource = Available + Allocation
5, 3, 2 + 2, 1, 1 => 7, 4, 3
Similarly, we examine another process P5.
Step 5: For Process P5: Need <= Available
4, 3, 1 <= 7, 4, 3 condition is true
New available resource = Available + Allocation
7, 4, 3 + 0, 0, 2 => 7, 4, 5
Now, we again examine each type of resource request for processes P1 and P3.
Step 6: For Process P1: Need <= Available
7, 4, 3 <= 7, 4, 5 condition is true
New Available Resource = Available + Allocation
7, 4, 5 + 0, 1, 0 => 7, 5, 5
So, we examine another process P2.
Step 7: For Process P3:
P3 Need <= Available
6, 0, 0 <= 7, 5, 5 condition is true
New Available Resource = Available + Allocation
7, 5, 5 + 3, 0, 2 => 10, 5, 7
Hence, we execute the banker's algorithm to find the safe state and the safe
sequence like P2, P4, P5, P1 and P3.
 Ans. 3: For granting the Request (1, 0, 2), first
we have to check that Request <= Available,
that is (1, 0, 2) <= (3, 3, 2), since the condition
is true. So the process P1 gets the request
immediately.

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