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

Deadlock

OS module 3 VTU BCS302

Uploaded by

abhinavhs3124
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Deadlock

OS module 3 VTU BCS302

Uploaded by

abhinavhs3124
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Operating Systems

Deadlocks
During the execution of processes, a process requests for resource, uses it and releases it. A process
may request as many resources it wants to execute its task. Under normal mode of operations, a
process may utilize a resource only in following sequence:

● Request: The process requests for a resource, if the process doesn’t get the resource then
the requesting process must wait till it acquires the resource.
● Use: The process can operate on the resource.
● Release: The process releases the resource

In a multiprogramming environment, several processes may compete for finite number of


resources. A process requests for a resource, if the requested resource is not available then process
enters waiting state. That process will be in waiting state until the resource is allocated. This
situation is called a deadlock.
In a deadlock, processes never finish executing, and system resources are tied up, preventing other
jobs from starting.
Conditions for a deadlock to occur are:
Mutual Exclusion: Atleast 1 resource must be held in a non-sharable mode i.e only 1 process can
use that resource at a time. If another process request that resource then it must delayed in
allocating that resource.
Hold and Wait: A process must be holding one resource and waiting to acquire additional
resources that are currently being held by other processes.
No preemption: Resources cannot be preempted; i.e a resource can only be released voluntarily by
the processes holding it, after that process has completed its task.
Circular Wait: A set {P0, P1, P2, …. Pn}of waiting processes must exist such that P0 is waiting for a
resource held by P1, P1 is waiting for resource held by P2 and so on. Circular wait is updated version
of hold and wait.
If all the above 4 conditions are true then a deadlock may or may not occur.
Resource-Allocation Graph:
Deadlocks can be described more in terms of a directed graph called a system resource allocation
graph. This graph consists of set of vertices v and set of edges E. The set of vertices can be
partitioned into 2 different types of nodes: P = { P1, P2, …. Pn } the set consisting of all active
processes in the system and R = { R1, R2, …. Rm } the set consisting all types of resource types in the
system.
A directed edge from process Pi to resource type Rj is denoted by Pi Rj it signifies that the process Pi
has requested for an instance of resource type Rj and is currently waiting for that resource. This
edge is called as request edge.
A directed edge from resource type Rj to process Pi is denoted by Rj Pi it signifies that the process Pi
has been allocated with an instance of resource type Rj . This edge is called as assignment edge.

1
Operating Systems

Pictorially the processes are represented as circle and resource type as a rectangle. The instances
for each resource type are represented within a rectangle with a dot.

Fig 4.1: Resource allocation graph


Fig 4.1 depicts the resource allocation graph
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}

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 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.

Given the definition of resource allocation graph, if a graph contains no cycles then deadlock
doesn’t exist. If a graph consists a cycle then a deadlock may exist.
Also if a resource type has only 1 instance and cycle exist then a deadlock has occurred. Each
process involved in a cycle is deadlocked.
If each resource type has multiple instances and a cycle exists, then it is not necessary for deadlock
to exist. It may or may not exist.

2
Operating Systems

Fig 4.2 Resource allocation graph with a deadlock


As shown in Fig 4.2, if process P3 requests for a instance of resource type R2. Then since no resource
instance is available, a request edge is added as shown in Fig 4.2. At this point, 2 minimal cycles
exist in the system
P1 R1 P2 R3 P3 R2 P1
P2 R3 P3 R2 P2
Processes P1, P2, P3 are deadlocked. Here P2 is waiting for resource type R3 which is held by P3 and P3
is waiting for a resource type R2 which is not available. In addition, P1 is waiting for a resource type
waiting R1 held by P2.
Consider Fig 4.3, here we have a cycle as P1 R1 P3 R2 P1

Fig 4.3 Resource allocation graph with a cycle but no deadlock


But there is no deadlock, as P4 may release R2, that resource can be allocated to P3 breaking the
cycle.
Hence if resource allocation graph does not have a cycle then there is no deadlock, if there a cycle
then the system may or may not be in deadlock state.
Methods of handling deadlock:
A deadlock problem can be handled in following ways:

3
Operating Systems

● A protocol which prevents or avoid deadlocks, ensuring that a system will never enter a
deadlock.
● To allow a system to enter into a deadlock, detect it and recover.
● Ignore the problem altogether and pretend that the deadlock never occurs in the system.

Most of the operating systems use the third options. It is up to the application developer to write
programs which handle deadlock. To ensure that a deadlock never occurs, the system can either use
deadlock prevention or deadlock avoidance scheme.

● 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 a
request for resources can be made.
● Deadlock Avoidance requires that an operating system be given with advance information of
resources required by the process during its lifetime, so that it can decide for each
requestwhether or not the process should wait. To make this decision, the system must
consider the resources currently available, the resources currently allocated to each process
and the future requests and releases of each process.
● If both deadlock prevention and deadlock avoidance is not employed, then deadlock may
occur. Here, the system can use an algorithm to examine the system for any deadlock
occurrence and another algorithm to recover from deadlock.
● If the above 3 techniques are not used, then the system may arrive at deadlock state where
the system performance deteriorates as the resources are held by processes cannot run and
more processes requests for that resources. Eventually the system will stop functioning and
will need to be restarted manually.
Most of the operating systems use the 4th option as the deadlock occurs infrequently (once a year)
and thus this method is cheaper compared to the rest 3 options.
Deadlock Prevention:
Deadlock prevention provides a set of methods for ensuring that at least one of the necessary
conditions cannot hold.
Mutual Exclusion:

● Mutual Exclusion condition must hold for non-sharable resources. For example, a printer is
a non-sharable resource and it requires mutually exclusive access, as many processes
cannot use printer at the same time.
● Sharable resources cannot be mutually exclusive. Example, Read only files. These read-only
files can be accessed by many processes at same time.
● A process never needs to wait for sharable resources. In general, we cannot prevent
deadlocks by denying the mutual exclusion condition because some resources are
intrinsically non-sharable.
Hold and Wait:
To ensure hold and wait condition to be prevented, whenever a process requests for a resource, it
does not hold any resources.

4
Operating Systems

● One protocol that can be used requires each process to request and be allocated all its
resources at the beginning of execution.
● Another protocol allows a process to request resources only when it has none. A process
may request some resources, use them. However if they require additional resources, it
must release all the resources that are currently allocated.
Both these protocols have some disadvantages,

● Resource utilization may be low, since resources may be allocated and unused for long time.
● Starvation is possible. A process may be in need of some popular resources, it may need to
wait indefinitely to get them.
No preemption:
To ensure that this condition does not hold, the following protocols can be used

● If a process is holding some resource and requests another resource, and if that is not
allotted immediately, then all the resources that are currently allotted to process is
preempted. The preempted resources are added to the list of resources that the process is
waiting. The process is restarted only when it can regain its old resources as well as new
ones that it was requesting.
● If a process requests some resources, it will be checked whether it is available. If so, then it
is allotted. If it is not available, then check is made whether that resource is allotted to some
other process which is waiting for additional resources. If so, the desired resource is
preempted from the waiting process and is allotted to requesting process. If the resource is
neither available nor held by any other waiting process, then the requesting process must
wait. While it is waiting, some of its resources can be preempted only if any other process
requests it.
These protocols can be used on the resources whose state can be easily saved and restored later
such as CPU registers and memory space.
Circular Wait:
One way to ensure that this condition never holds is to impose a total ordering of all resource types
and to require that each process requests resource in an increasing order of enumeration.
Here, let R = {R1, R2,…., Rm} be the set of resource types. Each resource type is assigned with a unique
integer number. The one to one function F:R N, where N is the set of natural numbers.
For example, if R includes tape drives, disk drives and printers then F can be defined as
F (tape drive) = 1
F (disk drive) = 5
F (printers) = 12

● Each process can request resources only in an increasing order of enumeration. That is a
process can request any number of instances of resource type Ri after that the process can
request any number of instances of resource type Rj only if F ( Rj ) > F ( Ri ). In the above eg,

5
Operating Systems

if a process requires tape drives and printer at same time, it must 1st request tape drive and
then request for printer.
● A process requesting an instance of resource type Rj must have released any resources Ri
such that F( Ri ) >= F( Rj ).
If these 2 protocols are used, then circular wait condition cannot hold.
Deadlock Avoidance:
The side effects of deadlock prevention like low device utilization and reduced system throughput,
the deadlock avoidance techniques are used.
Here, the system requires additional information about how resources are to be requested and
allocated. Based on the information, the system will decide whether to allocate the resource directly
or wait until further resources are released. To make this decision, the system considers the
currently available resources, the resources currently allocated to the process and the future
requests and releases of each process.
The various algorithms that use this approach differ in the amount and type of information
required. The simplest algorithm requires the maximum number of resources of each type that it
may need. Based on this, it is possible to construct an algorithm that ensures that the system will
never enter the deadlock state. Such an algorithm defines deadlock avoidance approach. The
deadlock avoidance algorithm dynamically examines the resource allocation state to ensure
circular wait condition does not exist. The resource allocation state is defined by the number of
available and allocated resources and the maximum demands of the processes.
Safe State:
A state is safe if the system can allocate the resources to each process in some order and still the
system will avoid the deadlock. A system is said to be safe state only if there exists a safe sequence.
A sequence of processes < P1, P2, P3,…, Pn> is a safe sequence for the current allocation state if, for
each Pi, the resource request by process Pi, can still be satisfied by currently available resources
plus the resources held by process Pj. If the required resources are not available, then Pi can wait
until all the Pj have finished.When they have finished, Pi can obtain all the required resources and
can finish its task. If no such sequence exists, then the system is said to be in unsafe state.

Fig 4.4: Safe, unsafe and deadlocked state space


A safe state is not deadlocked state. Conversely, a deadlocked state is an unsafe state. Not all unsafe
states are deadlocks as shown in Fig 4.4, however an unsafe state may lead to a deadlock. In an

6
Operating Systems

unsafe state, the operating system cannot prevent processes from requesting resources in such a
way that a deadlock occurs. The behavior of processes controls unsafe states.
Deadlock avoidance algorithm can be divided on the basis if instances,

● A single instance resource type uses Resource allocation graph algorithm


● A multiple instance resource type uses Banker’s algorithm

Resource Allocation Graph Algorithm


A variant of resource allocation graph. Here other than request edge and allocation edge, one more
edge is being used i.e Claimedge.

Fig 4.5: Resource allocation graph for deadlock avoidance


As shown in Fig 4.5, the claim edge Pi→Rjindicates that process Pj may request resource Rj; It is
being 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 edgeResources must be claimed a priori in the
system.
Bankers Algorithm:
A resource allocation graph algorithm works for resource type having only 1 instance. But for
resource type having multiple instance, the deadlocks can be avoided by using another algorithm
called as Banker’s algorithm.
When a new process enters the system, it must declare the maximum number of instances of each
resource type it may need. This number should 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. If it will, the resources are allocated
otherwise the process must wait until some other process releases enough resources.
Several data structures are to be maintained to implement the Banker’s algorithm. A system with n
processes and m resource type must have the following data structures

● Available: a vector of length m indicates the number of resources which are available of each
type. If available[j]=k, then it means that there k instances of resource type j are available for
use.

7
Operating Systems

● Max: A vector of length n x m matrix defines the maximum demand of each process. If
Max[i][j]=k, it means that process Pi may request atmost k instances of resource type Rj.
● Allocation: A vector of length n x m matrix defines the number of resources of each type
currently allocated to each process. If Allocation[i][j]=k, then the process Pi has been allocated
with k instances of resource type Rj.
● Need: A vector of length n x m matrix defines the remaining resource need of each of the
process. If Need[i][j]=k, then process Pi needs k instances of resource type Rj. Need [i][j] = Max
[i] [j] – Allocation [i] [j]
Safety Algorithm:
This algorithm is used for finding out whetehr or not a system is in a safe state.
1. Let Work and Finish be vectors of length m and n, respectively.
Initialize: Work = Available
Finish [i] = false for i = 0, 1, …, n- 1
2. Find an i such that both:
(a) Finish [i] = false
(b) Needi≤Work
3. If no such i exists, go to step 4
Work = Work + Allocationi
Finish[i] = true
go to step 2
4. If Finish [i] == true for all i, then the system is in a safe state
Resource Request Algorithm:
Request = request vector for process Pi. If Requesti[j] = k then process Pi wants k instances of
resource type Rj
1. If Requesti≤Needigo to step 2. Otherwise, raise error condition, since process has exceeded
its maximum claim
2. If Requesti≤Available, go to step 3. Otherwise Pi must wait, since resources are not available
3. Pretend to allocate requested resources to Pi by modifying the state as follows:
Available = Available – Request;
Allocationi= Allocationi + Requesti;
Needi= Needi – Requesti;
If resource allocation state is safe, then transaction is completed and the process Piis allocated with
the resources. If new state is unsafe, then Pi must wait for the requesti and the old resource-
allocation state is restored.
Refer Banker’s algorithm problems done in class
Deadlock Detection:
If a system does not employ either a deadlock prevention or deadlock avoidance algorithm then the
deadlock situation may occur. Here, the system may provide

8
Operating Systems

● An algorithm that examines the state of the system to determine whether a deadlock has
occurred
● An algorithm to recover from the deadlock.

Along this process of detection and recovery, the requires overhead that includes not only the run
time costs of maintaining the necessary information and executing the detection algorithm but also
the potential losses inherent in recovering from a deadlock.
In case there is single instance of each resource type, then a deadlock detection algorithm which a
variant of resource allocation graph is defined called as wait-for graph.
A wait-for graph eliminates the resource nodes and collapses the appropriate edges which is
present in resource allocation graph. In a wait-for graph, an edge from Pi to Pj implies that a process
Pi is waiting for a 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.as shown in Fig 4.6

Fig 4.6 a) Resource allocation graph b) Corresponding wait-for graph


Here, a deadlock exists in a system if and only if the wait-for graph contains a cycle. To detect
deadlocks, the system needs to maintain wait-for graph and periodically invoke an algorithm that
searches for a cycle in a graph.
The wait-for graph is not applicable to resource allocation system with a multiple instances of each
resource type. So an algorithm employs several time varying data structures that are similar to be
used in banker’s algorithm.
Available: A vector of length m indicates the number of available resources of each type.
Allocation: An n x m matrix defines the number of resources of each type currently allocated to each
process.
Request: An n x m matrix indicates the current request of each process. If Request [i] [j] = k, then k
instances of resource type Rj is requested by process Pi
Detection Algorithm
1. Let Work and Finish be vectors of length m and n, respectively
Initialize:
(a) Work = Available
(b) For i = 1,2, …, n, if Allocationi≠0, then
Finish[i] = false; otherwise, Finish[i] = true

9
Operating Systems

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, 1 ≤i≤n, then the system is in deadlock state. Moreover, if
Finish[i] == false, then Pi is deadlocked.
Detection Algorithm Usage

● If deadlocks occur frequently, then detection algorithm should be invoked frequently.


Resources allocated to deadlocked processes will be idle until the deadlock can be broken.
● Deadlocks occur only when some process makes a request that cannot be granted
immediately. This request may be last request that creates a chain of waiting processes.
Here, the detection algorithm can be invoked every time a request for allocation cannot be
granted immediately. In this case, not only the deadlocked set of processes but also specific
processes that caused the deadlock can be identified.
● The above said usage of deadlock detection algorithm will incur a considerable overhead in
computation time. A less expensive alternative is simply to invoke the algorithm at less
frequent intervals. If the detection algorithm is invoked at arbitrary points in time, there
may be many cycles in the resource graph. Here, which of many deadlocked processes
caused the deadlock cannot be identified.
Recovery from deadlock
When a detection algorithm determines that a deadlock exists, several alternatives are available.
One of the possibility is to inform the operator that a deadlock has occurred and to let the operator
to deal with it manually. Another possibility is to let the system recover from the deadlock
automatically.
Process termination
2 methods are used to eliminate deadlocks by aborting a process

● Abort all the deadlocked processes


It will break the deadlock cycle but at great expense that a deadlocked process may have
computed for a long time and the results of these partial computations must be discarded
and probably be recomputed later.
● Abort one process at a time until the deadlock cycle is eliminated
It incurs considerable overhead, since after each process is aborted, a deadlock detection
algorithm must be invoked to determine whether any processes are still deadlocked.
Process can be aborted in the following order

● Priority of the process


● How long process has computed, and how much longer to completion

10
Operating Systems

● Resources the process has used


● Resources process needs to complete
● How many processes will need to be terminated
● Is process interactive or batch?

Resource Preemption
To eliminate deadlocks using resource preemption, the resources from the process are successively
preempted and these preempted resources are given 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:
As in process termination, we must determine the order of preemption to minimize cost.
The parameters which determines the cost are the number of resources held by the
deadlocked process and the amount of time the process thus far consumed during its
execution.
● Rollback:
If a resource is preempted, the process cannot continue its normal execution. So the process
is roll backed to some safe state and restarted from that state only.
Since it is difficult to determine safe state, the solution is total rollback. Abort the process
and restart it. To roll back a process till its necessary to break a deadlock, the system
requires to keep more information about the state of all running processes.
● Starvation:
In a system where victim selection is based on cost factor, same process may be picked as
victim leading that process to be under starvation state. To overcome this, a number of
rollbacks in cost factor is included clearly picking a victim only for finite number of times.

11

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy