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

u2 c3 os sbm-deadlocks

Deadlocks occur in multiprogramming environments when processes compete for limited resources, leading to a situation where each process is waiting for resources held by others. The document outlines the necessary conditions for deadlock, methods for handling it, including prevention and avoidance strategies, and describes the resource-allocation graph used to visualize deadlocks. Additionally, it introduces the Banker's algorithm as a means to avoid deadlocks by ensuring that resource requests do not lead to unsafe states.

Uploaded by

safasiyan3
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)
6 views

u2 c3 os sbm-deadlocks

Deadlocks occur in multiprogramming environments when processes compete for limited resources, leading to a situation where each process is waiting for resources held by others. The document outlines the necessary conditions for deadlock, methods for handling it, including prevention and avoidance strategies, and describes the resource-allocation graph used to visualize deadlocks. Additionally, it introduces the Banker's algorithm as a means to avoid deadlocks by ensuring that resource requests do not lead to unsafe states.

Uploaded by

safasiyan3
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/ 19

UNIT 02: CHAPTER 3- DEADLOCKS

Deadlock
 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 wait state. Waiting processes may never again change
state, because the resources they have requested are held by other waiting
processes. This situation is called a deadlock.
 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.
Deadlock System model

A deadlock occurs when a set of processes is stalled because each process is holding
a resource and waiting for another process to acquire another resource.
In the diagram below, for example, Process 1 is holding Resource 1 while Process
2 acquires Resource 2, and Process 2 is waiting for Resource 1.
 For the purposes of deadlock discussion, a system can be modeled as a collection
of limited resources that can be divided into different categories and allocated
to a variety of processes, each with different requirements.
 Memory, printers, CPUs, open files, tape drives, CD-ROMs, and other resources
are examples of resource categories.
 By definition, all resources within a category are equivalent, and any of the
resources within that category can equally satisfy a request from that category.
If this is not the case (i.e. if there is some difference between the resources
within a category), then that category must be subdivided further. For example,
the term “printers” may need to be subdivided into “laser printers” and “color
inkjet printers.”
 Some categories may only have one resource.
 The kernel keeps track of which resources are free and which are allocated, to
which process they are allocated, and a queue of processes waiting for this
resource to become available for all kernel-managed resources. Mutexes or
wait() and signal() calls can be used to control application-managed resources
(i.e. binary or counting semaphores. )
 When every process in a set is waiting for a resource that is currently assigned
to another process in the set, the set is said to be deadlocked.
Resource allocation sequence:
Under the normal mode of operation, a process may utilize a resource in only the
following sequence:
a) Request (process requests to os to grant resources)
 If the request cannot be granted immediately (for example, the resource is
being used by another process), then the requesting processmust wait until it can
acquire the resource.
b) Use: (os grants the request)
 The process can operate on the resource (for example, if the resourceis a
printer, the process can print on the printer).
c) Release: (after the completion of usage, process release the resources)
 The process releases the resource.
Deadlock Characterization
In a deadlock, processes never finish its execution and system resources are tied
up, preventing other jobs from starting.
1. Necessary Conditions
A deadlock situation can arise if the following four conditions hold
simultaneously in a system:

a) 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

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

c) No preemption:
 Resources cannot be preempted; that is, a resource can be released only
voluntarily by the process holding it, after that process has completed its
task.

a) Circular wait:
 A set {P0, P1, ..., Pn,) of waiting processes must exist 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 that is held by P0.
All four conditions must hold for a deadlock to occur.

2. Resource-Allocation Graph
 Used to describe deadlock
 Deadlocks can be described more precisely in terms of a directed graph
called a system resource-allocation graph.
 This graph consists of a set of vertices V and a set of edges E.
 The set of vertices V is partitioned into two different types of nodes
o P = {PI, P2, ..., Pn}, the set of active processes in the system,
o R = {R1, R2, ..., Rm), the set of resource types in the system.

 We represent each process Pi as a circle, and each resource type Rj as a


square.
 Since resource type Rj may have more than one instance, we represent each
such instance as a dot within the square.
 When process Pi requests an instance of resource type Rj, a request edge is
inserted in the resource-allocation graph.
 When this request can be fulfilled, the request edge is transformed to an
assignment edge.
 When the process releases the resource, the assignment edge is deleted.

Request edge ( Pi  Rj):

 A directed edge Pi Rj signifies that process Pi requested an instance of


resource type Rj and is currently waiting for that resource.
Assignment edge (Rj  Pi)

 A directed edge Rj  Pi signifies that an instance of resource type Rj has


been allocated to process Pi.
The resource-allocation graph shown above depicts the following situation.
1) The sets P, R, and E:

2) Resource instances:
 One instance of resource type R1
 Two instances of resource type R2
 One instance of resource type R3
 Three instances of resource type R4

3) Process states:
 Process P1is 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 R2, and is waiting for an instance
of resource type R3.
 Process P3 is holding an instance of R3.

If the graph contains no cycles, then no process in the system is deadlocked. If the
graph does contain a cycle, then a deadlock may /may not exist.

Resource-allocation graph with a deadlock


Two minimal cycles exist in the system:

Methods for Handling Deadlocks

1. Deadlock prevention
2. Deadlock avoidance

We can deal with the deadlock problem in one of three ways:


 We can use a protocol to prevent or avoid deadlocks, ensuring that the
system will never enter a deadlock state.
 We can allow the system to enter a deadlock state, detect it, and recover.
 We can ignore the problem altogether, and pretend that deadlocks never
occur in the system. This solution is used by most operating systems,
including UNIX.
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.

Mutual Exclusion
 If resources are non-sharable, process has to wait for requested resource.so
mutual exclusion will occur. Ex .Printer

Protocol :
 If resources are in sharable mode, then process never need to wait for
resources. Here mutual exclusion never occur. Ex. Read only Files
 We cannot prevent deadlock by refusing mutual exclusion, because some
resources are non-sharable.
Hold and Wait
(A process requests for R1,R2,R3 and it got R1 , R2 and waiting for R3 )

 To ensure that the hold-and-wait condition never occurs in the system, we


must guarantee that, whenever a process requests a resource, it does not
hold any other resources.

Protocol 1:
(No waiting :-If the process gets all the resources R1,R2,R3,then only process
starts the execution)

 Before a process begins its execution ensure that the process should request
resources and all the requested resources to be allocated.
 Process holds the resources for its entire execution.

Protocol2:
(No holding:-if the process is holding R1,R2 and need R3 to continue execution,
then the process releases R1 &R2,then place a fresh request to get R1,R2,R3
together, then starts the execution)

 It allows a process to request resources only when the process has none.
 Before it can request any additional resources, it must release all the
resources that it is currently allocated.

These protocols have two main disadvantages.

1) Low resource utilization: since many of the resources may be allocated but
unused for a long period.
2) 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

 There be no preemption of resources that have already been allocated.ie


only after the completion of the task, the process release its resources.
 To violate this condition we can use the following protocol.

Protocol 1:
(Process P1 is simply holding R1,R2 and waiting for R3.Here OS takes back
R1,R2 from P1 and add those resources to resource list)

 If a process is holding some resources and waiting for another resource then
all resources currently being held are implicitly released (preempted).
 The preempted resources are added to the list of resources (for which the
process is waiting).
 The process will be restarted only when it can regain its old resources, as
well as the new ones that it is requesting.

Protocol 2:
(Process P1 is holding R1, R2 and waiting for R3.But R3 is held by P2 and P2 is
waiting for R4.Here OS takes back R3 from P2 and gives to P1)

 If a process requests some resources, we first check whether they are


available. If they are, we allocate them.
 If they are not available, we check whether they are allocated to some other
process that is waiting for additional resources. If so, we preempt the
desired resources from the waiting process and allocate them to the
requesting process.
 If the resources are not either available or held by a waiting process, the
requesting process must wait.

Circular Wait

 Aim : Break the chain.


 The fourth and final condition for deadlocks is the circular-wait condition.
 To violate this condition we have to impose (manipulate) a total ordering of
all resource types, and to require that each process requests resources in an
increasing order of enumeration.
Consider the following protocols to prevent deadlocks:
Assign priority to each resource.

Protocol 1:

 Each process can request resources only in an increasing order of


enumeration.
 The process is holding Ri and requesting instances of resource type Rj if and
only if F(Rj) > F(Ri).
Ex :
(Each process requests resources in increasing g order)

Each process requests resources in increasing order. Here P2 can’t request R1,
because P2 is already holding high priority resource R2 [priority of R2 is 5 and
priority of R2 is 3]

Protocol 2:
 Whenever a process requests an instance of resource type Rj, it has released
any resources Ri such that F(Ri) >= F(Rj).
 Process is holding Ri and requested for Rj, here Rj is less prior than Riie
(F(Rj)<=F(Ri)).
 In this case the process can release Ri and request for Rj
Ex:

P2 cannot request R1. If P2


wants R1, then release R2
first and then request for R1
Deadlock avoidance:

it means to avoid the deadlock in the operating system. Deadlock avoidance


ensures that requests for any resource are fulfilled as long as the system’s final
state avoids causing deadlock. The system’s condition will be monitored
continually to identify safe and harmful conditions.

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,
 The future requests
 Releases of each process.

Deadlock Avoidance :( Safe state and Banker’s algorithm)(impt)

Deadlock-avoidance algorithm

 This model requires that each process declare the maximum number of
resources of each type that it may need.
 Priori (early) information about the maximum number of resources ensures
that the system will never enter a deadlock state. This is Deadlock-
avoidance approach.
 This algorithm examines the resource-allocation state to ensure that a
circular wait condition can never exist.
 The resource-allocation state is defined by
a. The number of available and allocated resources,
b. The maximum demands of the processes.

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. Deadlock will never occur in
safe state.
A system is in a safe state only if there exists a safe sequence.

Safe, unsafe, and deadlock state spaces.

 A safe state is not a deadlock state.


 Conversely, a deadlock state is an unsafe state.
 Not all unsafe states are deadlocks
 An unsafe state may lead to a deadlock.
 As long as the state is safe, the operating system can avoid unsafe (and
deadlock) states.
 In an unsafe state, the operating system cannot prevent processes from
requesting resources such that a deadlock occurs: The behavior of the
processes controls unsafe states.

 To illustrate, we consider a system with 12 magnetic tape drives and 3


processes: Po, PI, and P2.
 Process Po requires 10 tape drives, process PI may need as many as 4, and
process P2 may need up to 9 tape drives.
 Suppose that, at time to, process Po is holding 5 tape drives, process P1 is
holding 2, and process P2 is holding 2 tape drives. (Thus, there are 3 free
tape drives.)

Holding Need
P0 10 5
P1 4 2
P2 9 2

 At time t0, the system is in a safe state.


 The sequence <P1, P0, P2> satisfies the safety condition, since process P1
can immediately be allocated all its tape drives and then return them (the
system will then have 5 available tape drives), then process Po can get all its
tape drives and return them (the system will then have 10 available tape
drives), and finally process P2 could get all its tape drives and return them
(the system will then have all 12 tape drives available).
Banker's Algorithm:

 Banker’s algorithm is applicable to a resource allocation system with


multiple instances of each resource type.
 Banker’s algorithm is a deadlock avoidance algorithm. It is named so
because this algorithm is used in banking systems to determine whether a
loan can be granted or not.
 Banker’s algorithm works in a similar way in computers. Whenever a new
process is created, it must exactly specify the maximum instances of each
resource type that it needs.
 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.
 If it will, the resources are allocated; otherwise, the process must wait until
some other process releases enough resources.

Following Data structures are used to implement the Banker’s Algorithm:
Let ‘n’ be the number of processes in the system and ‘m’ be the number of
resources types.
Available :
 It is a 1-d array of size ‘m’ indicating the number of available resources of each
type.
 Available[ j ] = k means there are ‘k’ instances of resource type Rj

Max :
 It is a 2-d array of size ‘n*m’ that defines the maximum demand of each process
in a system.
 Max[i, j ] = k means process Pi may request at most ‘k’ instances of resource
type Rj.

Allocation :
 It is a 2-d array of size ‘n*m’ that defines the number of resources of each type
currently allocated to each process.
 Allocation[ i, j ] = k means process Pi is currently allocated ‘k’ instances of
resource type Rj

Need :
 It is a 2-d array of size ‘n*m’ that indicates the remaining resource need of
each process to complete its task.
 Need [ i, j ] = k means process Pi currently allocated ‘k’ instances of resource
type Rj
 Need [ i, j ] = Max [ i, j ] – Allocation [ i, j ]

Allocationi specifies the resources currently allocated to process Pi and


Needi specifies the additional resources that process Pi may still request to
complete its task.

Note :

Banker’s algorithm consist of Safety algorithm and Resource request algorithm

Safety Algorithm
We claim that the system is currently in a safe state, the sequence<PI, P3, P4, P2,
P0>satisfies the safety criteria.

Need =Max-Allocation

Deadlock Detection
 A process will be suspended from its progress because of 2 reasons: life lock
(waiting to occur some I/O events to occur) and deadlock (waiting for a
resource that is held by another process)
 To detect deadlock in a system we can use :

1) Wait for graph(for single instance resources)


2) Deadlock detection algorithm(for multiple instance resources)

 If a system not providing either a deadlock-prevention or a deadlock


avoidance algorithm, then a deadlock situation may occur. In this
environment, the system must provide:
o An algorithm that examines whether a deadlock has occurred
o An algorithm to recover from the deadlock

a) Single Instance of Each Resource Type( wait- for graph)

 If all resources have only a single instance, then we can define a deadlock
detection algorithm that uses a variant (different form) of the resource-
allocation graph, called a wait-for graph.
 We obtain this graph from the resource-allocation graph by removing the
nodes of type resource and collapsing the appropriate edges.

Predict deadlock ensure deadlock


 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 to invoke an algorithm that searches for a cycle in the graph.
 Here the resource allocation graph predict the dead lock, but wait-for graph
ensure dead lock if cycle occurs.

b) Several Instances of a Resource Type(dead lock detection algorithm)

 The wait-for graph scheme is not applicable to a resource-allocation system


with multiple instances of each resource type.
 The deadlock-detection algorithm that we describe next is applicable to such
a system.
 The algorithm employs several time-varying data structures that are similar
to those used in the banker's algorithm
o Available: A vector of length rn indicates the number of available
resources of each type.
o Allocation: An n x m matrix defines the number of resources of each type
currently allocated to each process.
o Request: An n x m matrix indicates the current request of each process.
If Request [i,j] = k, then process Pi is requesting k more instances of
resource type Rj.

Dead lock detection Algorithm

1. Let Work and Finish be vectors of length m and n, respectively. Initialize Work :=
Available for i= 1, 2,..n,
If Request i=0,then Finish[i] := true, else, Finish[i] :=false ; if Finish[i] := true,
work=work+ allocation

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 + Allocation i
Finish[i] := true
go to step 2.
3. If Finish[i] = false, for some i, 1 <= i<= n, then the system is in a deadlock
state.
Moreover, if Finish[i] =false, then process Pi is deadlocked.

Recovery from Deadlock

 OS will initiate a recovery routine, these routine recover the system from
deadlock.
 When a detection algorithm determines that a deadlock exists, we have to
recover from deadlock.

1. One solution is to inform the operator that a deadlock has occurred, and to
let the operator deal with the deadlock manually.
2. The other possibility is to let the system recover from the deadlock
automatically by following solutions
a)Process termination (Simply to abort one or more processes to break the
circular wait)
b) Resource preemption(Preempt some resources from one or more of the
deadlocked processes.)
a) Process Termination
To eliminate deadlocks by aborting a process, we use one of two methods. In both
methods, the system reclaims all resources allocated to the terminated processes.
1) Abort all deadlocked processes:

 This method clearly will break the deadlock cycle, but at a great expense;
these processes may have computed for a long time, and the results of
these partial computations must be discarded and probably recomputed
later.

2) Abort one process at a time until the deadlock cycle is eliminated:

 In This method, after each process is aborted, a deadlock-detection


algorithm must be invoked to determine whether any processes are still
deadlocked.

b) Resource Preemption

 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:
1. Selecting a victim:
 Which resources and which processes are to be preempted?
2. Rollback:
If we preempt a resource from a process, we must roll back the
process to some safe state, and restart it from that state.
 It is difficult to determine what a safe state is, so abort the process and
then restart it.
3. Starvation:
 We ensure that starvation will not occur?
 We guarantee that resources will not always be preempted from the
same process
 If the same process is always picked as a victim. As a result, this
process never completes its designated task, a starvation occur.
Clearly, we must ensure that a process can be picked as a victim only a (small)
finite number of times

Difference between Starvation and Deadlocks


Aspect Deadlock Starvation

A condition where two or more A condition where a process is


processes are blocked forever, perpetually denied necessary
Definition
each waiting for a resource held by resources, despite resources being
another. available.

Resources are available but are


Resource Resources are held by processes
continuously allocated to other
Availability involved in the deadlock.
processes.

Circular dependency between


Continuous preference or priority
processes, where each process is
Cause given to other processes, causing
waiting for a resource from
a process to wait indefinitely.
another.

Requires intervention, such as Can be mitigated by adjusting


Resolution aborting processes or preempting scheduling policies to ensure fair
resources to break the cycle. resource allocation.

You might also like