0% found this document useful (0 votes)
17 views47 pages

Unit-3 OS Deadlock

Uploaded by

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

Unit-3 OS Deadlock

Uploaded by

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

Operating System

Unit – 3 C
Deadlock

Unit- 5 Deadlock (Prof. Aanchal Phutela) 1


Disclaimer
 It is hereby declared that the production of the said content is meant for
non-commercial, scholastic and research purposes only.

 We admit that some of the content or the images provided in this channel's
videos may be obtained through the routine Google image searches and
few of them may be under copyright protection. Such usage is completely
inadvertent.

 It is quite possible that we overlooked to give full scholarly credit to the


Copyright Owners. We believe that the non-commercial, only-for-
educational use of the material may allow the video in question fall under
fair use of such content. However we honor the copyright holder's rights
and the video shall be deleted from our channel in case of any such claim
received by us or reported to us.
Unit- 5 Deadlock (Prof. Aanchal Phutela) 2
Topics to be covered
 Deadlock and its Principles
 Four Necessary Conditions of Deadlock
 Deadlock Prevention
 Resource Allocation Graph (RAG)
 Deadlock Avoidance - Banker’s algorithm
 Deadlock Detection & Recovery.

Unit- 5 Deadlock (Prof. Aanchal Phutela) 3


What is Deadlock?

Unit- 5 Deadlock (Prof. Aanchal Phutela) 4


Definition of Deadlock
 A deadlock consists of a set of blocked processes, each holding
a resource and waiting to acquire a resource held by another
process in the set.
For Example:
 two processes each want to record a scanned document on a CD.
 process 1 requests for scanner & gets it
 process 2 requests for CD writer & gets it
 process 1 requests CD writer but is blocked
 process 2 requests scanner but is blocked.
 At this point both processes are blocked and will remain so
forever,
 This situation is called a deadlock
Unit- 5 Deadlock (Prof. Aanchal Phutela) 5
Deadlock
 The processes are the cars.
 The resources are the spaces occupied by the cars

Deadlock possible Deadlock occurred

Unit- 5 Deadlock (Prof. Aanchal Phutela) 6


Sequence of events to use a Resource

Unit- 5 Deadlock (Prof. Aanchal Phutela) 7


Necessary Conditions for Deadlock
1. Mutual exclusion:
• At least one resource type in the system which can be used in
non-shareable mode (i.e. one at a time) for example printer.
• Because many resource can be shared by more than one
process at a time (e.g., Memory location).
2. Hold and Wait:
• Processes are allowed to request for new resources without
releasing the resources they are currently holding.
3. No pre-emption:
• A resource can be released only voluntarily by the process
holding it after that process has completed its task.

Unit- 5 Deadlock (Prof. Aanchal Phutela) 8


Necessary Conditions for Deadlock
4. Circular wait:
• Two or more processes must form a circular chain in which
each process is waiting for a resource that is held by the next
member of the chain.
 All four of these conditions must hold simultaneously in a system
for a deadlock to occur.

Unit- 5 Deadlock (Prof. Aanchal Phutela) 9


Deadlock Modeling
 For Deadlock modeling a directed graph, called Resource
Allocation Graph (RAG) is used.
 Notation used for representation of RAG -

Unit- 5 Deadlock (Prof. Aanchal Phutela) 10


Construction of Resource Allocation Graph

R
(b)

Figure : Resource allocation graphs


(a) Holding a resource. (b) Requesting a resource. (c)
Deadlock.
Unit- 5 Deadlock (Prof. Aanchal Phutela) 11
Resource Allocation Graph
Problem-1 : Find the system is in a deadlock state
or not?

Unit- 5 Deadlock (Prof. Aanchal Phutela) 12


Resource Allocation Graph
Problem-2 : Find the system is in a deadlock state or not?

Unit- 5 Deadlock (Prof. Aanchal Phutela) 13


Resource Allocation Graph
Problem-3 : Find the system is in a deadlock state or not?

Unit- 5 Deadlock (Prof. Aanchal Phutela) 14


Handling Deadlocks In DOS
 Handling of deadlocks in distributed systems is more complex than
in centralized systems.
 Because the resources, the processes, and other relevant
information are scattered on different nodes of the system.
 Strategies to handle deadlocks:
1. Prevention – constraints are imposed on the way in which
processes request resources in order to prevent deadlocks.
2. Avoidance – resources are carefully allocated to avoid deadlocks.
3. Detection and recovery – deadlocks are allowed to occur and a
detection algorithm is used to detect them. After deadlock is
detected it is resolved by certain means.
4. Ignore – do nothing, just ignore the problem.
Unit- 5 Deadlock (Prof. Aanchal Phutela) 15
Deadlock Prevention
 Deadlock can be prevented by violating the one of the four
conditions that leads to deadlock.
• Attacking the Mutual Exclusion Condition
• Attacking the Hold and Wait Condition
• Attacking the No Preemption Condition
• Attacking the Circular Wait Condition

Unit- 5 Deadlock (Prof. Aanchal Phutela) 16


Violation of Mutual Exclusion Condition
 No deadlock if each resource can be assigned to more than one
process.
 This can violate the hardware properties of a resource.
 We can not assign some resources to more than one process at a
time such as printer.
 Not feasible

Unit- 5 Deadlock (Prof. Aanchal Phutela) 17


Violation of Hold and Wait Condition
1. Conservative Approach : Process is allowed to start execution if
and only if it has acquired all the resources (less efficient, not
implementable, easy, deadlock independence)
 A process is allowed to run if all resources it needed is available.
Otherwise nothing will be allocated and it will just wait.
 Problem with this strategy is that a process may not know
required resources at start of run.
 Resource will not be used optimally.
 It may cause starvation of a process that needs may resources.

Unit- 5 Deadlock (Prof. Aanchal Phutela) 18


Violation of Hold and Wait Condition
2. Do not Hold: A process will acquire all desired resources but
before making any fresh request it must release all the resources
that it currently hold (efficient, implementable)

3. Wait Timeout: We place a maximum time up to which a process


can wait after which process and release all the holding resources.

Unit- 5 Deadlock (Prof. Aanchal Phutela) 19


Violation of No Preemption Condition
Forcefully Pre-emption: We allow a process to forcefully pre-empt
the resource by other processes
 For example - When a process request for a resource that is
currently not available, all the resources held by the process are
taken away from it and process is blocked.
 This method may be used by high priority process or system
process.
 The process which are in waiting state must be selected as a
victim instead of process in the running state.

Unit- 5 Deadlock (Prof. Aanchal Phutela) 20


Violation of Circular Wait Condition
 To provide a global numbering of all the resources.
 Processes can request resources whenever they want to, but all
requests must be made in increasing or decreasing order.
 A process need not acquire them all at once.
 Circular wait is prevented if a process holding resource n cannot
wait for resource m, if m > n.
1.Printer, 2.Scanner, 3.Plotter, 4.Tape drive, 5.CD ROM
 A process may request 1st a CD ROM drive, then tape drive. But it
may not request 1st a plotter, then a Tape drive.
 Resource graph can never have cycle.

 P1 – R1, R2, R4, R43, R56


 P1 – R1, R4, R2 NOT POSSIBLE

Unit- 5 Deadlock (Prof. Aanchal Phutela) 21


Deadlock Avoidance
 In most systems, however, resources are requested one at a time.
The system must be able to decide whether granting a resource is
safe or not and only make the allocation when it is safe.

Safe and Unsafe state


 A state is said to be safe if it is not deadlocked and there is
some scheduling order in which every process can run to
completion if all of them suddenly request their maximum
number of resources immediately.

Unit- 5 Deadlock (Prof. Aanchal Phutela) 22


Safe State
 A total of 10 instance of the resource exist, so with 7 resources
already allocated, there are 3 still free.
 Figure : Demonstration that the state in (a) is safe.

Unit- 5 Deadlock (Prof. Aanchal Phutela) 23


Unsafe State
 Figure : Demonstration that the state in (b) is not safe.

 Thus from the safe state system can guarantee that all process will
finish and from unsafe state no such guarantee can be given.

Unit- 5 Deadlock (Prof. Aanchal Phutela) 24


The Banker’s Algorithm for a Single Resource

It is modeled on the way a small-town banker might deal with


a group of customers to whom he has granted lines of credit.

What the algorithm does is check to see if granting the


request leads to an unsafe state.

If it does, the request is denied.

If granting the request leads to a safe state, it is carried out.

Unit- 5 Deadlock (Prof. Aanchal Phutela) 25


The Banker’s Algorithm for a Single Resource
 Figure : Three resource allocation states:

 The banker’s algorithm considers each request as it occurs, and see if


granting it leads to a safe state. If it does, the request is granted; otherwise, it
is postponed until later. To see if a state is safe, the banker checks to see if he
has enough resources to satisfy some customer. If so, those loans are
assumed to be repaid.
Unit- 5 Deadlock (Prof. Aanchal Phutela) 26
The Banker’s Algorithm for Multiple Resources

• Figure : The banker’s algorithm with multiple


resources. {Max (7,5,5,5) Allocated (5,3,2,2)
avail=max-allocated (1,1,3,3) A=(3,0,1,1)+(1,1,3,3)}

Unit- 5 Deadlock (Prof. Aanchal Phutela) 27


The Banker’s Algorithm for Multiple Resources

Algorithm for checking to see if a state is safe:


1. Look for row, R, whose unmet resource needs all
≤ A. If no such row exists, system will eventually deadlock since no
process can run to completion
2. Assume process of row chosen requests all resources it needs and
finishes. Mark process as terminated, add all its resources to the A
vector.
3. Repeat steps 1 and 2 until either all processes marked terminated
(initial state was safe) or no process left whose resource needs can
be met (there is a deadlock).
4. If the order of execution is: D,A,B,C and E then safe state(no
Deadlock)
5. If D request 1 CDROM then it leads to unsafe state(deadlock)
Unit- 5 Deadlock (Prof. Aanchal Phutela) 28
The Banker’s Algorithm for Multiple Resources

Algorithm for checking to see if a state is safe:


1. Look for row, R, whose unmet resource needs all
≤ A. If no such row exists, system will eventually deadlock
since no process can run to completion
2. Assume process of row chosen requests all resources it needs
and finishes. Mark process as terminated, add all its resources
to the A vector.
3. Repeat steps 1 and 2 until either all processes marked
terminated (initial state was safe) or no process left whose
resource needs can be met (there is a deadlock).

Unit- 5 Deadlock (Prof. Aanchal Phutela) 29


Numerical - The Banker’s Algorithm
 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. Find the safe sequence of
the system with current allocation.

Unit- 5 Deadlock (Prof. Aanchal Phutela) 30


Unit- 5 Deadlock (Prof. Aanchal Phutela) 31
Numerical - The Banker’s Algorithm
 A single processor system has three resource types X, Y and Z, which are shared
by three processes. There are 5 units of each resource type. Consider the
following scenario, where the column alloc denotes the number of units of
each resource type allocated to each process, and the column request denotes
the number of units of each resource type requested by a process in order to
complete execution. Which of these processes will finish LAST? (GATE-2007 | 2-
MARKS)

(A) P0
(B) P1
(C) P2
(D) None of the above, since
the system is in a deadlock

Unit- 5 Deadlock (Prof. Aanchal Phutela) 32


Numerical - The Banker’s Algorithm

(UGC-NET 2016 AUG)

Unit- 5 Deadlock (Prof. Aanchal Phutela) 33


Deadlock Detection
 When this technique is used, the system does not attempt to
prevent deadlocks from occurring.
 Instead, it lets them occur, tries to detect when this happens, and
then takes some action to recover after the fact.
 For deadlock detection, the system must provide
• An algorithm that examines the state of the system to detect
whether a deadlock has occurred
• And an algorithm to recover from the deadlock

Unit- 5 Deadlock (Prof. Aanchal Phutela) 34


Deadlock Detection: one Resource of each type
 There are seven process, A to G, and six resources R through W
• Process A hold R and wants S.
• Process B holds nothing but wants T.
• Process C holds nothing but wants S.
• Process D holds U and wants S and T.
• Process E holds T and wants V.
• Process F holds W and wants S.
• Process G holds V and wants U.

Is this system deadlock ? And if so which processes are involved ?

Unit- 5 Deadlock (Prof. Aanchal Phutela) 35


Deadlock Detection: one Resource of each type
 Figure :
 (a) A resource graph. (b) A cycle extracted from fig(a).

Unit- 5 Deadlock (Prof. Aanchal Phutela) 36


Deadlock Detection: one Resource of each type
Algorithm for detecting Deadlock

1. For each node, N in the graph, perform the following five steps with N as the
starting node.
2. Initialize L to the empty list, designate all arcs as unmarked.
3. Add current node to end of L, check to see if node now appears in L two
times. If it does, graph contains a cycle (listed in L), algorithm terminates.
4. From given node, see if any unmarked outgoing arcs. If so, go to step 5; if not,
go to step 6.
5. Pick an unmarked outgoing arc at random and mark it. Then follow it to the
new current node and go to step 3.
6. If this is initial node, graph does not contain any cycles, algorithm terminates.
Otherwise, dead end. Remove it, go back to previous node, make that one
current node, go to step 3.
Unit- 5 Deadlock (Prof. Aanchal Phutela) 37
Unit- 5 Deadlock (Prof. Aanchal Phutela) 38
Deadlock Detection: one Resource of each type

• We start at R and initialize L to the empty list. Then we add R to


the list and move to the only possibility, A, and add it to L, giving
• L = [R, A].
• A we go to S, giving L = [R, A, S].
• Dead end.
• we restart the algorithm starting at A, resetting L to the empty list.
This search, too, quickly stops, so we start again at B.
• From B get to D, at which time L = [B, T, E, V, G, U, D].
• If we pick S we come to a dead end and backtrack to D. The
second time we pick T and
• update L to be [B, T, E, V, G, U, D, T], at which point we discover
the cycle and stop the algorithm.
Unit- 5 Deadlock (Prof. Aanchal Phutela) 39
Deadlock Detection: Multiple Resources of Each Type

 Each process is initially said to be unmarked. As the algorithm


progresses, processes will be marked, indicating that they are able
to complete and are thus not deadlocked. When the algorithm
terminates, any unmarked processes are known to be deadlocked

Look for an If such a


unmarked process is
found, add If no such
Deadlock process, Pi , the i-th row process
detection for which of C to A, exists, the
algorithm the i-th row mark the algorithm
of R is less process, terminates.
than or and go back
equal to A. to step 1.

Unit- 5 Deadlock (Prof. Aanchal Phutela) 40


Deadlock Detection: Multiple Resources of Each Type

 Three process and four classes for device are there

Unit- 5 Deadlock (Prof. Aanchal Phutela) 41


Deadlock Detection: Multiple Resources of Each Type
 We will check the request matrix,
process 0 request can not satisfied.
 The process 1 request can not be
satisfied.
 Process 2 request can be satisfied so
resources are granted.
 Now A=(0 0 0 0)
 After completion of process 2
 A=(2 2 2 0)
 Now we can assign resources to
process 1.
 After process 1 A=(4 2 2 1)
 So there no dead lock.

Unit- 5 Deadlock (Prof. Aanchal Phutela) 42


Deadlock Recovery

Recovery

Recovery Recovery Recovery


through through through killing
preemption rollback processes

Unit- 5 Deadlock (Prof. Aanchal Phutela) 43


Deadlock Recovery
 Recovery through preemption
• In some cases, it may be possible to temporarily take a
resource away from its current owner and give it to another
process.
• Recovering this way is frequently difficult or impossible.
• Choosing the process to suspend depends largely on which
ones have resources that can easily be taken back.
P1

R1 R2

P2

Unit- 5 Deadlock (Prof. Aanchal Phutela) 44


Deadlock Recovery
 Recovery through killing processes
• The simplest way to break a deadlock is to kill one or more
processes.
• Kill all the process involved in deadlock.
• Kill process one by one.
• After killing each process check for deadlock.
• If deadlock recovered then stop killing more process.
• Otherwise kill another process.

Unit- 5 Deadlock (Prof. Aanchal Phutela) 45


Deadlock Recovery
 Recovery through rollback
• Checkpoint a process periodically.
• Check pointing a process means that its state is written to a file
so that it can be restarted later.
• When deadlock is detected, rollback the preempted process up
to the previous safe state before it acquired that resource.

Unit- 5 Deadlock (Prof. Aanchal Phutela) 46


Unit- 5 Deadlock (Prof. Aanchal Phutela) 47

You might also like