OS UNIT II Part 2(Chp 7 Deadlock)
OS UNIT II Part 2(Chp 7 Deadlock)
• Deadlock Characterization
• Methods for Handling Deadlocks
• Deadlock Prevention
• Deadlock Avoidance
• Deadlock Detection
• Recovery from Deadlock
• A process in operating system uses resources in the following way.
1) Requests a resource
2) Use the resource
3) Releases the resource
• Deadlock is a situation where a set of processes are blocked because each
process is holding a resource and waiting for another resource acquired by some
other process.
Consider an example when two trains are coming toward each other on the same
track and there is only one track, none of the trains can move once they are in
front of each other.
• A similar situation occurs in operating systems when there are two or more
processes that hold some resources and wait for resources held by other(s).
• For example, in the below diagram, Process 1 is holding Resource 1 and waiting
for resource 2 which is acquired by process 2, and process 2 is waiting for
resource 1.
• Deadlock can arise if the following four conditions hold simultaneously in a System
(Necessary Conditions)
Mutual Exclusion:
• At least one resource must be held in a non-shareable mode, i.e., only one process at a
time can use the resource. P1 -> R1 P2->R2
• If another process requests that resource, the requesting process must be delayed until
the resource has been released.
Hold and Wait: A process is holding at least one resource and waiting for resources.
• It must guarantee that whenever a process requests a resource, it does not hold any
other resources
• Require process to request and be allocated all its resources before it begins execution,
or allow process to request resources only when the process has none allocated to it.
• Low resource utilization; starvation possible
No Preemption: A resource cannot be taken from a process unless the process
releases the resource.
• 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.
• Process will be restarted only when it can regain its old resources, as well as the
new ones that it is requesting
Circular Wait
• A set of processes are waiting for each other in circular form.
• There exists a set {P0 , P1 , …, P n } 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 , …, P
n–1 is waiting for a resource that is held by P n , and P n is waiting for a resource
that is held by P0 .
Resource-Allocation Graph
• Deadlocks can be described in terms of a directed graph called a System resource
allocation graph.
• This graph consists of a set of vertices V and set of edges E.
• V is partitioned into two types:
• P = {P1 , P2 , …, P n }, the set consisting of all the processes in the system
• R = {R1 , R2 , …, R m }, the set consisting of all resource types in the system
• A directed edge from process Pi to resource type Rj is denoted by Pi --> Rj, it
signifies that process Pi has requested an instance of resource type Rj and is
currently waiting for that resource.
• A directed edge from resource type Rj to process Pi is denoted by Rj --> Pi, it
signifies that resource type Rj has been allocated to process Pi.
• Request edge – directed edge Pi → Rj
• Assignment edge – directed edge Rj → Pi
• A set of vertices V and a set of edges
• When process Pi requests an instance of resources type Rj, 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 resources, it releases the resource;
as a result, the assignment edge is deleted.
• The resource-allocation graph depicts the following situation.
• The sets P,R and E:
• P={P1,P2,P3}
• R={R1,R2,R3,R4}
• E={P1→R1,P2→R3, R1→P2,R2→P2,R2→P1,R3→P3}
• Resources instances:
• One instance of resource type R1
• Two instance of resource type R2
• One instance of resources type R3
• Three instances of resources type R4
• Process States:
• Process P1 is holding an instance of resource type R2 and is waiting for an
instance of resource type R1.
• Process P2 is holding an instance of r1 and an instance of R2 and is waiting for an
instance of R3.
• Process P3 is holding an instance of R3.
• NOTE:
• If graph contains no cycles ⇒ no deadlock
● If graph contains a cycle ⇒
● if only one instance per resource type, then deadlock
● if several instances per resource type, possibility of deadlock
• Differences between Deadlock and Starvation:
1. Deadlock is a situation where no process got Starvation is a situation where the low priority process
blocked and no process proceeds got blocked and the high priority processes proceed.
4 The requested resource is blocked by the other The requested resource is continuously be used by the
process. higher priority processes.
5 Deadlock happens when Mutual exclusion, It occurs due to the uncontrolled priority and resource
hold and wait, No preemption and circular wait management.
occurs simultaneously.
Methods for Handling Deadlocks
• Ensure that the system will never enter a deadlock state:
● Deadlock prevention
● Deadlock avoidance
• Deadlock prevention provides a set of methods for ensuring that at least one of
the necessary conditions cannot hold. These methods prevent deadlocks by
constraining how requests for resources can be made.
• Deadlock Avoidance requires that the operating system be given to advance
additional information concerning which resources a process will request and use
during its life time.
• To decide whether the current request can be satisfied or must be delayed, the
system must consider the resources currently available, the resources currently
allocated to each process, and future requests and releases of each process.
Deadlock Prevention in Operating System
• As we are already familiar with all the necessary conditions for a deadlock. In
brief, the conditions are as follows:
• Mutual Exclusion
• Hold and Wait
• No Preemption
• Circular Wait
Mutual Exclusion
• This condition must hold for non-sharable resources.
• For example, a printer cannot be simultaneously shared by several processes.
• In contrast, Sharable resources do not require mutually exclusive access and thus
cannot be involved in a deadlock.
• A good example of a sharable resource is Read-only files because if several
processes attempt to open a read-only file at the same time, then they can be
granted simultaneous access to the file.
• A process need not to wait for the sharable resource.
• Generally, deadlocks cannot be prevented by denying the mutual exclusion
condition because there are some resources that are intrinsically non-sharable.
Hold and Wait
• Hold and wait condition occurs when a process holds a resource and is also
waiting for some other resource in order to complete its execution.
• Thus if we did not want the occurrence of this condition then we must guarantee
that when a process requests a resource, it does not hold any other resource.
• There are some protocols that can be used in order to ensure that the Hold and
Wait condition never occurs:
• According to the first protocol; Each process must request and gets all its
resources before the beginning of its execution.
• The second protocol allows a process to request resources only when it does not
occupy any resource.
• The difference between these two protocols:
• We will consider a process that mainly copies data from a DVD drive to a file
on disk, sorts the file, and then prints the results to a printer.
• If all the resources must be requested at the beginning of the process according
to the first protocol, then the process requests the DVD drive, disk file, and
printer initially.
• It will hold the printer during its entire execution, even though the printer is
needed only at the end.
• While the second method allows the process to request initially only the DVD
drive and disk file. It copies the data from the DVD drive to the disk and then
releases both the DVD drive and the disk file. The process must then again request
the disk file and printer. After copying the disk file to the printer, the process
releases these two resources as well and then terminates.
Disadvantages
• Utilization of resources may be low, since resources may be allocated but unused
for a long period. In the above-given example, for instance, we can release the
DVD drive and disk file and again request the disk file and printer only if we can
be sure that our data will remain on the disk file. Otherwise, we must request all
the resources at the beginning of both protocols.
• There is a possibility of starvation. A process that needs several popular resources
may have to wait indefinitely because at least one of the resources that it needs is
always allocated to some other process.
No Preemption
• The third necessary condition for deadlocks is that there should be no preemption
of resources that have already been allocated. In order to ensure that this condition
does not hold the following protocols can be used :
• According to the First Protocol: "If a process that is already holding some
resources requests another resource and if the requested resources cannot be
allocated to it, then it must release all the resources currently allocated to it."
• According to the Second Protocol: "When a process requests some resources, if
they are available, then allocate them. If in case the requested resource is not
available then we will check whether it is being used or is allocated to some other
process waiting for other resources. If that resource is not being used, then the
operating system preempts it from the waiting process and allocate it to the
requesting process. And if that resource is being used, then the requesting process
must wait".
• The second protocol can be applied to those resources whose state can be easily
saved and restored later for example CPU registers and memory space, and cannot
be applied to resources like printers and tape drivers.
Circular Wait
• The Fourth necessary condition to cause deadlock is circular wait, In order to
ensure violate this condition we can do the following:
• Assign a priority number to each resource. There will be a condition that any
process cannot request for a lesser priority resource. This method ensures that not
a single process can request a resource that is being utilized by any other process
and due to which no cycle will be formed.
• Example: Assume that R5 resource is allocated to P1, if next time P1 asks for R4,
R3 that are lesser than R5; then such request will not be granted. Only the request
for resources that are more than R5 will be granted.
Practical
S.No Necessary Conditions Approach
Implementation
The approach used
1 Mutual Exclusion to violate this Not possible
condition is spooling.
In order to violate
this condition, the
approach is to
2 Hold and wait Not possible
request all the
resources for a
process initially
In order to violate
this condition, the
3 No Preemption approach is: snatch Not possible
all the resources
from the process.
In this approach is to
assign priority to
4 Circular Wait each resource and possible
order them
numerically
Deadlock Avoidance in Operating System
• The deadlock Avoidance method is used by the operating system in order to check
whether the system is in a safe state or in an unsafe state and in order to avoid the
deadlocks, the process must need to tell the operating system about the maximum
number of resources a process can request in order to complete its execution.
How does Deadlock Avoidance work?
• In this method, the request for any resource will be granted only if the resulting
state of the system doesn't cause any deadlock in the system.
• This method checks every step performed by the operating system. Any process
continues its execution until the system is in a safe state.
• Once the system enters into an unsafe state, the operating system has to take a step
back.
• With the help of a deadlock-avoidance algorithm, you can dynamically assess the
resource-allocation state so that there can never be a circular-wait situation.
• According to the simplest and useful approach, any process should declare the
maximum number of resources of each type it will need.
• The algorithms of deadlock avoidance mainly examine the resource allocations so
that there can never be an occurrence of circular wait conditions.
• Deadlock avoidance can mainly be done with the help of Banker's Algorithm.
Safe State and Unsafe State
• A state is safe if the system can allocate resources to each process( up to its
maximum requirement) in some order and still avoid a deadlock.
• Formally, a system is in a safe state only, if there exists a safe sequence. So a safe
state is not a deadlocked state and conversely a deadlocked state is an unsafe state.
• In an Unsafe state, the operating system cannot prevent processes from requesting
resources in such a way that any deadlock occurs.
• It is not necessary that all unsafe states are deadlocks; an unsafe state may lead to
a deadlock.
Deadlock Avoidance Example
• A system having 12 magnetic tapes and three processes P1, P2, P3.
• Process P1 requires 10 magnetic tapes, process P2 may need as many as 4 tapes,
process P3 may need up to 9 tapes.
• Suppose at a time to, process P1 is holding 5 tapes, process P2 is holding 2 tapes
and process P3 is holding 2 tapes. (There are 3 free magnetic tapes)
• So at time t0, the system is in a safe state.
• The sequence is <P2,P1,P3> satisfies the safety condition.
• Process P2 can immediately be allocated all its tape drives and then return them.
• After the return the system will have 5 available tapes, then process P1 can get all
its tapes and return them ( the system will then have 10 tapes);
• finally, process P3 can get all its tapes and return them (The system will then have
12 available tapes).
• A system can go from a safe state to an unsafe state.
• Suppose at time t1, process P3 requests and is allocated one more tape.
• The system is no longer in a safe state. At this point, only process P2 can be
allocated all its tapes.
• When it returns them the system will then have only 4 available tapes.
• Since P1 is allocated five tapes but has a maximum of ten so it may request 5 more
tapes.
• If it does so, it will have to wait because they are unavailable.
• Similarly, process P3 may request its additional 6 tapes and have to wait which
then results in a deadlock.
• The mistake was granting the request from P3 for one more tape.
• If we made P3 wait until either of the other processes had finished and released its
resources, then we could have avoided the deadlock.
• Note: In a case, if the system is unable to fulfill the request of all processes then
the state of the system is called unsafe.
• The main key of the deadlock avoidance method is whenever the request is made
for resources then the request must only be approved only in the case if
the resulting state is a safe state.
Resource-Allocation Graph Scheme
• Claim edge Pi → Rj indicated that process Pj may request resource Rj; represented
by a dashed line
• Claim edge converts to request edge when a process requests a resource
• Request edge converted to an assignment edge when the resource is allocated to
the process
• When a resource is released by a process, assignment edge reconverts to a claim
edge
• Resources must be claimed a priori in the system
Safe state Unsafe State:
• Multiple instances
• When a process gets all its resources it must return them in a finite amount of time
• When a new process enters the system, it must declare the maximum number of
instances of each resource type that it may need.
• This number may not exceed the total number of resources in the system.
• When a user requests a set of resources, the system must determine whether the
allocation of these resources will leave the system in a safe state.
• Several data structures must be maintained to implement the banker’s algorithm.
These data structures encode the state of the resource-allocation system.
• Let n = number of processes, and m = number of resources types
• Available: Vector of length m. If available [j] = k, there are k instances of
resource type Rj available
Need
ABC
P0 743
P1 122
P2 600
P3 011
P4 431
• The system is in a safe state since the sequence < P1, P3, P4, P2, P0> satisfies
safety criteria
Example: P1 Request (1,0,2)
• Check that Request Available (that is, (1,0,2) (3,3,2) true
Allocation Need Available
ABC ABC ABC
P0 0 1 0 743 230
P1 302 020
P2 3 0 2 600
P3 2 1 1 011
P4 0 0 2 431
• Executing safety algorithm shows that sequence < P1, P3, P4, P0, P2> satisfies
safety requirement
• Can request for (3,3,0) by P4 be granted?
• Can request for (0,2,0) by P0 be granted?
• 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 situation. the system must provide:
• An algorithm that examines the state of the system to determine whether a deadlock has
occurred.
• An algorithm to recover from the deadlock.
• These two requirements as they pertain to systems with only a single instance of each resource
type, as well as to systems with several instances of each resource type.
1. 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 resourc
nodes and collapsing the appropriate edges.
• 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 → Rq and Rq → Pj for some
resource Rq.
• 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.
• An algorithm to detect a cycle in a graph requires an order of n2 operations, where
n is the number of vertices in the graph.
• 2. Several 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.
• The algorithm employs several time-varying data structures that are similar to
those used in the bankers algorithm.
• Available –
A vector of length m indicates the number of available resources of each type.
• Allocation –
An n*m matrix defines the number of resources of each type currently allocated
to a process. Column represents resource and rows represent process.
• Request –
An n*m matrix indicates the current request of each process. If request[i][j]
equals k then process P i is requesting k more instances of resource type Rj.
• Bankers algorithm includes a Safety Algorithm / Deadlock Detection Algorithm
The algorithm for finding out whether a system is in a safe state can be described
as follows:
Steps of Algorithm:
1. Let Work and Finish be vectors of length m and n respectively. Initialize Work=
Available. For i=0, 1, …., n-1, if Requesti = 0, then Finish[i] = true;
otherwise, Finish[i]= false.
2. Find an index i such that both
a) Finish[i] == false
b) Requesti <= Work
If no such i exists go to step 4.
3. Work= Work+ Allocationi
Finish[i]= true
Go to Step 2.
4. If Finish[i]== false for some i, 0<=i<n, then the system is in a deadlocked state.
Moreover, if Finish[i]==false the process Pi is deadlocked.
• In this, Work = [0, 0, 0] &
Finish = [false, false, false, false, false]
• Since Finish is a vector of all true it means there is no deadlock in this example.
Recovery from Deadlock: Process Termination
• When a Deadlock Detection Algorithm determines that a deadlock has occurred in
the system, the system must recover from that deadlock. There are two approaches
of breaking a Deadlock:
• 1. Process Termination:
To eliminate the deadlock, we can simply kill one or more processes. For this, we
use two methods:
(a). Abort all the Deadlocked Processes:
Aborting all the processes will certainly break the deadlock, but with a great
expense. The deadlocked processes may have computed for a long time and the
result of those partial computations must be discarded and there is a probability to
recalculate them later.
(b). Abort one process at a time until deadlock is eliminated:
Abort one deadlocked process at a time, until 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 deadlock detection algorithm to check
whether any processes are still deadlocked. alculate them later.
• 2. Resource Preemption:
To eliminate deadlocks using resource preemption, we preempt some resources
from processes and give those resources to other processes. This method will raise
three issues –
(a). Selecting a victim:
We must determine which resources and which processes are to be preempted and
also the order to minimize the cost.
(b). Rollback:
We must determine what should be done with the process from which resources are
preempted. One simple idea is total rollback. That means abort the process and
restart it.
(c). Starvation:
In a system, it may happen that 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.
• Domain of Protection :
• The protection policies limit the access of each process with respect to their
resource handling. A process is bound to use only those resources which it requires
to complete its task, in the time limit that it requires and also the mode in which it
is required. That is the protected domain of a process.
• A computer system has processes and objects, which are treated as abstract data
types, and these objects have operations specific to them. A domain element is
described as <object, {set of operations on object}>.
• Each domain consists of a set of objects and the operations that can be performed
on them. A domain can consist of either only a process or a procedure or a user.
Then, if a domain corresponds to a procedure, then changing domain would mean
changing procedure ID. Objects may share a common operation or two. Then the
domains overlap.
Access Matrix
• Access Matrix is a security model of protection state in computer system. It is
represented as a matrix. Access matrix is used to define the rights of each process
executing in the domain with respect to each object.
• The rows of matrix represent domains and columns represent objects. Each cell of
matrix represents set of access rights which are given to the processes of domain
means each entry(i, j) defines the set of operations that a process executing in
domain Di can invoke on object Oj.
• According to the above matrix: there are four domains and four objects- three
files(F1, F2, F3) and one printer.
• A process executing in D1 can read files F1 and F3. A process executing in
domain D4 has same rights as D1 but it can also write on files.
• Printer can be accessed by only one process executing in domain D2. The
mechanism of access matrix consists of many policies and semantic properties.
• Specifically, We must ensure that a process executing in domain Di can access
only those
• Policies of access matrix concerning protection involve which rights should be
included in the (i, j)th entry.
• We must also decide the domain in which each process executes. This policy is
usually decided by the operating system.
• The Users decide the contents of the access-matrix entries. objects that are
specified in row i.