Unit-3 OS Deadlock
Unit-3 OS Deadlock
Unit-3 OS Deadlock
Deadlock
1
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.
2
What is Deadlock?
3
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
4
Deadlock
▪ The processes are the cars.
▪ The resources are the spaces occupied by the cars
5
Sequence of events to use a Resource
6
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.
7
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.
8
Deadlock Modeling
▪ For Deadlock modeling a directed graph, called Resource
Allocation Graph (RAG) is used.
▪ Notation used for representation of RAG -
9
Construction of Resource Allocation Graph
R
(b)
11
Resource Allocation Graph
Problem-2 : Find the system is in a deadlock state or not?
12
Resource Allocation Graph
Problem-3 : Find the system is in a deadlock state or not?
13
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.
14
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
15
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
16
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.
17
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)
18
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.
19
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.
20
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.
21
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.
22
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.
23
The Banker’s Algorithm for a Single Resource
24
The Banker’s Algorithm for a Single Resource
▪ Figure : Three resource allocation states:
26
The Banker’s Algorithm for Multiple Resources
28
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.
29
30
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
31
Numerical - The Banker’s Algorithm
32
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
33
Deadlock Detection: one Resource of each type
34
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.
35
36
Deadlock Detection: one Resource of each type
38
Deadlock Detection: Multiple Resources of Each Type
39
Deadlock Detection: Multiple Resources of Each Type
40
Deadlock Recovery
Recovery
41
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
42
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.
43
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.
44
45