Unit 3
Unit 3
UNIT NO.3
DEPARTMENT OF COMPUTER TECHNOLOGY
IV-SEMESTER
OPERATING SYSTEMS
Syllabus of Unit III
Deadlocks
System Model,
Deadlock characterization-necessary conditions,
Resource allocation graph (RAG),
Methods for handling deadlock
deadlock avoidance, deadlock detection, deadlock
prevention, Recovery from deadlock.
System Model
DTEL 4
The Deadlock Problem
DTEL 5
DEADLOCKS The Deadlock Problem
DTEL 6
DEADLOCKS Deadlock Characterization
Necessary condition:
Deadlock can arise if four conditions hold simultaneously.
● Mutual exclusion: only one process at a time can use a
resource
● Hold and wait: a process holding at least one resource is
waiting to acquire additional resources held by other processes
● No preemption: a resource can be released only voluntarily
by the process holding it after that process has completed its
task
● Circular wait: there exists a set {P0, P1, …, P0} of waiting
processes such that P0 is waiting for a resource that is held by
P1, P1 is waiting for a resource that is held by
P2, …, Pn–1 is waiting for a resource that is held by
Pn, and Pn is waiting for a resource that is held by P0
7
DTEL 7
DEADLOCKS Resource-Allocation Graph(RAG)
A deadlock is described in terms of a directed
graph G(V,E) called as a system Resource
Graph(RAG).
DTEL 8
DEADLOCKS Resource-Allocation Graph(RAG)
DTEL 9
DEADLOCKS Resource Allocation Graph With A Deadlock
10
DTEL 10
DEADLOCKS Graph With A Cycle But No Deadlock
DTEL 11
DEADLOCKS Relationship of cycles to deadlocks
Basic Facts
12
DTEL 12
DEADLOCKS Methods for Handling Deadlocks
DTEL 13
DEADLOCKS Deadlock Prevention
● Two methods:
1. Direct method: We need to prevent the occurrence of a
circular wait
2. Indirect method: We need to prevent occurrence of one of the
other 3 conditions
● Mutual Exclusion – The mutual-exclusion condition must hold
for non-sharable resources.
● 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
14
DTEL 14
LECTURE TOPIC:-DEADLOCKS Deadlock Prevention
15
DTEL 15
DEADLOCKS Deadlock Prevention
● No Preemption –
● If a process that is holding some resources requests another
resource that cannot be immediately allocated to it, then all
resources currently being held are released
● Preempted resources are added to the list of resources for
which the process is waiting
● A process will be restarted only when it can regain its old
resources, as well as the new ones that it is requesting
● Alternative technique-Explaination
● Circular Wait – impose a total ordering of all resource types,
and require that each process requests resources in an
increasing order of enumeration. For example:
F(tape drive) = 1
F(disk drive) = 5 16
F(printer) = 12
DTEL 16
DEADLOCKS Deadlock Avoidance
DTEL 17
:-DEADLOCKS
Safe State
DTEL 18
DEADLOCKS Safe State Example
● Suppose there are 12 tape drives
max need current usage could ask for
p0 10 5 5
p1 4 2 2
p2 9 7 2
3 drives remain
● current state is safe because a safe sequence exists: <p1,p0,p2>
p1 can complete with current resources
p0 can complete with current+p1
p2 can complete with current +p1+p0
19
DTEL 19
DEADLOCKS Safe State Example
20
DTEL 20
DEADLOCKS Safe, Unsafe , Deadlock State
21
DTEL 21
LECTURE 1:-DEADLOCKS The Deadlock Problem
22
DTEL 22
DEADLOCKS Resource-Allocation Graph Scheme
23
DTEL 23
DEADLOCKS Resource-Allocation Graph with Claim Edges
24
DTEL 24
DEADLOCKS Resource-Allocation Graph Algorithm
25
DTEL 25
DEADLOCKS Banker’s Algorithm (To avoid deadlock)
DTEL 26
DEADLOCKS Banker’s Algorithm (To avoid deadlock)
27
DTEL 27
DEADLOCKS Banker’s Algorithm (To avoid deadlock)
DTEL 28
DEADLOCKS Banker’s Algorithm (To avoid deadlock)
DTEL 29
DEADLOCKS Banker’s Algorithm (To avoid deadlock)
This algorithm implements using two sub algorithms:
i) Safety Algorithm
ii) Resource-request algo
i) Safety Algorithm (used to check whether the system is in safe state or not)
1. Let Work and Finish be vectors of length m and n, respectively. Initialize:
Work = Available (no. of available resources)
Finish [ i ] == false for i = 1,2, …, n. (execution not finished)
2. Find an i such that both
Finish [ i ] == false
Need i ≤ Work/Available
If no such i exists, go to step 4. (It means, we need to find an unfinished process whose
needs can be satisfied by the available resource s)
3. Work = Work + Allocation (Resources freed when process completes!)
Finish[ i ] == true
go to step 2.
4. If Finish [ i ] == true for all i, then the system is in a safe state. 30
That means if all processes are finished, then the system is in safe state. 30
DEADLOCKS Banker’s Algorithm (To avoid deadlock)
32
DTEL 32
DEADLOCKS Banker’s Algorithm (To avoid deadlock)
Example of Banker’s Algorithm (safety Algorithm)
5 processes P0 through P4
3 resource types A (10 units), B (5 units), and C (7 units).
DTEL 33
DEADLOCKS Banker’s Algorithm (To avoid deadlock)
34
DTEL 34
DEADLOCKS Banker’s Algorithm (To avoid deadlock)
35
DTEL 35
DEADLOCKS Banker’s Algorithm (To avoid deadlock)
36
DTEL 36
DEADLOCKS Banker’s Algorithm (To avoid deadlock)
37
DTEL 37
DEADLOCKS Banker’s Algorithm (To avoid deadlock)
38
DTEL 38
Apply the Banker's Algorithm:
41
DTEL 41
Banker’s Algorithm (To avoid deadlock)
DTEL 42
DEADLOCKS Banker’s Algorithm (To avoid deadlock)
DTEL 43
DEADLOCKS Banker’s Algorithm (To avoid deadlock)
44
DTEL 44
DEADLOCKS Banker’s Algorithm (To avoid deadlock)
New Table
DTEL 45
DEADLOCKS Banker’s Algorithm (To avoid deadlock)
DTEL 46
Deadlock Detection
48
DTEL 48
Deadlock Detection
49
DTEL 49
LECTURE 1:-DEADLOCKS Deadlock Detection
50
LECTURE 1:-DEADLOCKS Deadlock Detection
Resource-Allocation Graph and Wait-for Graph
DTEL 51
LECTURE 1:-DEADLOCKS Deadlock Detection
DTEL 52
LECTURE 1:-DEADLOCKS Deadlock Detection
DTEL 54
LECTURE 1:-DEADLOCKS Deadlock Detection
DTEL 55
LECTURE 1:-DEADLOCKS Recovery From Deadlock
56
DTEL 56
LECTURE 1:-DEADLOCKS Recovery From Deadlock
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. It 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 canrollback 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.
57
DTEL 57
LECTURE 1:-DEADLOCKS Recovery From Deadlock
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.
DTEL 58
LECTURE 1:-DEADLOCKS Recovery From Deadlock
● Two Approaches
● Process termination
● Terminate all the deadlock processes
● Terminate processes one by one until the
deadlock is broken
● Resource preemption
59
DTEL 59
LECTURE 1:-DEADLOCKS Recovery From Deadlock
● Abort all deadlocked processes
● This approach will break the deadlock, but at great expense
● Starvation – How do we ensure that starvation will not occur? That is,
how can we guarantee that resources will not always be preempted
from the same process?
61
DTEL 61
LECTURE 1:-DEADLOCKS
Deadlock Ignorance
Ignore the problem as it does not exit.
As handling deadlock is expensive and need lots of
codes which will decrease the performance. so
deadlock are ignored for less critical jobs.
UNIX and WINDOWS , operating system ignore the
deadlock.
If there is deadlock occured in the system, then the
OS will just ignore the deadlock and reboot the
system in order to function well.
This method of solving any problem is called Ostrich
Algorithm
62
DTEL 62
LECTURE 1:-DEADLOCKS
Deadlock Ignorance
Ostrich Algorithm: it is based on the amazing
behavior of the bird Ostrich
During the storm, the Ostrich stick his head in the
sand and pretend there is no storm
This Ostrich algorithm is most widely used technique
in order to ignore the deadlock and also it is used for
the single end users systems.
63
DTEL 63
LECTURE 1:-DEADLOCKS
Deadlock Ignorance
Ostrich Algorithm:
It pretends there is no deadlock in the system.
It is simply assumed that the deadlock will never
occur.
It is reasonable to use if,
deadlock occur very rarely
the cost of their prevention would be high
The UNIX and Windows o.s are utilizing this approach
64
DTEL 64
LECTURE 1:-DEADLOCKS
Deadlock Ignorance
Using ostrich algorithm is one of the method of
dealing with deadlocks.
This will not be a good idea to solve the deadlock
problem, hence the drawbacks are more, without
finding solution to deadlock problems.
The methods for handling deadlocks,
Deadlock detection
Deadlock recovery
Deadlock avoidance
Deadlock prevention
65
DTEL 65
LECTURE 1:-DEADLOCKS Summary
● Four necessary conditions must hold in the system for a deadlock
to occur
● Mutual exclusion
● Hold and wait
● No preemption
● Circular wait
● Four principal methods for dealing with deadlocks
● Use some protocol to (1) prevent or (2) avoid deadlocks, ensuring that the
system will never enter a deadlock state
● Allow the system to enter a deadlock state, (3) detect it, and then recover
Recover by process termination or resource preemption
● (4) Do nothing; ignore the problem altogether and pretend that deadlocks
never occur in the system (used by Windows and Unix)
● To prevent deadlocks, we can ensure that at least one of the four
necessary conditions never holds
66
DTEL 66
THANK YOU
DTEL 67