Unit 5-Notes_OS_Part2
Unit 5-Notes_OS_Part2
INDEX
Page No. 1
Deadlocks
Definition of Deadlock: A set of processes is in a deadlock state when every process in
the set is waiting for an event that can be caused by only other process in the set.
(Events: Resource acquisition and release).
In a deadlock processes never finish executing and system resources are tied up,
preventing other jobs from even starting.
• There is a cycle in
tN tS resource allocation graph.
Hence Deadlock.
A B
Train A Train B
Deadlock characterization:
Necessary conditions: a deadlock situation can arise if the following four conditions hold
simultaneously in a system.
1. Mutual exclusion: at least one resource must be held in a non sharable mode, i.e.
only one process at a time can use the resource.
2. 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.
3. No Preemption: Resources cannot be preempted; i.e. a resource can be released
only voluntarily by the process holding it after that process has completed its task.
4. Circular wait: there must exist a set {P0, P1, P2, …,Pn} 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 held by P0.
Page No. 2
Qu. How the trains deadlock can be resolved?
Classification of Resources
• Sharable resources:
• Can only be used by one user at a time (Requires mutually exclusive access)
• Data areas that need to be written.
• Most external devices like printers.
• The processor.
• preemtable:
-- Use of resource may be preempted and restarted later on.
-- e.g. The processor , The primary memory
• non-preemptable:
-- Use of resource can not be preempted.
-- e.g. Printers. (A new document cannot be printed until the previous is completed.)
Q: Give examples of deadlock, which are not related to a computer system environment:
Ans.
a. River crossing by two goats
b. Traffic deadlock example
Page No. 3
Here each stream of cars holds one intersection but needs another intersection before
it can proceed. Consider each intersection as a resource and each stream of cars as a
process.
1. Mutual exclusion condition applies, since only one stream of vehicles can be on an
intersection of the street at a time.
2. Hold-and-wait condition applies, since each stream of vehicles is occupying an
intersection, and waiting for another intersection.
3. No-preemptive condition applies, since an intersection that is occupied by a stream
of vehicles cannot be taken away from it.
4. Circular wait condition applies, since each stream of vehicles is waiting on the next
stream of vehicles to move. That is, each stream of vehicles in the traffic is waiting
for an intersection held by the next stream of vehicles in the traffic.
ANS:
(A) Deadlock involving single resource type: -
System with 4 tape drives and two processes, if each process holds two tape drives
but needs three, deadlock occurs.
(B) Deadlock involving different resource types: -
System with one printer and one card reader and two processes. Suppose that
process P is holding the card reader and process Q is holding the printer if P now
requests the printer and Q requests the card reader a deadlock occurs.
Qu.: a) Show that the four necessary conditions for deadlock indeed hold in the
examples (A,B) given above.
b) Come up with a simple rule that will avoid deadlocks.
ANS: Allow each process to acquire all the resources it needs in advance.
Page No. 4
Resource Allocation graph(RAG): It is a directed graph used to describe deadlocks
more precisely. It consists of set of vertices V and a set of edges E. the set of vertices is
partitioned into tow types.
P = {P1, P2, …, Pn}, the set containing all the processes in the system and r={R1, R2,…,
Rm}, the set consisting of all the resource types in the system.
Example(1) :
R1 R3
Assignment edge
Request edge P1 P2 P3
Instances of
R4 resource R4
R2
P = {P1,P2,P3}
R = {R1,R2,R3,R4}
E = {P1->R1, P2->R3, R1->P2, R2->P1, R3->P3}
Request Edge Assignment edge
• Resource instances:
One instance of resource type R1
Two instances of resource type R2
One instances of resource type R3
Three instances of resource type R4
• Process states:
Process P1 holding one instance of type R2 and waiting for an instance of type R1
Process P2 holding one instance of type R2 and waiting for an instance of type R3
Process P3 holding one instance of type R3
Page No. 5
Wait for graph: We obtain this graph from the resource allocation graph by removing the
nodes of type resource and collapsing the appropriate edges. In this graph we show the
processes waiting on each other.
Q1: show that request from process P3 for an instance of a resource type R2 in example 1
leads to a deadlock.
Ans.: Adding the request edge P3->R2 in example 1.
There are two cycles in the graph hence there is a chance that the given system is
deadlock.
R1 R3
P3
P1 P2 P3
P1 P2
R2 R4
Fig. Wait for graph for RAG on the left
The four necessary conditions for deadlock hold for the above graph hence the above
system is in a deadlock state.
Q2: Show that the resource allocation graph shown below is not representing a deadlock
system OR Find our whether the following system is deadlocked. Justify your answer.
Ans: As there is a cycle in the graph the system is unsafe. But it is not deadlocked because
the process P4 may release its instance of resource type R2. That resource can then be
allocated to P3, causing the circular wait/cycle to break.
R1 P2 P2 P3
P1
P3 P1 P4
R2
Page No. 6
Q: Draw resource allocation graphs for deadlock examples A & B
Ans :
Example A Example B
Tape drives
P Q
P1 P2
Card reader Printer
Page No. 7
Deadlock Prevention:
For a deadlock to occur each of the four necessary conditions must hold. By ensuring that
at least one of these conditions cannot hold, we can prevent the occurrence of a deadlock.
1) Mutual Exclusion:
i) Shareable resources: Mutually exclusive access is not required; hence these
resources cannot be involved in a deadlock. (e.g. read only files)
ii) Non-Shareable Resources: Some resources like printer are intrinsically non
shareable. Hence mutually exclusive access is required.
Hence in general it is not possible to prevent deadlocks by denying mutual
exclusion condition.
2) Hold and wait: To deny this condition we must guarantee that whenever a process
requests a resource it does not hold any other resources. The different protocols to achieve
this are
a) Each process to request and be allocate all of its resources before it begins
execution.
b) Allow a process to request only when it has none. Here before requesting an
additional resource the process has to release any previously hold resource.
Qu.: Certain system (THE system) allows printer to be preempted from a process. Explain
How this system can eliminate the problem of corrupted printout?
Page No. 8
Ans: It can finish the current page and remember the next page number to be printed for a
process. OS can maintain a table storing process id’s and page numbers to be printed next.
4) Circular wait: In order to ensure that the circular wait condition never holds we may
impose a total ordering of different classes of system resources. In this approach system
resources are divided into different classes Cj where j=1,…,n. Deadlocks are prevented by
requiring all processes to request and acquire their resources in a strictly increasing order of
the specified system resource classes. Moreover acquisition of all resources within a given
class must be made with a single request, and not incremental. In other words once a
process acquires a resource belonging the class Cj, it can only request resources of class
Cj+1 or higher thereafter.
Ex. Two processes require one tape drive and one printer.
P1 P1
T P T (1) P(2)
P2 P2
In the RAG below assume that Pi is a process holding a resource in class Ci.
Process Pi cannot possibly wait for any process that is itself waiting for a resource in class
Ci or lower. Hence circular wait condition possibility is eliminated.
Wait Wait
P1 P2 P3
Wait
The type of wait shown in above figure is impossible since a process holding class 3
resources can’t wait for class 1 resource (since 3>1)
Example: 3 students are writing records with 2 ink pens 1 pencils and 1 erasure, on a
partitioned table with the slot containing these four items open for access to three persons,
as shown in figure below.
Pn Pn
Pl Er
Page No. 9
Class Number of
number instance
Ink pen 1 2
Pencil 2 1
Erasure 3 1
S1 S2 S3
Fig. Resource Allocation graph for the above example without resource ordering
S1 S2 S3
Fig. Resource Allocation graph for the above example with resource ordering
Page No. 10
S1 S2 S3 S1 S2 S3
Fig. Wait for graph without resource Fig. Wait for graph with resource
ordering ( Note cycle in this graph: ordering (No cycles; hence safe state)
Hence Unsafe state)
Deadlock avoidance:
i) This approach requires additional information about how resources are to be requested.
ii) With this knowledge of the complete sequence of requests and release for each process
we can decide for each request whether request can be satisfied or whether the process
must wait to avoid a possible future deadlock.
Whenever a process requests a resource that is currently available, the system must
decide if the resource can be immediately allocated or if the process must wait. The request
is granted only if it leaves the system in a safe state.
In this scheme if a process requests a resource, which is currently available, it may
still have to wait. Thus resource utilization may be lower than without a deadlock
avoidance algorithm.
Safe state: A state is safe if the system can allocate resources to each process (up to it’s
maximum) in some order and still avoid a deadlock.
Page No. 11
Example: Consider a system with total 12 magnetic tape drives and three processes P0,
P1, P2. Resource allocation state of these processes is as follows (at time T0)
P0 10 5 3 5
P1 4 2 2
P2 9 2 7
Ans : The content of the work vector is given as processes are allocated additional tapes.
Work=available (initially)
i) Safety Algorithm:
Work=Work+Allocation Remarks
3 Initially (At time T0)
5 P1 finished
10 P0 finished
12 P2 finished
There exists a safe sequence <P1, P0, P2>. Hence the given resource allocation state is safe.
Page No. 12
Banker’s Algorithm: A deadlock avoidance algorithm:
The name was chosen because the algorithm could be used in banking system to ensure that
the bank never allocates its available cash in such a way that it could no longer satisfy the
needs of all its customers.
Algorithm:
i) When a new process enters the system it must declare the max number of
instances of each resource type that it may need. This number may not exceed
the total number of resources in the system.
ii) When a user process requests a set of resources, it must be determined whether
the allocation of these resources will leave the system in safe state. If so the
resources are allocated otherwise the process must wait until some other process
release enough resources.
Implementation: Let n be the number of processes in the system, let m be the number
of resource types.
Data structure required:
• Availabe: A vector of length m, indicating number of available resources of each
type.
• Max: An mxn matrix defining the max demand of each process.
• Allocation: An nxm matrix defining the no. of resources of each type currently
allocated to each process.
• Need: An nxm matrix indicating the remaining resource needs of each process.
Need [i, j] =max [i,j] – Allocation [i,j]
Example: - Consider a system with five processors {p0, p1, p2, p3, p4} and three resource
types {A, B, C}. Resource type A has 10 instances, B has 5, C has 7 instances.
Suppose that at time T0 following snapshot of system is taken.
Total = [10 5 7] , Available=[10 5 7] – [7 2 5] = [3 3 2]
Find out whether the system is in a safe state.
Page No. 13
As finish[i]=true for all i , the system is in a safe state. ( See the algorithm below)
Programming Assignment:
Simulate the Banker’s Algorithm for deadlock avoidance using a Computer program
Page No. 14
Deadlock avoidance for resource allocation system with only one instance
of each resource type( using resource allocation graph algorithm).
In addition to the request and assignment edges, we introduce a new type of edge, called
the claim edge.
Claim edge A claim edge Pi->Rj indicates that process P1 may request resource Rj at some
time in the future. This edge resembles the request edge in direction, but is represented by a
dashed line.
R1
R1 Suppose P2 requests R2,
claim edge is P2->R2 P1 P2
P1 P2 converted to assignment
edge R2->P2, as this R2
R2
results in a cycle the
request is not granted. Fig. An unsafe state in
Fig. Resource allocation resource allocation graph
graph for deadlock avoidance
Page No. 15
Deadlock Detection: If a system does not employ either a deadlock prevention or
deadlock avoidance algorithm then a deadlock situation may occur. In this environment,
the system may provide:
• An algorithm that examines the state of the system to determine whether a
deadlock has occurred.
• An algorithm to recover from the deadlock.
Deadlock Detection for resource allocation system with single instance of each
resource type:
P5
R1 R3 R4
P1 P2 P3
R2 P4 R5
P5
This is an example of resource allocation system with single instance of each resource type.
Hence the presence of cycle in the graph shows that the system is deadlocked.
The algorithm uses additional data structure ‘Request which is an n*m matrix. It indicates
the current request of each process. If Request [i,j] =k then process Pi is requesting k more
instances of resource type Rj.
Page No. 16
Example (a): Consider a system with resources A, B, C with instances 7,2 and 6
respectively. Suppose that at time T0 we have the following resource allocation state.
(b) Suppose that process P2 makes one additional request for an instance of type C. The
request matrix is modified as follows:
After this for no
Request Available/Work process
ABC ABC Requesti <= Work
P0 000 000 Initially Hence we claim that
P1 202 010 P0 finished system is now
P2 001 deadlocked and
P3 100 processes involved are
P4 002 P1,P2,P3 and P4
Page No. 17
Ex. Consider a system consisting of four resources of the same type that are shared by
three processes, each of which needs at most two resources. Show that the system is
deadlock free.
R1 R2
P0 P1 P2 P3
Total
R1 R2 R3 R3
2 3 2
i) find if the system is in deadlock state
ii) Otherwise find a safe sequence
From the above table it is clear that the system is not in a deadlock state, since there exists
a safe sequence < P2,P1P0,P3 >.
Page No. 18
Recovery from deadlock:
1. process termination
• Abort all deadlocked processes.
• Abort one process at a time untill the deadlock cycle is eliminated.
2. Resource preemption: To eliminate deadlocks using resource preemption we
successfully preempt some resources from processes and give these resources to other
processes until the deadlock cycle is broken.
Issues:
• Selecting a victim (no. of resources a process is holding, amount of time a process
has consumed so far during its execution)
• Rollback (Total, Partial)
• Starvation (Some process is always picked up as a victim: Select a victim based on
no. of rollbacks of a process.)
Ex. Consider the following resource-allocation policy. Requests and releases for resources
are allowed at any time. If a request for resources cannot be satisfied because the resources
are not available, then we check any processes that are blocked, waiting for resources. If
they have the desired resources, then these resources are taken away from them and are
given to the requesting process. The vector of resources for which the process is waiting is
increased to include the resources that were taken away.
For example, consider a system with three resource types and the vector
Available initialized to (4,2,2). If process P0 asks for (2,2,1), it gets them. If P1 asks for
(1,0,1), it gets them. Then, if P0 asks for (0,0,1), it is blocked (resource not available). If P2
now asks for (2,0,0), it gets the available one (1,0,0) and one that was allocated to P0 (since
P0 is blocked). P0’s Allocation vector goes down to (1,2,1) and its Need vector goes up to
(1,0,1).
a. Can deadlock occur? If you answer “yes”, give an example. If you answer “no,”
specify which necessary condition cannot occur.
b. Can indefinite blocking occur? Explain your answer.
Ans:
a) Deadlock cannot occur because preemption exists.
b) Yes. A process may never acquire all the resources it needs if they are continuously
preempted by a series of requests such as those of process P0.
Page No. 19