Vous êtes sur la page 1sur 25

11/22/2018

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

 For multiple instances of a resource type, use the


Banker’s Algorithm

7
RESOURCE-ALLOCATION GRAPH SCHEME

11/22/2018
 Introduce a new kind of edge called a claim edge

 Claim edge Pi Rj indicates that process Pj may

A. Manoj
request resource Rj. .

 A claim edge converts to a request edge when a


process requests a resource. .

 A request edge converts to an assignment edge when


the resource is allocated to the process.

 When a resource is released by a process, an


assignment edge reconverts to a claim edge.
8
RESOURCE-ALLOCATION GRAPH WITH
CLAIM EDGES

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

The request can be granted only if converting the

A. Manoj

request edge to an assignment edge does not


result in the formation of a cycle in the resource
allocation graph

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

 When a process requests a resource, it may have


to wait.
 When a process gets all its resources, it must
return them in a finite amount of time

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.

 A detection – and – recovery scheme requires


various kinds of overhead
 Run-time costs of maintaining necessary information
and executing the detection algorithm
 Potential losses inherent in recovering from a
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 .

 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 invoke an algorithm
that searches for a cycle in the graph. 17
RESOURCE-ALLOCATION GRAPH AND WAIT-FOR
GRAPH

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?

 If deadlocks occur frequently, then the algorithm should be invoked


frequently.
 Deadlocks only occur when some process makes a request which
cannot be granted
 Extreme: invoke the algorithm every time a request is denied
 Alternative: invoke the algorithm at less frequent time intervals:
 Once per hour
 Whenever CPU utilization < 40%
20
Disadvantage: cannot determine exactly which process 'caused' the deadlock
11/22/2018
A. Manoj
RECOVERY FROM DEADLOCK
21
RECOVERY FROM DEADLOCK

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

Abort one process at a time until the deadlock cycle is

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

 Many factors may affect which process is chosen for


termination
 What is the priority of the process?
 How long has the process run so far and how much longer will the
process need to run before completing its task?
 How many and what type of resources has the process used?
 How many more resources does the process need in order to finish its
task?
 How many processes will need to be terminated?
 Is the process interactive or batch? 23
RECOVERY FROM DEADLOCK: RESOURCE
PREEMPTION

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?

 Rollback – If we preempt a resource from a process, what


should be done with that process?

 Starvation – How do we ensure that starvation will not


occur? That is, how can we guarantee that resources will
not always be preempted from the same process? 24
11/22/2018 A. Manoj
END OF CHAPTER
25

Vous aimerez peut-être aussi