Documente Academic
Documente Profesional
Documente Cultură
A. Manoj
DEADLOCK AVOIDANCE
1
DEADLOCK AVOIDANCE
11/22/2018
For avoiding deadlocks is to require additional
information about how resources are to be
A. Manoj
requested.
For example, in a system with one tape drive and one
printer, the system might need to know that process P
will request first the tape drive and then the printer
before releasing both resources, whereas process Q will
request first the printer and then the tape drive.
With this knowledge of the complete sequence of
requests and releases for each process, the system can
decide for each request whether or not the process
should wait in order to avoid a possible future deadlock.
2
DEADLOCK AVOIDANCE
The deadlock-avoidance algorithm dynamically
examines the resource-allocation state to ensure
that there can never be a circular-wait condition.
The system's resource-allocation state is defined
by the number of available and allocated
resources, and the maximum demands of the
processes.
When a process requests an available resource,
the system must decide if immediate allocation
leaves the system in a safe state.
SAFE STATE
11/22/2018
A system is in a safe state only if there exists a safe
sequence.
A sequence of processes <P1, P2, …, Pn> is a safe
A. Manoj
sequence for the current allocation state.
If, for each Pi, the resource requests that Pi can still make,
can be satisfied by currently available resources plus
resources held by all Pj, with j < i.
That is:
If the Pi resource needs are not immediately available,
then Pi can wait until all Pj have finished
When Pj is finished, Pi can obtain needed resources,
execute, return allocated resources, and terminate.
When Pi terminates, Pi+1 can obtain its needed resources, 4
and so on
SAFE STATE
11/22/2018
A. Manoj
No Possibility
Avoidance
Deadlocks Of Deadlock
• If a system • If a system • Ensure
is in safe is in unsafe that a
state state system will
never enter
an unsafe
state
5
SAFE, UNSAFE , DEADLOCK STATE
11/22/2018
A. Manoj
6
AVOIDANCE ALGORITHMS
11/22/2018
Two deadlock avoidance algorithms:
For a single instance of a resource type, use a
A. Manoj
Resource-allocation Graph
7
RESOURCE-ALLOCATION GRAPH SCHEME
11/22/2018
Introduce a new kind of edge called a claim edge
A. Manoj
request resource Rj. .
11/22/2018
Assignment
A. Manoj
Request
edge
edge
Claim
Claim edge
edge
9
UNSAFE STATE IN RESOURCE-ALLOCATION GRAPH
11/22/2018
Assignment
A. Manoj
Request
edge
edge
Assignment
Claim edge
edge
10
RESOURCE-ALLOCATION GRAPH
ALGORITHM
11/22/2018
Suppose that process Pi requests a resource Rj
A. Manoj
11
BANKER’S ALGORITHM
11/22/2018
Used when there exists multiple instances of a
resource type.
A. Manoj
Each process must a priori claim maximum use
12
DATA STRUCTURES FOR THE BANKER’S
ALGORITHM
11/22/2018
Let n = number of processes, and m = number of
resources types.
Available: Vector of length m. If available [j] = k,
A. Manoj
there are k instances of resource type Rj available.
The number of units of R currently unallocated.
Max: n X m matrix. If Max [i,j] = k, then process Pi
may request at most k instances of resource type Rj.
Describes the maximum demands of each process.
Allocation: n X m matrix. If Allocation[i,j] = k then
Pi is currently allocated k instances of Rj.
Describes the current allocation status.
Need: n X m matrix. If Need[i,j] = k, then Pi may
need k more instances of Rj to complete its task.
Describes the remaining possible need.
13
Need [i,j] = Max[i,j] – Allocation [i,j]
DATA STRUCTURES FOR THE BANKER’S
ALGORITHM
11/22/2018
Let n = number of processes, and m = number of
resources types.
Available: Vector of length m. If available [j] = k,
A. Manoj
there are k instances of resource type Rj available.
The number of units of R currently unallocated.
Max: n X m matrix. If Max [i,j] = k, then process Pi
may request at most k instances of resource type Rj.
Describes the maximum demands of each process.
Allocation: n X m matrix. If Allocation[i,j] = k then
Pi is currently allocated k instances of Rj.
Describes the current allocation status.
Need: n X m matrix. If Need[i,j] = k, then Pi may
need k more instances of Rj to complete its task.
Describes the remaining possible need.
14
Need [i,j] = Max[i,j] – Allocation [i,j]
11/22/2018
A. Manoj
DEADLOCK DETECTION
15
DEADLOCK DETECTION
11/22/2018
For deadlock detection, the system must provide
An algorithm that examines the state of the system
to detect whether a deadlock has occurred.
A. Manoj
And an algorithm to recover from the deadlock.
16
SINGLE INSTANCE OF EACH RESOURCE
TYPE
11/22/2018
If all resources have only a single instance, then we
can define a deadlock detection algorithm that uses a
alternative of the resource-allocation graph, called a
A. Manoj
wait-for graph.
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 .
11/22/2018
A. Manoj
18
Resource-Allocation Graph Corresponding wait-for graph
MULTIPLE INSTANCES OF A RESOURCE
TYPE
11/22/2018
Use an algorithm similar to Banker's, which simply
investigates every possible allocation sequence for
the processes which remain to be completed.
A. Manoj
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 [ij] = k, then
process Pi is requesting k more instances of
19
resource type. Rj.
DETECTION-ALGORITHM USAGE
11/22/2018
When should we invoke the detection algorithm? The answer depends
on two factors:
How often is a deadlock likely to occur?
A. Manoj
How many processes will be affected by deadlock when it happens?
11/22/2018
Two Approaches
Process termination
A. Manoj
Resource preemption
22
RECOVERY FROM DEADLOCK: PROCESS
TERMINATION
11/22/2018
Abort all deadlocked processes
This approach will break the deadlock, but at great expense
A. Manoj
eliminated
This approach incurs considerable overhead, since, after each process
is aborted, a deadlock-detection algorithm must be re-invoked to
determine whether any processes are still deadlocked
11/22/2018
With this approach, we successively preempt some
resources from processes and give these resources to
other processes until the deadlock cycle is broken
A. Manoj
When preemption is required to deal with deadlocks,
then three issues need to be addressed:
Selecting a victim – Which resources and which
processes are to be preempted?