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

Module 3- os

The document discusses deadlock in operating systems, detailing its characteristics, conditions for occurrence, and methods for prevention and avoidance. It explains concepts such as resource allocation graphs, safe and unsafe states, and algorithms like the Banker's algorithm for managing resources to prevent deadlock. The document emphasizes the importance of understanding and managing resource allocation to maintain system stability and efficiency.

Uploaded by

Shashank S
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Module 3- os

The document discusses deadlock in operating systems, detailing its characteristics, conditions for occurrence, and methods for prevention and avoidance. It explains concepts such as resource allocation graphs, safe and unsafe states, and algorithms like the Banker's algorithm for managing resources to prevent deadlock. The document emphasizes the importance of understanding and managing resource allocation to maintain system stability and efficiency.

Uploaded by

Shashank S
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 73

Department: Information Science & Engineering

Course Name: OPERATING SYSTEMS (MODULE )


Module3
DEADLOCK:
 System Models,
 Deadlock Characterization,
 Resource Allocation Graph,
 Deadlock Prevention,
 Deadlock Avoidance,
 Deadlock Detection and Recovery,
 Banker’s algorithm.
Dead Lock
• In a multiprogramming environment, several processes may compete
for a finite number of resources.
• A process requests resources; if the resources are not available at that
time, the process enters a waiting state. Sometimes, a waiting process
is never again able to change state, because the resources it has
requested are held by other waiting processes. This situation is called
a deadlock
Deadlocked state- Example
• consider a system with three CD RW drives. Suppose each of three
processes holds one of these CD RW drives. If each process now
requests another drive, the three processes will be in a deadlocked
state. Each is waiting for the event "CD RW is released,“ This example
illustrates a deadlock involving the same resource type.
• Deadlocks may also involve different resource types. For example,
consider a system with one printer and one DVD drive. Suppose that
process P; is holding the DVD and process Pi is holding the printer. If
P; requests the printer and P1 requests the DVD drive, a deadlock
occurs.
System Model
• A system consists of a finite number of resources to be distributed
among a number of competing processes. Memory space, CPU cycles,
files, and I/0 devices (such as printers and DVD drives) are examples
of resource types.
• A process must request a resource before using it and must release
the resource after using it.
• A process may request as many resources as it requires to carry out
its designated task. Obviously, the number of resources requested
may not exceed the total number of resources available in the system.
In other words, a process cannot request three printers if the system
has only two
• Under the normal mode of operation, a process may utilize a resource
in only the following sequence:
• Request. The process requests the resource. If the request cannot be
granted immediately (for example, if the resource is being used by
another process), then the requesting process must wait until it can
acquire the resource.
• Assignment: The OS will assign to the requesting process an instance
of the requested resource, whenever, it is available. Then, the process
comes out of its waiting state
• Use: The process can operate on the resource (for example, if the
resource is a printer, the process can print on the printer).
• Release. The process releases the resource.
Deadlock Characteristics
• A deadlock situation can arise if the following four conditions hold
simultaneously in a system:
• Conditions For Deadlock
• Mutual Exclusion
• Hold and Wait
• No preemption
• Circular wait
• Important Note
• All these 4 conditions must hold simultaneously for the occurrence of
deadlock.
• If any of these conditions fail, then the system can be ensured deadlock
free.
Mutual exclusion
• At least one resource must be held in a non-sharable mode; that is,
only one process at a time can use the resource.
• If another process requests that resource, the requesting process
must be delayed until the resource has been released.
• Printer is an example of a resource that can be used by only one
process at a time.
• Hold and Wait
• A process must be holding at least one resource and waiting to
acquire additional resources that are currently being held by other
processes.
• No pre-emption
• 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.
• Circular wait.
• A set { P0 , Pl, ... , P11 } of waiting processes must exist such that Po is waiting for
a resource held by P1, P1 is waiting for a resource held by P2, ... , Pn-1 is waiting
for a resource held by Pn and Pn is waiting for a resource held by Po.
Quiz 1
Quiz 2
Resource Allocation Graph
• Deadlocks can be described more precisely in terms of a directed graph called a system Resource
Allocation Graph (RAG).
• Components Of RAG
• There are two major components of a Resource Allocation Graph-
• Vertices
• Edges
• Vertices
• There are following types of vertices in a Resource Allocation Graph-
• Process Vertices-
• Process vertices represent the processes.
• They are drawn as a circle by mentioning the name of process inside
the circle.

• Resource Vertices
Resource vertices represent the resources.
•Depending on the number of instances that exists in the
system, resource vertices may be single instance or
multiple instance.
•They are drawn as a rectangle by mentioning the dots
inside the rectangle.
•The number of dots inside the rectangle indicates the
number of instances of that resource existing in the system.
Edges
•How Many Process? Name it
•How Many Resource?.
•How instances of resource R1 ,resource R2?
•Process P1 holds ___ instance of resource ____and is waiting for an instance
of resource ____.
•Process P2 holds ____instance of resource____ and is waiting for an instance
of resource _____.
•Process P3 holds one instance of resource ____ and waiting for _______.
• When process P; requests an instance of resource type Ri, a request
edge is inserted in the resource-allocation graph.
• When this request can be fulfilled, the request edge is
instantaneously transformed to an assignment edge.
• When the process no longer needs access to the resource, it releases
the resource; as a result, the assignment edge is deleted.
• 1.If the graph contains no cycles, then no process in the system is
deadlocked.
• 2. If the graph does contain a cycle, then a deadlock may exist.
• If each resource type has exactly one instance, then a cycle implies
that a deadlock has occurred. If the cycle involves only a set of
resource types, each of which has only a single instance, then a
deadlock has occurred
RAG with Deadlock
• Given resource allocation graph is multi instance with a cycle
contained in it. So, the system may or may not be in a deadlock state.
Suppose that process P3 requests an instance of resource type
R2. Since no resource instance is currently available, a request
edge P3 ---+ R2 is added to the graph
At this point, two minimal cycles exist in the system:

Processes P1, P2, and P3 are deadlocked. Process P2 is waiting


for the resource R3, which is held by process P3. Process P3 is
waiting for either process P1 or process P2 to release resource
R2. In addition, process P1 is waiting for process P2 to release
resource R1.
RAG with out Deadlock
However, there is no deadlock. Observe that process P4 may
release its instance of resource type R2. That resource can then
be allocated to P3, breaking the cycle.

if a resource-allocation graph does not have a cycle, then the


system is not in a deadlocked state.
If there is a cycle, then the system may or may not be in a
deadlocked state.
This observation is important when we deal with the deadlock
problem
quiz
Methods for Handling Deadlock
• Deadlock Handling-
Deadlock Prevention
• If we simulate deadlock with a table which is standing on its four
legs then we can also simulate four legs with the four conditions
which when occurs simultaneously, cause the deadlock
• However, if we break one of the legs of the table then the table will
fall definitely. The same happens with deadlock, if we can be able to
violate one of the four necessary conditions and don't let them occur
together then we can prevent the deadlock.
• Let's see how we can prevent each of the conditions
• 1. Mutual Exclusion
• Some resources are inherently unshareable, for example, Printers. For
unshareable resources, processes require exclusive control of the
resources.
• A mutual exclusion means that unshareable resources cannot be
accessed simultaneously by processes.
• Shared resources do not cause deadlock but some resources can't be
shared among processes, leading to a deadlock.
• For Example: read operation on a file can be done simultaneously by
multiple processes, but write operation cannot. Write operation
requires sequential access, so, some processes have to wait while
another process is doing a write operation.
• It is not possible to eliminate mutual exclusion, as some resources
are inherently non-shareable
• 2. Hold and Wait
Hold and wait is a condition in which a process is holding one resource
while simultaneously waiting for another resource that is being held by
another process. The process cannot continue till it gets all the
required resources.
Deadlock occurs because there can be more than one process which
are holding one resource and waiting for other in the cyclic order
There are two ways to eliminate hold and wait:-
(i) By eliminating wait:
The process specifies the resources it requires in advance so that it does not have to wait for
allocation after execution starts.
For Example: Process1 declares in advance that it requires both Resource1 and Resource2
it requests all resources at the beginning of the process (DVD, disk & printer). It will hold the printer for its
entire execution, even though it needs the printer only at the end.( lead to lower device utilization)
(i) By eliminating hold:
The process has to release all resources it is currently holding before making a new request.
For Example: Process1 has to release Resource2 and Resource3 before making request for Resource1
• Challenges:
 As a process executes instructions one by one, it cannot know about all required resources before
execution.
 Releasing all the resources a process is currently holding is also problematic as they may not be usable
by other processes and are released unnecessarily.
 For example: When Process1 releases both Resource2 and Resource3, Resource3 is released
unnecessarily as it is not required by Process2.
3.No pre-emption
Preemption is temporarily interrupting an executing task and later resuming it.
For example, if process P1 is using a resource and a high-priority process P2
requests for the resource, process P1 is stopped and the resources are allocated to
P2.
• There are two ways to eliminate this condition by preemption:
1.If a process is holding some resources and waiting for other resources, then it
should release all previously held resources and put a new request for the required
resources again. The process can resume once it has all the required resources.
For example: If a process has resources R1, R2, and R3 and it is waiting for
resource R4, then it has to release R1, R2, and R3 and put a new request of all
resources again.
2.if a process P1 is waiting for some resource, and there is another process P2 that
is holding that resource and is blocked waiting for some other resource. Then the
resource is taken from P2 and allocated to P1. This way process P2 is preempted
and it requests again for its required resources to resume the task.
Challenges:
• This is not a good approach at all since if we take a resource away
which is being used by the process then all the work which it has done
till now can become inconsistent.
• Consider a printer is being used by any process. If we take the printer
away from that process and assign it to some other process then all the
data which has been printed can become inconsistent and ineffective
and also the fact that the process can't start printing again from where
it has left which causes performance inefficiency.
4.Circular Wait
• In circular wait, two or more processes wait for resources in a circular order. We can understand
this better by the diagram given below:
To eliminate circular wait, we assign a priority to each
resource. A process can only request resources in increasing
order of priority.
A process can't request for a lesser priority resource

In the example above, process P3 is requesting


resource R1, which has a number lower than
resource R3 which is already allocated to process P3. So
this request is invalid and cannot be made, as R1 is
already allocated to process P1.
Challenges:
•It is difficult to assign a relative priority to resources, as
one resource can be prioritized differently by different
processes.
•For Example: A media player will give a lesser priority to
a printer while a document processor might give it a
higher priority. The priority of resources is different
according to the situation and use case.
• Feasibility of Deadlock Prevention
• Mutual exclusion cannot be
eliminated completely because
some resources are inherently non-
shareable
• Hold and wait cannot be eliminated
as we cannot know in advance
about the required resources to
prevent waiting. It is inefficient to
prevent a hold by releasing all the
resources while requesting a new
one
• Preempting processes can cause
inconsistency and starting the
process over by putting requests for
all resources again is inefficient.
• Eliminating circular wait is the only
practical way to prevent deadlock.
quiz
Deadlock avoidance
• Deadlock is a situation that occurs in the
Operating System when any Process enters a
waiting state because another waiting process is
holding the demanded resource.
• But how can an Operating System avoid
Deadlock?
• The operating system avoids Deadlock by knowing
the maximum resource requirements of the
processes and the free resources available at
that time
• Operating system tries to allocate the resources
according to the process requirements and checks
if the allocation can lead to a safe state or an
unsafe state.
• If the resource allocation leads to an unsafe state,
then the Operating System does not proceed
further with the allocation sequence.
Safe State and Unsafe State
• Safe State
• A state is safe if the system can allocate resources to each process (up to its
maximum) in some order and still avoid a deadlock. a system is in a safe state
only if there exists a safe sequence
• So safe state does not lead to Deadlock.

Unsafe State - If the Operating System is


not able to prevent Processes from
requesting resources which can also lead
to a Deadlock, then the System is said to
be in an Unsafe State.
If no safe sequence exists, then the system
stateState
Unsafe is saiddoes
to benot
unsafe.
necessarily cause
deadlock it may or may not cause deadlock.
• To illustrate, we consider a system with twelve magnetic tape drives
and three processes: Po, P1, and P2. Process Po requires ten tape
drives, process P1 may need as many as four tape drives, and process
P2 may need up to nine tape drives. Suppose that, at time to, process
Po is holding five tape drives, process P1 is holding two tape drives,
and process P2 is holding two tape drives. (Thus, there are three free
tape drives.)
At time t0, the system is in a safe state.
The sequence < p1,p0,p2> satisfies the safety condition. Process P1 can
immediately be allocated all its tape drives and then return them (the system
will then have five available tape drives); then process Po can get all its tape
drives and return them (the system will then have ten available tape drives); and
finally process P2 can get all its tape drives and return them (the system will then
have all twelve tape drives available)
Deadlock Avoidance Algorithm
Dead lock Avoidance

Single Instance
Multiple Instance
( Resource Allocation
Graph Algorithm) (Bankers Algorithm)
Resource-Allocation-Graph Algorithm
• In addition to the request and assignment edges
already described(refer resource Allocation graph
topic), we introduce a new type of edge, called a
claim edge
• A claim edge Pi ->Rj indicates that process Pi may
request resource Rj at some time in the future
• This edge resembles a request edge in direction
but is represented in the graph by a dashed line.
• When process P1 requests resource R2, the claim
edge P1-> R2 is converted to a request edge.
Similarly, when a resource R2 is released by P1,
the assignment edge R2-> P1; is reconverted to a
claim edge p1->R2
• If no cycle exists, then the allocation of the resource will leave the
system in a safe state. If a cycle is found, then the allocation will put
the system in an unsafe state.
• Suppose that P2 requests R2 . Although R2 is currently free, we
cannot allocate it to P2, since this action will create a cycle in the
graph
Banker's Algorithm
• Bankers algorithm in Operating System is used to avoid deadlock and
for resource allocation safely to each process in the system.
• As the name suggests, it is mainly used in the banking system to
check whether the loan can be sanctioned to a person or not.
• Bankers algorithm in OS is a combination of two main algorithms:
(i) safety algorithm (to check whether the system is in a safe state or
not)
(ii) resource request algorithm (to check how the system behaves
when a process makes a resource request).
Safety Algorithm
• A safety algorithm is an algorithm used to find whether or not a
system is in its safe state. The algorithm is as follows
• Steps in Safety Algorithm are:
Bankers Algorithm in OS- To find safe sequence
solution
Example 2

Calculate need matrix and identify the possible safe sequence


Home work problem
Bankers Algorithm – Resource request algorithm
• Resource request algorithm - Resource request algorithm checks if
the request can be safely granted or not to the process requesting
for resources.
• If a process Pi request for c instances for resource of type j then it can
be represented as Request[i,j]=c.
• Steps in Resource request algorithm :
If the above resource allocation is safe then the transaction is completed and Pi is allocated its
resources for execution. But, if the new state of the system is unsafe then the process Pi has to
wait for the resources to fulfil its request demands and the old allocation state is again restored.
Example
Suppose now that process P1 requests one additional instance of resource type A and two instances of
resource type C, so Request1 = (1,0,2). To decide whether this request can be immediately granted,
we first check that
(i) Request1 <= need -that is, that (1,0,2) <= (1,2,2), which is true.
(ii) Request1 <= Available-that is, that (1,0,2) <= (3,3,2), which is true.

(iii)

NEW Allocation Table (After Request)


OLD Allocation table (Before Request)
Solution
Deadlock Detection
• If a system does not employ either
a deadlock-prevention or a
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
Single Instance of Each Resource Type
• if all resources have only a single instance, then we can define a
deadlock detection algorithm that uses a variant of the resource-
allocation graph, called a wait-for graph.
• We obtain this graph from the resource-allocation graph by removing
the resource nodes and collapsing the appropriate edges.
• More precisely, an edge from Pi to Pj in a wait-for graph implies that
process Pi is waiting for process Pj to release a resource that Pi;
needs. An edge Pi -> Pj exists in a wait-for graph if and only if the
corresponding resource allocation graph contains two edges Pi -> R4
and R4->Pj for some resource R4
a deadlock exists in the system if and only if the wait-for graph contains a cycle
. To detect deadlocks, the system needs to maintain the wait-for graph and periodically invoke an
algorithm that searches for a cycle in the graph.
Example – Draw Wait for graph and detect
deadlock
Multiple Instances of a Resource Type
• The wait-for graph scheme is not applicable to a resource-allocation
system with multiple instances of each resource type
• This algorithm mainly uses several time-varying data structures that
are similar to those used in Banker's Algorithm and these are as
follows
Deadlock Recovery
“Recovery from Deadlock in Operating Systems” refers to
the set of techniques and algorithms designed to detect,
resolve, or mitigate deadlock situations.
Process Termination
• To eliminate the deadlock, we can simply kill one or more processes.
For this, we use two methods:
• Abort all the Deadlocked Processes: Aborting all the processes will
certainly break the deadlock but at a great expense. The deadlocked
processes may have been computed for a long time, and the result of
those partial computations must be discarded and there is a
probability of recalculating them later.
• Abort one process at a time until the deadlock is eliminated: Abort
one deadlocked process at a time, until the deadlock cycle is
eliminated from the system. Due to this method, there may be
considerable overhead, because, after aborting each process, we
have to run a deadlock detection algorithm, to check whether any
processes are still deadlocked.
• Advantages of Process Termination
• It is a simple method for breaking a deadlock.
• It ensures that the deadlock will be resolved quickly, as all processes
involved in the deadlock are terminated simultaneously.
• It frees up resources that were being used by the deadlocked process
making those resources available for other processes.
• Disadvantages of Process Termination
• It can result in the loss of data and other resources that were being
used by the terminated processes.
• It may result in a waste of resources, as the terminated processes may
have already completed a significant amount of work before being
terminated.
Resource Pre-emption
To eliminate deadlocks using resource preemption, we successively preempt some
resources from processes and give these resources to other processes until the
deadlock cycle is broken. If preemption is required to deal with deadlocks, then three
issues need to be addressed
Selecting a victim: We must determine which resources and which processes are to
be preempted and also in order to minimize the cost.
Rollback. If we preempt a resource from a process, what should be done with that
process? Clearly, it cannot continue with its normal execution . it is missing some
needed resource. We must roll back the process to some safe state and restart it from
that state.
Starvation
In a system, it may happen that the same process is always picked as a victim. As a
result, that process will never complete its designated task. This situation is
called starvation and must be avoided. One solution is that a process must be picked
as a victim only a finite number of times.

You might also like