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

Complete Deadlock Unit3 Os Sanjaygoswami

Uploaded by

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

Complete Deadlock Unit3 Os Sanjaygoswami

Uploaded by

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

Deadlock

Deadlock is a situation where The execution of two or more


processes is blocked because each process holds some resource
and waits for another resource held by some other process.
Example-

Here
 Process P1 holds resource R1 and waits for resource R2 which
is held by process P2.
 Process P2 holds resource R2 and waits for resource R1 which
is held by process P1.
 None of the two processes can complete and release their
resource.
 Thus, both the processes keep waiting infinitely.

SanjayGoswami, Deadlock (Unit-3 OS), UCER Page 1


Conditions For Deadlock- (m.imp.)
There are following 4 necessary conditions for the occurrence of
deadlock-
1. Mutual Exclusion
2. Hold and Wait
3. No preemption
4. Circular wait

Mutual Exclusion-

 By this condition,
 There must exist at least one non sharable resource in the
system which can be used by only one process at a time.
 If there exists no such resource, then deadlock will never occur.
 Printer is an example of a resource that can be used by only
one process at a time.

Hold and Wait-


By this condition,

SanjayGoswami, Deadlock (Unit-3 OS), UCER Page 2


 There must exist a process which holds some resource and
waits for another resource held by some other process.

No Preemption-
By this condition,
 Once the resource has been allocated to the process, it can not
be preempted.
 It means resource can not be snatched forcefully from one
process and given to the other process.
 The process must release the resource voluntarily by itself.

SanjayGoswami, Deadlock (Unit-3 OS), UCER Page 3


Circular Wait-
A process is waiting for the resource held by the second process,
which is waiting for the resource held by the third process and so
on, till the last process is waiting for a resource held by the first
process. This forms a circular chain. 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.

SanjayGoswami, Deadlock (Unit-3 OS), UCER Page 4


Here,
 Process P1 waits for a resource held by process P2.
 Process P2 waits for a resource held by process P3.
 Process P3 waits for a resource held by process P4.
 Process P4 waits for a resource held by process P1.

SanjayGoswami, Deadlock (Unit-3 OS), UCER Page 5


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

Important Note-

1. All these 4 conditions must hold simultaneously for the


occurrence of deadlock.
2. If any of these conditions fail, then the system can be
ensured deadlock free.

Resource Allocation Graph-

 Resource Allocation Graph (RAG) is a directed graph that


represents the state of a system pictorially.
 There are two components of RAG- Vertices and Edges.
 It is divided in to: A set of vertices V and a set of edges E.
SanjayGoswami, Deadlock (Unit-3 OS), UCER Page 6
 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
 Symbols are used:

 Process

 Resource Type with 4 instances


 Pi requests instance of Rj

 Pi is holding an instance of Rj

Example: Here, there are 3 processes P1, P2 and P3. There are
4 resources R1 and R3 with one instance each, R2 with two
instances, R4 with four instances.
SanjayGoswami, Deadlock (Unit-3 OS), UCER Page 7
Example:

SanjayGoswami, Deadlock (Unit-3 OS), UCER Page 8


Important Rule:
 If graph contains no cycles  no deadlock.
 If graph contains a cycle 
 if only one instance per resource
type, then guaranteed deadlock.
 if several instances per resource
type, possibility of deadlock.

Deadlock Handling-
The various strategies for handling deadlock are-
1. Ensure that the system will never enter
a deadlock state:
a) Deadlock Prevention
b) Deadlock Avoidance
2. Deadlock Detection and Recovery
(allow the system to enter a deadlock
state and then recover)

Deadlock Prevention-

 This strategy involves designing a system that violates


one of the four necessary conditions required for the
occurrence of deadlock.
 This ensures that the system remains free from the
deadlock.

SanjayGoswami, Deadlock (Unit-3 OS), UCER Page 9


Mutual Exclusion-
 To violate this condition, all the system resources must be
such that they can be used in a shareable mode.
 In a system, there are always some resources which are
mutually exclusive by nature.
 So, this condition cannot be violated.

Hold and Wait-


This condition can be violated in the following ways-
Approach-01:
In this approach,
 A process has to first request for all the resources it requires
for execution.
 Once it has acquired all the resources, only then it can start
its execution.
 This approach ensures that the process does not hold some
resources and wait for other resources.
Drawbacks-
The drawbacks of this approach are-
 It is less efficient.
 It is not implementable since it is not possible to predict in
advance which resources will be required during execution.

Approach-02:
In this approach,

SanjayGoswami, Deadlock (Unit-3 OS), UCER Page 10


 A process is allowed to acquire the resources it desires at the
current moment.
 After acquiring the resources, it start its execution.
 Now before making any new request, it has to compulsorily
release all the resources that it holds currently.
 This approach is efficient and implementable.

No Preemption-

This condition can by violated by forceful preemption.


 Consider a process is holding some resources and request
other resources that can not be immediately allocated to it.
 Then, by forcefully preempting the currently held resources,
the condition can be violated.
A process is allowed to forcefully preempt the resources
possessed by some other process only if-
 It is a high priority process or a system process.
 The victim process is in the waiting state.

Circular Wait-
 This condition can be violated by not allowing the processes
to wait for resources in a cyclic manner.
 To violate this condition, the following approach is followed-

Approach-
 A natural number is assigned to every resource.
 Each process is allowed to request for the resources either in
only increasing or only decreasing order of the resource
number.
SanjayGoswami, Deadlock (Unit-3 OS), UCER Page 11
 In case increasing order is followed, if a process requires a
lesser number resource, then it must release all the resources
having larger number and vice versa.
 This approach is the most practical approach and
implementable.
 However, this approach may cause starvation but will never
lead to deadlock.

Deadlock Avoidance

It is better to avoid a deadlock rather than take measures after the


deadlock has occurred. The wait for graph can be used for
deadlock avoidance. This is however only useful for smaller
databases as it can get quite complex in larger databases.

Wait for Graph

SanjayGoswami, Deadlock (Unit-3 OS), UCER Page 12


Safe Sate
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 in the systems such that for each P i, 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.

Avoidance Algorithm:
 Single instance of a resource type
 Use a resource-allocation graph
 Multiple instances of a resource type
 Use the banker’s algorithm

Banker Algorithm-

SanjayGoswami, Deadlock (Unit-3 OS), UCER Page 13


Banker’s algorithm is named so because it is used in banking
system to check whether loan can be sanctioned to a person or
not. Suppose there are n number of account holders in a bank
and the total sum of their money is S. If a person applies for a
loan then the bank first subtracts the loan amount from the total
money that bank has and if the remaining amount is greater than
S then only the loan is sanctioned. It is done because if all the
account holders comes to withdraw their money then the bank
can easily do it.
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 safe state always.
In similar way this algorithm is used for deadlock avoidance.
There are few data structures (matrix) used in it as below:

1. Available
Available[R1] = K
It means K instances of resource type R1 are currently
available.

2. Max
Max[P1][R1] = K
It means process P1 is allowed to ask for maximum K
instances of resource type R1.

3. Allocation
Allocation[P1][R1] = K
It means K instances of resource type R1 have been
allocated to the process P1.
4. Need

SanjayGoswami, Deadlock (Unit-3 OS), UCER Page 14


Need[P1][R1] = K
It means process P1 requires K more instances of resource
type R1 for execution.

Example1: 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:

a) What is the reference of the need matrix.


b) Determine if the system is safe or not.
c) What will happen if the resource request (1, 0, 0) for process
P1 can the system accept this request immediately?

Ans. a): 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

SanjayGoswami, Deadlock (Unit-3 OS), UCER Page 15


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. b): Apply the Banker's Algorithm:

Available Resources of type 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, so P2 release its held
resources.

New available = available + Allocation

(3, 3, 2) + (2, 0, 0) => 5, 3, 2

Similarly, we examine another process P3.


SanjayGoswami, Deadlock (Unit-3 OS), UCER Page 16
Step 3: For Process P3:

P3 Need <= Available

[6, 0, 0] < = [ 5, 3, 2 ] condition is false.

Similarly, we examine another process, P4.

Step 4: For Process P4:

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:

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:

P1 Need <= Available


[7, 4, 3] <= [7, 4, 5] condition is true
SanjayGoswami, Deadlock (Unit-3 OS), UCER Page 17
New Available Resource = Available + Allocation

7, 4, 5 + 0, 1, 0 => 7, 5, 5, So, we examine another process P3.

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. c): 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.

Example2[aktu2013-14]: Consider a system with five processes


P1, P2, P3, P4, P5 and the three resource types R1, R2 and R3.
At time T0, the following snapshot of the system has been taken:
Process Allocation Max Available
R1 R2 R3 R1 R2 R3
R1 R2 R3

P1 1 1 2 4 3 3 3 1 0

P2 2 1 2 3 2 2

P3 4 0 1 9 0 2

P4 0 2 0 7 5 3

P5 1 1 2 11 2 3

SanjayGoswami, Deadlock (Unit-3 OS), UCER Page 18


Answer the following questions using the banker's algorithm:

a) Determine the total amount of each resource types.


b) Compute the need matrix.
c) Determine if the system is safe or not using Banker’s Algo.
d) Would the following requests be granted in current state?
i) P1(3, 3, 1) Not 3,3,1<=3,1,0 request<=available
ii) P2(2, 1, 0) <= (3,1,0)

a) Allocation[I , j]+Available[ j ] = [11, 6, 7] {each column of


allocation matrix represent resource of same type; so in R1
column sum all values 1+2+4+0+1=8 total allocated instances of
R1+ 3 in available vector =11….}
b) Need[i] = Max[i] – Allocation [i]

Process R1 R2 R3

P1 3 2 1

P2 1 1 0

P3 5 0 1

P4 7 3 3

P5 10 1 1

Now for process P1: NEED<=AVAILABLE


[3, 2, 1]<=[3,1,0] False.

Now for process P2: [1,1,0] <= [3,2,0] True, so P2 IS


COMPLETED. Now NEW AVAILABLE=[3,1,0]+[2,1,2] =[5,2,2]

SanjayGoswami, Deadlock (Unit-3 OS), UCER Page 19


Now for process P3: [5,0,1]<=[5,2,2] True, so P2 IS
COMPLETED. Now NEW AVAILABLE= [5,2,2] + [4,0,1] = [9,2,3]

Now for process P4: [7,3,3]<=[9,2,3] False.

Now for process P5: [10,1,1]<=[9,4,3] False.

Repeat the procedure or next iteration, then

Now for process P1: [3,2,1]<=[9,2,3] True, then


NEW AVAILABLE = [9,2,3] + [1,1,2] = [10,3,5]

Now for process P4: [7,3,3]<=[10,3,5] again True,


NEW AVAILABLE = [10,3,5] + [0,2,0] = [10,5,5]

Now for process P5: [10,1,1]<=[10,5,5] True, then


New Available = [10,5,5] + [1,1,2] = [11, 6 , 7]

So SAFE SEQUENCE is: [P2,P3,P1,P4,P5]

c) i) P1(3, 3, 1) Not immediately granted because it should be


request<=available but (3,3,1)<=(3,1,0) false.
ii) P2(2, 1, 0) <= (3,1,0) is immediately granted
(2,1,0)<=(3,1,0).

Example3[2011-12]: Consider a system consisting of ‘m’


resources of the same type, being shared by ‘n’ processes.
Resources can be requested and released by only one at a
time. Show that the system is deadlock free if each process
needs at most ‘m’ resources and the sum of all needs is less
than (m+n).

SanjayGoswami, Deadlock (Unit-3 OS), UCER Page 20


Solution: suppose maxi, needi and allocatei are the max, need
and allocate resources of process ith respectively.

(a) Σ i=1..n maxi < (m+n)


(b) maxi >= 1 for all i= 1 to m.
Since needi = maxi – allocatei; deadlock free then,

(c) Σ i=1..n allocatei = m (only one at a time and m


resources are same type).
But Σ i = 1..n needi + allocatei

= Σ i=1..n maxi < (m+n)

Or Σ i = 1..n needi + m < (m+n) [from (c)]

Or Σ i = 1..n needi < n

This implies that there exists a process Pi such that needi


= 0, But maxi >= 1 [from (b)]

So there is a contradiction. So system is deadlock free.


Example4[2012-13]: A system contains of ‘6’ units of
resource being shared by ‘n’ processes. What is the
maximum value of ‘n’, for which the system will be deadlock
free if the maximum requirement of each process is three.
Solution: m =6, n =n
N * 3 (need of each n; i.e. sum of all need)
[3+3+3+……+n]
<= 3n < 6 + n <= 2n < 6 then n<=3.
SanjayGoswami, Deadlock (Unit-3 OS), UCER Page 21
Deadlock Detection and Recovery-

 This strategy involves waiting until a deadlock occurs.


 After deadlock occurs, the system state is recovered.

 The main challenge with this approach is detecting the deadlock.

Deadlock Detection

If resources have single instance:


In this case for Deadlock detection we can run an algorithm to
check for cycle in the Resource Allocation Graph. Presence of
cycle in the graph is the sufficient condition for deadlock.

SanjayGoswami, Deadlock (Unit-3 OS), UCER Page 22


In the above diagram, resource 1 and resource 2 have single
instances. There is a cycle R1 → P1 → R2 → P2. So,
Deadlock is confirmed.

If there are multiple instances of resources:


Detection of the cycle is necessary but not sufficient condition for
deadlock detection, in this case, the system may or may not be
in deadlock varies according to different situations, so different
approaches such as Safety Algorithm, Banker’s Algorithm used.

Deadlock Recovery

There are some following approaches used for deadlock


recovery:

SanjayGoswami, Deadlock (Unit-3 OS), UCER Page 23


For Resource

 Preempt the resource

We can snatch one of the resources from the owner of the


resource (process) and give it to the other process with the
expectation that it will complete the execution and will release this
resource sooner. Well, choosing a resource which will be
snatched is going to be a bit difficult.

 Rollback to a safe state

System passes through various states to get into the deadlock


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

SanjayGoswami, Deadlock (Unit-3 OS), UCER Page 24


For Process

 Kill a process

Killing a process can solve our problem but the bigger concern is
to decide which process to kill. Generally, Operating system kills a
process which has done least amount of work until now.

 Kill all process

This is not a suggestible approach but can be implemented if the


problem becomes very serious. Killing all process will lead to
inefficiency in the system because all the processes will execute
again from starting.
Thread cancellation
Terminating a thread before it has completed is called Thread
cancellation. For an example, if multiple threads are concurrently
searching through a database and one thread returns the result,
the remaining threads might be canceled. Another situation might
be occurred when a user presses a button on a web browser that
stops a web page from loading any further. Often, using several
threads a web page loads — each image is loaded in a separate
thread. When the stop button is pressed by a user on the
browser, all threads loading the page are canceled. A thread
which is to be cancelled is often referred to as the target thread.
Cancellation of a target thread may occur in two different cases −
 Asynchronous cancellation − One thread terminates
immediately the target thread.
 Deferred cancellation − The target thread checks
periodically whether it should terminate, allowing it an
opportunity to terminate itself in an orderly fashion.

SanjayGoswami, Deadlock (Unit-3 OS), UCER Page 25

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