u2 c3 os sbm-deadlocks
u2 c3 os sbm-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
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.
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.
1. Deadlock prevention
2. Deadlock avoidance
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 )
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.
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
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)
Circular Wait
Protocol 1:
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:
To decide whether the current request can be satisfied or must be delayed, the
system must consider :
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.
Holding Need
P0 10 5
P1 4 2
P2 9 2
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 ]
Note :
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 :
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.
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
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.
b) Resource Preemption