DEADLOCKS SOCSAMS e-learning Dept. of Computer Applications, MES College Marampally

advertisement
SOCSAMS e-learning
DEADLOCKS
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Introduction
In a computer system, we have a finite number of
resources to be distributed among a number of
competing processes.
System resources are classified into:
• Physical Resources
• Logical Resources
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Introduction continue…
In a computer system, we have a finite number of
resources to be distributed among a number of
competing processes.
System resources are classified into:
• Physical Resources
• Printers
• Tape drivers
• Memory
space
• CPU cycles
• Logical Resources
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Introduction continue…
In a computer system, we have a finite number of
resources to be distributed among a number of
competing processes.
System resources are classified into:
• Physical Resources
• Logical Resources
• Files
• Semaphores
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Introduction continue…
A process must request a resource before using it and
release the resource after using it.
The number of resources requested can not exceed the
total number of resources available in the system.
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Introduction continue…
In a normal operation, a process may utilize a resource
in the following sequence:
• Request the resource
• Use the resource
• Release the resource
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Introduction continue…
In a normal operation, a process may utilize a resource
in the following sequence:
• Request
• Use
If the request can not be immediately
granted, then the requesting process
must wait until it can get the
resource.
• Release
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Introduction continue…
In a normal operation, a process may utilize a resource
in the following sequence:
• Request
• Use
• Release
The requesting process can operate
on the resource.
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Introduction continue…
In a normal operation, a process may utilize a resource
in the following sequence:
• Request
• Use
• Release
The process releases the resource
after using it.
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Introduction continue…
The OS is responsible for making sure that the requesting
process has been allocated the resource. Request and release
of resources can be accomplished through the wait and
signal operations on semaphores. A system table records
whether each resource is free or allocated, and if allocated,
to which process. If a process requests a resource that is
currently allocated to another process, it can be added to a
queue of processes waiting for this resource.
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Deadlock Situation
In
a
multi-programming
environment,
several
processes may compete for a fixed number of
resources. A process requests resources and if the
resources are not available at that time, it 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
deadlock.
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Deadlock Situation
P1
Printer
P2
Scanner
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Deadlock Situation
Consider a system with one printer and one scanner.
Process P1 request the printer and process P2 requests
the scanner. Both requests are granted. Now P1
requests the scanner (without giving up the printer)
and P2 requests the printer (without giving up the
scanner). Neither request can be granted so both
processes enter a deadlock situation.
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Deadlock Situation
P1
Printer
P2
Scanner
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Deadlock - Definition
A deadlock is a situation where a group of processes is
permanently blocked as a result of each process having
acquired a set of resources needed for its completion and
having to wait for release of the remaining resources held
by others thus making it impossible for any of the
deadlocked processes to proceed. Deadlocks can occur in
concurrent environments as a result of the uncontrolled granting
of the system resources to the requesting processes.
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Deadlock - Definition
A set of process is in a deadlock state if each process in the set
is waiting for an event that can be caused by only another
process in the set.
Each member of the set of deadlock processes is waiting for a
resource that can be released only by a deadlock process.
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Deadlock Characterization
 Necessary Conditions
A deadlock situation can arise if the following four conditions
hold simultaneously in a system.
• Mutual Exclusion
• Hold and Wait
• No Preemption
• Circular Wait
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Deadlock Characterization
 Necessary Conditions
Continue…
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.
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Deadlock Characterization
 Necessary Conditions
Continue…
Hold and Wait
A process must be holding at least one resource and waiting
to acquire additional resources that are currently held by
other processes.
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Deadlock Characterization
 Necessary Conditions
Continue…
No Preemption
Resources already allocated to a process cannot be
preempted; that is, a resource can be released only
voluntarily by the process holding it, after process has
completed its task.
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Deadlock Characterization
 Necessary Conditions
Continue…
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.
The processes in the system form a circular list or chain where each process
in the list is waiting for a resource held by the next process in the list.
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Deadlock Situation
P1
P2
P1
R1
i
i
Holding a
resource
Requesting
a resource
R1
R2
i
i
R2
P2
Deadlock
situation
Graphic representation of Resource Allocation
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Resource-Allocation Graph
Resource-allocation graph is used to described the
deadlock.
This graph consists of a set of vertices V and a set of edges
E. The set vertices V is partitioned into two 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 of all resource types in the system.
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Resource-Allocation Graph continue…
A directed edge from resource type Rj to process Pi is
denoted by Rj gPi ; it signifies that an instance of resource
type Rj has been allocated to process Pi .
A directed edge Pi gRj is called a request edge and
a directed edge Rj gPi is called an assignment edge.
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Resource-Allocation Graph continue…
Process node is represented by circles.
P1
Resource node is represented by rectangles.
R1
For each instances of a resource type, there is a dot in the
resource node rectangle.
i
i
R2
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Resource-Allocation Graph continue…
A request edge points to only the square , where as an
assignment edge must also designate one of the dots in the
square.
R1
i
P1
R2
i
P2
i
i
R3
P3
i
i
i
R4
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Resource-Allocation Graph continue…
When process Pi requests an instance of resource type Rj, a
request edge is inserted in the resource allocation graph.
R1
i
P1
R2
i
P2
i
i
R3
P3
i
i
i
R4
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Resource-Allocation Graph continue…
When this requests can be fulfilled the request edge is
instantaneously transformed to an assignment edge.
R1
i
P1
R2
i
P2
i
i
R3
P3
i
i
i
R4
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Resource-Allocation Graph continue…
When the process no longer needs access to the resource it
releases the resource, and as a result the assignment edge is
deleted.
R1
i
P1
R2
i
P2
i
i
R3
P3
i
i
i
R4
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Resource-Allocation Graph continue…
The resource-allocation graph depicts the following:
R1
i
P1
i
P2
R3
P3
The sets P, R, and E
• P = {P1, P2, …, Pn}
• R = {R1, R2, …, Rm}
R2
i
i
i
i
i
R4
• E = {P1 gR1, P2 gR3, R1 gP2, R2 gP2, R2 gP1, R3 gP3}
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Resource-Allocation Graph continue…
The resource-allocation graph depicts the following:
R1
Resource instances
i
P1
i
P2
R3
P3
• One instance of resource type R1
• Two instances of resource type R2
• One instance of resource type R3
R2
i
i
i
i
i
R4
• Three instances of resource type R4
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Resource-Allocation Graph continue…
The resource-allocation graph depicts the following:
Process states
Process P1 is holding an
instance of resource type R2 ,
and is waiting for an
instance of resource type R1 .
R1
i
P1
R2
i
P2
i
i
R3
P3
i
i
i
R4
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Resource-Allocation Graph continue…
The resource-allocation graph depicts the following:
Process states
Process P1 is holding an
instance of resource type R2,
Process
P2 is holding
and
is waiting
for anan
instance
resource
types
instance
of of
resource
type
R1. R1
and R2, and is waiting for an
instance of resource type R3 .
R1
i
P1
R2
i
P2
i
i
R3
P3
i
i
i
R4
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Resource-Allocation Graph continue…
The resource-allocation graph depicts the following:
Process states
Process P1 is holding an
instance of resource type R2,
Process
P2 is holding
and
is waiting
for anan
instance
resource
types
instance
of of
resource
type
R1. R1
and R2, and is waiting for an
Processof resource
P3 is holding
instance
type R3 .an
instance of resource type R3 .
R1
i
P1
R2
i
P2
i
i
R3
P3
i
i
i
R4
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Resource-Allocation Graph continue…
Tips to check deadlock:
R1
If no cycle exists in the
resource allocation graph,
there is no deadlock.
i
P1
R2
i
P2
i
i
R3
P3
i
i
i
R4
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Resource-Allocation Graph continue…
Tips to check deadlock:
R1
If there is a cycle in the
graph and each resource
has only one instance, then
there is a deadlock. Here a
cycle is a necessary and
sufficient condition for
deadlock.
i
P1
R2
i
P2
i
R3
P3
i
i
i
R4
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Resource-Allocation Graph continue…
Tips to check deadlock:
If there is a cycle in the graph and each resource has more
than one instance, there may or may not be a deadlock. A
cycle may be broken if some process outside the cycle has
a resource instance that can break the cycle. Therefore, a
cycle in the resource allocation graph is a necessary but
not sufficient condition for deadlock, when multiple
resource instances are considered.
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Resource-Allocation Graph continue…
Tips to check deadlock:
If there is a cycle in the graph and each
resource has more than one instance,
there may or may not be a deadlock. A
cycle may be broken if some process
outside the cycle has a resource instance
that can break the cycle. Therefore, a
cycle in the resource allocation graph is
a necessary but not sufficient condition
for deadlock, when multiple resource
instances are considered.
R1
i
i
P1
R2
P2
P3
i
i
P4
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Handling Deadlocks
Possible strategies to deal with deadlocks:
 Deadlock Prevention
 Deadlock Avoidance
 Deadlock Detection and Recovery
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Handling Deadlocks continue…
Possible strategies to deal with deadlocks:
 Deadlock Prevention
Is a set methods for ensuring
that at least one of the necessary
 Deadlock Avoidance
conditions cannot hold. These
methods prevent deadlocks by
 Deadlock Detection and Recovery
constraining how requests for
resources can be made.
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Handling Deadlocks continue…
Possible strategies to deal with deadlocks:
 Deadlock Prevention
DA requires that the operating
system be given in advance
 Deadlock Avoidance
additional information concerning
which resources a process will
 Deadlock Detection and Recovery
request and use during its lifetime.
With this knowledge, we can
decide for each request whether or
not the process should wait.
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Handling Deadlocks continue…
Possible strategies to deal with deadlocks:
 Deadlock Prevention
 Deadlock Avoidance
 Deadlock Detection and Recovery
Here the system can
provide an algorithm
that examines the
state of the system to
determine whether a
deadlock
has
occurred
and
an
algorithm to recover
from the deadlock.
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Deadlock Prevention
We can prevent the occurrence of a deadlock by ensuring
that at least one of the four necessary conditions can not
hold.
 Mutual Exclusion
 Hold and Wait
 No Preemption
 Circular Wait
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Deadlock Prevention continue…
Mutual Exclusion:
Shareable resources do not require mutually exclusive
access, and thus cannot be involved in a deadlock.
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Deadlock Prevention continue…
Hold and Wait:
When a process requests a resource, it does not hold any
other resources. Two protocols:
 Requires each process to request and be allocated all its
resources before it begins execution.
 Allows a process to request resources only when the
process has none.
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Deadlock Prevention continue…
Hold and Wait:
A process may request some
resources and use them.
When a process requests a resource,
it does not hold any
However it must release all
other resources. Two protocols:
the resources that is currently
allocated before it can
request
additional
 Requires each process to request
and beany
allocated
all its
resources.
resources before it begins execution.
 Allows a process to request resources only when the
process has none.
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Deadlock Prevention continue…
No Preemption:
If a process is holding some resources and requests another
resource that cannot be immediately allocated to it, then
all resources currently being held are preempted. The
process will be restarted only when it can regain its old
resources, as well as the new ones that is requesting.
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Deadlock Prevention continue…
No Preemption:
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. While it is waiting, some of its resources
may be preempted, but only if another process request them. A process
can be restarted only when it is allocated the new resources it is
requesting and recovers any resources that were preempted while it was
waiting.
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Deadlock Prevention continue…
Circular Wait:
One way to ensure that circular wait never holds is to
impose a total ordering of all resource types, and to
require that each process requests resources in an
increasing order of enumeration.
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Deadlock Prevention continue…
Circular Wait:
Each process can request resources only in an increasing
order of enumeration. That is, a process can initially
request any number of instances of a resource type, say
Ri. After that, the process can request instances of
resource type Rj if and onlyif F(Rj) > F(Ri).
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Deadlock Prevention continue…
Circular Wait:
Alternatively, whenever a process requests an instance of
resource type Rj, it has released any resources Ri such
that F(Ri) >= F(Rj).
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Deadlock Prevention continue…
Deadlock prevention algorithms prevent deadlocks by
restraining how request can be made. This ensures that at
least one of the necessary conditions for deadlock cannot
occur. Possible side effects of preventing deadlocks by this
methods are low utilization and reduced system throughput.
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Deadlock Avoidance
Deadlock-Allocation requires that the operating system be
given in advance additional information concerning which
resources a process will request and use during its lifetime.
With this knowledge, we can decide for each request
whether or not the process should wait.
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Deadlock Avoidance
Deadlock-Allocation requires that the operating system be given in
advance additional information concerning which resources a process
will request and use during its lifetime. With this knowledge, we can
decide for each request whether or not the process should wait.
Given a priori information about the maximum number of resources of
each type that may be requested for each process, it is possible to
construct an algorithm that ensures the deadlock-avoidance approach.
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Deadlock Avoidance
Deadlock-Allocation requires that the operating system be given in
advance additional information concerning which resources a process
will request and use during its lifetime. With this knowledge, we can
decide for each request whether or not the process should wait.
Given a priori information about the maximum number of resources of
each type that may be requested for each process, it is possible to
construct an algorithm that ensures the deadlock-avoidance approach.
A deadlock-avoidance algorithm dynamically examines the resourceallocation state to ensure that a circular wait condition can never exist.
The resource-allocation state is defined by the number of available and
allocated resources, and the maximum demands of the processes.
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Deadlock Avoidance continue…
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.
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Deadlock Avoidance continue…
Safe State
A sequence of processes <P1, P2, …, Pn> is a safe sequence
for the current allocation state if, for each Pi, the resources
that Pi can still request can be satisfied by the currently
available resources plus the resources held by all the Pj,
with j < i.
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Deadlock Avoidance continue…
Safe State
In this situation the resources that process Pi needs are not
immediately available, then Pi can wait until all Pj have
finished. When they have finished, Pi can obtain all of its
needed resources, complete its designated task, return its
allocated resources and terminate. When Pi terminates,
Pi+1 can obtain its needed resources and so on. If no
sequence exists, then the system state is said to be unsafe.
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Deadlock Avoidance continue…
Safe State
Consider a system with 12 magnetic tape drives and 3
processes, P0, P1 and P2.
Maximum need
To
P0
10
5
P1
4
2
P2
9
2
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Deadlock Avoidance continue…
Safe State
Consider a system with 12 magnetic tape drives and 3
processes, P0, P1 and P2.
Maximum need
To
P0
10
5
P1
4
2
P2
9
2
At time To, the
system is in a
safe state
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Deadlock Avoidance continue…
Safe State
Now there are 3 free tape drives.
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 then will have 5 available
tape drives), then process P0 can get all its tape drives and
return them(then available – 10 tape drives), and finally
process P2 could get all its tape drives.
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Deadlock Avoidance continue…
Safe State
A system may go from a safe state to an unsafe state.
Suppose that at time T1, process P2 requests and is
allocated 1 more tape drive. Then the system is no longer in
a safe state, because at this point only process P1 can be
allocated all its tape drives and process P0 and P2 have to
wait – resulting in a deadlock.
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Deadlock Avoidance continue…
Safe State
Avoidance algorithms ensure that the system will always
remain in a safe state. Initially, the system is in a safe state.
Whenever a process requests a resource that is currently
available, the system must decide whether the resource can
be allocated immediately or whether the process must wait.
The request is granted only if the allocation leaves the
system in safe state.
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Deadlock Avoidance continue…
Safe State
Avoidance algorithms ensure that the system will always
remain in a safe state. Initially, the system is in a safe state.
Whenever a process requests a resource that is currently
available, the system must decide whether the resource can
be allocated immediately or whether the process must wait.
The request is granted only if the allocation leaves the
system in safe state.
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Deadlock Avoidance continue…
Safe State
In this scheme, if a process requests a resource that is
currently available, it may still have to wait. Thus, resource
utilization may be lower than it would be without
deadlock-avoidance algorithm.
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Deadlock Avoidance continue…
Resource-Allocation Graph Algorithm
o Claim edge
i
o Assignment edge
i
o Request edge
i
This graph has three types of edges:
A claim edge Pi gRj indicates that the process Pi may
request resource Rj at some time in the future.
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Deadlock Avoidance continue…
Resource-Allocation Graph Algorithm
When process Pi request resource Rj, the claim edge Pi gRj
is converted to a request edge. When the resource Rj is
released by Pi, the assignment edge Rj gPi is converted to a
claim edge Pi gRj.
Before process Pi starts executing, all its claim edges must
appear in the resource-allocation graph.
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Deadlock Avoidance continue…
Resource-Allocation Graph Algorithm
Suppose, process Pi requests resource Rj. The request can be
granted only if converting the request edge Pi gRj to an
assignment edge Rj gPi does not result in the formation of a
cycle in the resource-allocation graph.
Safety is provided by the use of cycle detection algorithm.
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Deadlock Avoidance continue…
Resource-Allocation Graph Algorithm
R1
If P2 requests R2, we
cannot allocate it to P2 even
if R2 is currently free, since
this action will create a
cycle in the graph.
i
P1
R2
P2
i
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Deadlock Avoidance continue…
Resource-Allocation Graph Algorithm
R1
If
P2
R2,
requests
we
cannot
allocate it to P2
even
i
if
R2
currently
is
i
P1
P1
P2
P2
free,
since this action
will create a cycle
in the graph.
R1
R2
i
R2
i
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Banker’s Algorithm
Resource-Allocation Graph Algorithm is not applicable to a
resource allocation system with multiple instances of each
resource type.
Banker’s Algorithm deals multiple instances of each
resource type.
This algorithm could be used in a banking system to ensure
that the bank never allocates its available cash such that it
can no longer satisfy the needs of all its customers.
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Banker’s Algorithm continue…
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
resources in the system.
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Banker’s Algorithm continue…
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.
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Banker’s Algorithm continue…
To implement Banker’s algorithm, we need the following
data structures:
 Available
 Max
 Allocation
 Need
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Banker’s Algorithm continue…
To implement Banker’s algorithm, we need the following
data structures:
 Available
 Max
 Allocation
A vector of length m indicates the
number of available resources of each
type. If Available[j] = k, there are k
instances of resource type Rj available.
 Need
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Banker’s Algorithm continue…
To implement Banker’s algorithm, we need the following
data structures:
 Available
 Max
 Allocation
An n x m matrix defines the maximum
demand of each process. If max[i,j] =
k, then process Pi may request at most
k instances of resource type Rj.
 Need
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Banker’s Algorithm continue…
To implement Banker’s algorithm, we need the following
data structures:
 Available
 Max
 Allocation
 Need
An n x m matrix defines the number of
resources of each type currently
allocated to each process. If
Allocation[i,j] = k, then process Pi is
currently allocated k instances of
resource type Rj.
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Banker’s Algorithm continue…
To implement Banker’s algorithm, we need the following
data structures:
 Available
 Max
 Allocation
 Need
An n x m matrix indicates the
remaining resource need of each
process. If Need[i, j] = k, then process
Pi may need k more instances of
resource type Rj to complete its tasks.
Need[i,j] = Max[i,j] – Allocation[i,j].
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Banker’s Algorithm continue…
We can treat each row in the matrices Allocation and Need
as vectors and refer to them as Allocationi and Needi. The
vector Allocationi specifies the resources currently allocated
to process Pi; the vector Needi specifies the additional
resources that process Pi may still request to complete its
task.
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Banker’s Algorithm continue…
Resource-Request Algorithm:
Let Requesti be the request vector for process Pi. If
Requesti[j] = k, then
process Pi wants k instances of
resource type Rj. When a request for resources is made by
process Pi, the following actions are taken:
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Banker’s Algorithm continue…
Resource-Request Algorithm: continue…
1.
If Requesti ≤ Needi, go to step 2. Otherwise, raise an error
condition, since the process has exceeded it maximum claim.
2.
If Requesti ≤ Available, go to step 3. Otherwise, Pi must wait,
since
the resources are not available.
3.
Pretend to have allocated the requested resources to process Pi by
modifying the state as follows:
• Available := Available – Requesti;
• Allocationi := Allocationi + Requesti;
• Needi := Needi – Requesti;
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Banker’s Algorithm continue…
Resource-Request Algorithm: continue…
If the resulting resource-allocation state is safe, the
transaction is completed and process Pi is allocated its
resources. However, if the new state is unsafe, then Pi must
wait for Requesti and the old resource-allocation state is
restored.
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Banker’s Algorithm continue…
Safety Algorithm:
1.
2.
Let Work and Finish be vectors of length m and n respectively.
Work := Available and Finish[i] := false for i=1,2, …, n.
Initialize
Find an i such that both
•
Finish[i] = false
•
Needi ≤ 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] = true for all i, then the system is an a safe state.
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Deadlock Detection
If a system does not employ either a deadlock-prevention or
a deadlock avoidance algorithm, then it should provide:
•
An algorithm that examines the state of the system to
determine whether a deadlock has occurred.
•
An algorithm to recover from the deadlock.
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Deadlock Detection continue…
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.
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Deadlock Detection continue…
Single Instance of Each Resource Type
(a) Resource-allocation graph (b) Corresponding wait-for graph
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Deadlock Detection continue…
Single Instance of Each Resource Type
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 gPj exists in a wait-for graph if and only if the
corresponding resource-allocation graph contains two edges
Pi gRq and Rq gPj for some resource Rq.
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Deadlock Detection continue…
Single Instance of Each Resource Type
If there exists a cycle in wait-for graph, there is a deadlock
in the system, and the processes forming the part of cycle
are blocked in the deadlock. To take appropriate action to
recover from this situation, an algorithm needs to be called
periodically to detect existence of cycle in wait-for graph.
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Deadlock Detection continue…
Multiple Instances of a Resource Type
When multiple instances of a resource type exist, the waitfor graph becomes inefficient to detect the deadlock in the
system.
An algorithm which uses certain data structures similar to
ones used in Banker’s algorithm is applied.
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Deadlock Detection continue…
Multiple Instances of a Resource Type
The following data structures are used:
 Available Resources
 Current Allocation
 Request
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Deadlock Detection continue…
Multiple Instances of a Resource Type
The following data structures are used:
 Available Resources, A: A vector of size q stores
information about the number of available resources of
each type.
 Current Allocation
 Request
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Deadlock Detection continue…
Multiple Instances of a Resource Type
The following data structures are used:
 Available Resources
 Current Allocation, C: A matrix of order pxq stores
information about the number of resources of each type
allocated to each process. C[i][j] indicates the number of
resources of type j currently held by the process i.
 Request
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Deadlock Detection continue…
Multiple Instances of a Resource Type
The following data structures are used:
 Available Resources
 Current Allocation
 Request, R: A matrix of order pxq stores information
about the number of resources of each type currently
requested by each process. R[i][j] indicates the number of
resources of type j currently requested by the process i.
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Deadlock Detection continue…
Multiple Instances of a Resource Type
Consider a vector Complete of size p.
Steps to detect the deadlock:
1.
Initialize Complete[i]=False for all i=1,2,3, …., p. Complete[i]=False
indicates that the ith process is still not completed.
2.
Search for an i, such that Complete[i]=False and (R<=A), that is ,
resources currently requested by this process is less than the
available resources. If no such process exists, then go to step 4.
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Deadlock Detection continue…
Multiple Instances of a Resource Type
3.
Allocate the required resources and let the process finish its
execution and set Complete[i]=True for that process. Go to Step 2.
4.
If Complete[i]=False for some i, then the system is in the state of
deadlock and the ith process is deadlocked.
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Deadlock Recovery
When a detection algorithm determines that a deadlock
exists, then:
• let the operator deal with the deadlock manually.
• let the system recover from the deadlock automatically.
There are two options for breaking a deadlock:
• to abort one or more processes to break the circular wait.
• to preempt some resources from one or more of the
deadlocked processes.
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Deadlock Recovery continue…
Process Termination:
There are two methods that can be used for terminating the
processes to recover from the deadlock:
•
Terminating one process at a time until the circular-wait
condition is eliminated.
•
Terminating all processes involved in the deadlock.
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Deadlock Recovery continue…
involves an over head of invoking a deadlock
Process ItTermination:
detection algorithm after terminating each process to
detectmethods
whether circular-wait
condition
is eliminated
There are two
that can be
used for
terminating the
or recover
not, that is,from
whether
processes are still
processes to
theany
deadlock:
deadlocked.
•
Terminating one process at a time until the circular-wait
condition is eliminated.
•
Terminating all processes involved in the deadlock.
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Deadlock Recovery continue…
Process Termination:
There are two methods that can be used for terminating the
processes toThis
recover
the deadlock:
methodfrom
will definitely
ensure the recovery of a
•
system from the deadlock. The disadvantage of this
method is that many processes may have executed
for a long time; close to their completion. As a
Terminating
one process at a time until the circular-wait
result, the computations performed till the time of
condition
is eliminated.
termination
are discarded.
•
Terminating all processes involved in the deadlock.
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Deadlock Recovery continue…
Resource Preemption:
An alternate method to recover system from the state of
deadlock is to preempt the resources from the processes one
by one and allocate them to other processes until the
circular-wait condition is eliminated.
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Deadlock Recovery continue…
Resource Preemption:
Steps involved in the preemption of resources from processes are:
1. Select a process for preemption.
2. Roll back of the process.
3. Prevent starvation.
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Deadlock Recovery continue…
Resource Preemption:
The choice of resources and processes must be such
Steps involved
in they
the preemption
of resources
from
processes are:
that
incur minimum
cost to the
system.
1. Select a process for preemption.
2. Roll back of the process.
3. Prevent starvation.
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Deadlock
Recovery
continue…
After preempting the
resources, the corresponding
process must be rolled backed properly so that it
does not leave the system in an inconsistent state.
Process must be brought to some safe state from
Steps involvedwhere
in theitpreemption
of resources
are:
can be restarted
later. Infrom
case processes
no such safe
state can be achieved, the process must be totally
rolled back. Partial rollback is preferred.
Resource Preemption:
1. Select a process for preemption.
2. Roll back of the process.
3. Prevent starvation.
Dept. of Computer Applications, MES College Marampally
SOCSAMS e-learning
Deadlock Recovery continue…
Resource Preemption:
Steps involved in the preemption of resources from processes are:
In case of the selection of a process is based on the
cost factor, it is quite possible that same process is
selected repeatedly for the rollback leading to the
situation of starvation. This can be avoided by
including the number of rollbacks of a given process
in the cost
2. Roll back
of factor.
the process.
1. Select a process for preemption.
3. Prevent starvation.
Dept. of Computer Applications, MES College Marampally
Download