Chapter 7: Deadlocks - PowerPoint PPT Presentation

1 / 56
About This Presentation
Title:

Chapter 7: Deadlocks

Description:

... occurs, it can be resolved if one car backs up (preempt resources and rollback) ... (think of the Dining Philosophers each holding one chopstick) ... – PowerPoint PPT presentation

Number of Views:68
Avg rating:3.0/5.0
Slides: 57
Provided by: marily210
Category:

less

Transcript and Presenter's Notes

Title: Chapter 7: Deadlocks


1
Chapter 7 Deadlocks
2
Chapter 7 Deadlocks
  • The Deadlock Problem
  • System Model
  • Deadlock Characterization
  • Methods for Handling Deadlocks
  • Deadlock Prevention
  • Deadlock Avoidance
  • Deadlock Detection
  • Recovery from Deadlock

3
Chapter Objectives
  • To develop a description of deadlocks, which
    prevent sets of concurrent processes from
    completing their tasks
  • To present a number of different methods for
    preventing or avoiding deadlocks in a computer
    system.

4
The Deadlock Problem
  • A set of blocked processes each holding a
    resource and waiting to acquire a resource held
    by another process in the set.
  • Example
  • System has 2 tape drives.
  • P1 and P2 each hold one tape drive and each needs
    another one.
  • Example
  • semaphores A and B, initialized to 1
  • P0 P1
  • wait (A) wait(B)
  • wait (B) wait(A)

5
Bridge Crossing Example
  • Traffic possible in only one direction in center
    of bridge.
  • Each section of a bridge can be viewed as a
    resource.
  • If a deadlock occurs, it can be resolved if one
    car backs up (preempt resources and rollback).
  • Several cars may have to be backed up if a
    deadlock occurs.
  • Starvation is possible.

6
System Model
  • A system is made up of resources (hard and soft)
  • e.g., CPU cycles, memory space, I/O devices
  • Each resource type has a finite number of
    instances
  • e.g., there are a finite number of printers
    on the system, there is only so much memory,
    there is only one copy of shared data, etc.
  • Each process utilizes a resource as follows
  • Requests resource
  • Uses resource (in Critical Section)
  • Releases resource
  • Conflict / deadlock is possible, just like on
    bridge

7
Deadlock Characterization
Deadlock can arise if four conditions exist
simultaneously.
  • Mutual exclusion only one process at a time can
    use a resource (e.g., a printer, tape drive, DB
    file/record).
  • Hold and wait a process that is holding at
    least one resource is waiting to acquire
    additional resources held by other processes
    (think of 2 processes wanting to copy tapes).
  • No preemption a resource can be released only
    voluntarily by the process holding it, after that
    process has completed its task (e.g., cannot be
    interrupted when only half done printing a job).
  • Circular wait there exists a set P0, P1, ,
    Pn of waiting processes such that P0 is waiting
    for a resource that is held by P1, P1 is waiting
    for a resource that is held by P2, , Pn1 is
    waiting for a resource that is held by Pn, and Pn
    is waiting for a resource that is held by P0.
  • (think of the Dining Philosophers each
    holding one chopstick)

8
Example of Deadlock Situation
Mutual Exclusion, Hold and Wait, No Preemption,
Circular Wait
9
Methods for Handling Deadlocks
  • Ensure that the system will never enter a
    deadlock state.
  • Prevention algorithms
  • Avoidance algorithms
  • Allow the system to enter a deadlock state and
    then recover.
  • Detection algorithms
  • Recovery algorithms
  • Ignore the problem and pretend that deadlocks
    never occur in the system
  • Used by most operating systems, including UNIX.
  • Not as bad an option as it seems, provided the
    user / administrator can find a way to recover
    when occur

10
Deadlock Prevention
To prevent deadlocks, must insure that one of the
four required conditions cannot exist Must
control / constrain the ways requests can be made.
  • Mutual Exclusion not required for shareable
    resources but must be required for non shareable
    resources.
  • So, cannot always prevent, since some resources
    are by their nature non-shareable (e.g.,
    printers)
  • Hold and Wait must guarantee that whenever a
    process requests a resource, it does not hold any
    other resources.
  • Require a process to request and be allocated all
    its resources before it begins execution, or
    allow a process to request resources only when
    the process owns none.
  • Problem Low resource utilization possible
    starvation.
  • Also, sometimes processes must hold multiple
    resources in order to be able to do their work

11
Deadlock Prevention (Cont.)
  • No Preemption cannot always prevent (e.g.
    printers, tapes), but can sometimes use the
    following
  • If a process that is holding some resources
    requests another resource that cannot be
    immediately allocated to it, then all resources
    currently being held are released.
  • Preempted resources are added to the list of
    resources for which the process is waiting.
  • Process will be restarted only when it can regain
    its old resources, as well as the new ones that
    it is requesting.
  • Circular Wait impose a total ordering of all
    resource types, hierarchy and require that each
    process requests resources in an increasing order
    of enumeration.
  • For handling each of these four conditions
    potential problems
  • Resource utilization low, decreased throughput,
    starvation
  • Small number of resources can only hold a short
    time

12
Deadlock Avoidance
Develop protocol to insure deadlock will never
occur Requires that the system has some
additional a priori information available (i.e.,
must know some information ahead of time).
  • Simplest and most useful model requires that each
    process declare the maximum number of resources
    of each type that it may need before process
    starts to run.
  • Process is only allowed to start to run if all
    resources it needs are available and could be
    allocated to it
  • System keeps track of where resources are
    currently allocated and where they will be
    allocated in the future
  • Resource-allocation state is defined by the
    number of available and allocated resources, and
    the maximum demands of the processes.
  • Again, problem of low resource utilization
  • Since some resources and processes may sit idle
    and wait until other resources are available

13
Safe State
  • When a process requests an available resource,
    system must decide if immediate allocation leaves
    the system in a safe state.
  • System is in safe state if there exists some
    sequence in which all the processes can run, and
    each can get all the resources it needs to be
    able to complete. For example
  • Sequence ltP1, P2, , Pngt is safe if for each Pi,
    the resources that Pi can still request can be
    satisfied by currently available resources
    resources held by all the Pj, with jltI.
  • If 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, Pi1 can obtain its needed
    resources, and so on.
  • Such an algorithm is relatively simple to
    construct if processing is single-threaded
  • Complexity increases dramatically in a
    multiprogramming environment

14
Basic Facts
  • If a system is in safe state gt no deadlocks.
  • If a system is in unsafe state gt possibility of
    deadlock.
  • Avoidance gt ensure that a system will never
    enter an unsafe state.
  • Potential problems
  • Reduced resource utilization
  • Reduced throughput
  • Reduced performance
  • Will describe and try to solve problem using
    resource-allocation graphs

15
Safe, Unsafe , Deadlock State
16
Resource-Allocation Graph Concepts
  • Processes and resources are represented by nodes
  • Relationships between them are represented by
    edges
  • Resources must be claimed a priori before
    process can run.
  • Claim edge Pi ? Rj indicates that process Pj may
    request resource Rj (a claim is like a
    reservation)
  • Represented by a dashed line from the process to
    the resource.
  • Claim edge converts to request edge when a
    process actually requests a resource
  • Represented by a solid line from the process to
    the resource.
  • A request edge changes to an assignment edge when
    the resource is allocated to the process
  • Represented by solid line from the resource to
    the process.
  • When a resource is released by a process, the
    assignment edge reverts back to a claim edge.

17
Resource-Allocation Graph For Deadlock Avoidance
When resource is assigned, process can use it
Resource is requested when running process needs
to use it
Assigned
Requested (but not yet assigned)
Claimed (but not yet requested)
Resource is claimed before process starts to run
When process is done using it, resource
returns to claimed state
18
Unsafe State In Resource-Allocation Graph
If no cycles, system is in a safe
state (guarantee system not deadlocked)
If cycles exist, system is in an unsafe state
(may be deadlocked)
Cycle
19
Bankers Algorithm
DEADLOCK AVOIDANCE ALGORITHM
  • Assume there are multiple instances of
    resources.
  • Each process must make an a priori claim of
    maximum number of resources of each type it will
    use (ahead of time)
  • System determines whether allocating all the
    resources will leave the system in a safe state
    (i.e., some sequence exists in which all
    processes can run and still get all resources
    they need)
  • If so, resources are allocated and process can
    continue
  • If not, process must wait (important!)
  • When a process gets all its resources, it must
    return them in a finite amount of time.
  • System must maintain data structures to keep
    track of state of resources

20
Data Structures for the Bankers Algorithm
Let n number of processes, and m number of
resources types.
  • Available Vector of length m. If available j
    k, there are k instances of resource type Rj
    available.
  • Max n x m matrix. If Max i,j k, then
    process Pi may request at most k instances of
    resource type Rj.
  • Allocation n x m matrix. If Allocationi,j
    k then Pi is currently allocated k instances of
    Rj.
  • Need n x m matrix. If Needi,j k, then Pi
    may need k more instances of Rj to complete its
    task.
  • Need i,j Maxi,j Allocation i,j.

21
Resource-Request Algorithm for Process Pi
  • Request request vector for process Pi. If
    Requesti j k then process Pi wants k
    instances of resource type Rj.
  • 1. If Requesti lt Needi go to step 2. Otherwise,
    raise error condition, since process has exceeded
    its maximum claim.
  • 2. If Requesti lt Available, go to step 3.
    Otherwise Pi must wait, since resources are not
    available.
  • 3. Pretend to allocate requested resources to Pi
    by modifying the state as follows to see if
    allocating the resources would leave the system
    in a safe state
  • Availablei Availablei -
    Requesti
  • Allocationi Allocationi
    Requesti
  • Needi Needi Requesti
  • If safe -- the resources are allocated to Pi.
  • If unsafe Pi must wait, and the old
    resource-allocation state is restored

22
Bankers Algorithm Example
  • Five processes -- P0 through P4
  • 3 resource types A (10 instances), B (5
    instances), and C (7 instances).
  • Snapshot in time T0 is system in a safe state ?
  • Need is defined to be Max Allocation.
  • Allocation Max Need
    Available
  • A B C A B C A B
    C A B C
  • P0 0 1 0 7 5 3
    7 4 3 3 3 2
  • P1 2 0 0 3 2 2
    1 2 2
  • P2 3 0 2 9 0 2
    6 0 0
  • P3 2 1 1 2 2 2
    0 1 1
  • P4 0 0 2 4 3 3
    4 3 1

23
Bankers Algorithm Example (Cont.)
  • Five processes -- P0 through P4
  • 3 resource types A (10 instances), B (5
    instances), and C (7 instances).
  • Snapshot in time T0 is system in a safe state ?
  • Need is defined to be Max Allocation.
  • Allocation Max Need
    Available
  • A B C A B C A B
    C A B C
  • P0 0 1 0 7 5 3
    7 4 3 3 3 2 10 5 7
  • P1 2 0 0 3 2 2
    1 2 2 5 3 2
  • P2 3 0 2 9 0 2
    6 0 0 10 4 7
  • P3 2 1 1 2 2 2
    0 1 1 7 4 3
  • P4 0 0 2 4 3 3
    4 3 1 7 4 5
  • Yes, the system is in a safe state, since the
    sequence lt P1, P3, P4, P2, P0gt satisfies safety
    criteria.
  • Other sequences possible

24
Example -- P1 Requests (1,0,2) Additional
  • Check that Request lt Available (that is,
    (1,0,2)lt(3,3,2) true.
  • Available changed from (3,3,2) to (2,3,0) and
    P1s allocation changed from (2,0,0) to (3,0,2)
    -- IS THE SYSTEM STILL IN A SAFE STATE?
  • Allocation Max Need
    Available
  • A B C A B C A B
    C A B C
  • P0 0 1 0 7 5 3
    7 4 3 3 3 2 2 3 0
  • P1 2 0 0 3 0 2 3 2 2 1 2
    2 0 2 0
  • P2 3 0 2 9 0 2
    6 0 0
  • P3 2 1 1 2 2 2
    0 1 1
  • P4 0 0 2 4 3 3
    4 3 1

25
Example -- P1 Requests (1,0,2) (Cont.)
  • Check that Request lt Available (that is,
    (1,0,2)lt(3,3,2) true.
  • Available changed from (3,3,2) to (2,3,0) and
    P1s allocation changed from (2,0,0) to (3,0,2)
    -- IS THE SYSTEM STILL IN A SAFE STATE?
  • Allocation Max Need
    Available
  • A B C A B C A B
    C A B C
  • P0 0 1 0 7 5 3
    7 4 3 3 3 2 2 3 0 7 5 5
  • P1 2 0 0 3 0 2 3 2 2 1 2
    2 0 2 0 5 3 2
  • P2 3 0 2 9 0 2
    6 0 0
    10 5 7
  • P3 2 1 1 2 2 2
    0 1 1 7 4 3
  • P4 0 0 2 4 3 3
    4 3 1 7 4 5
  • YES SYSTEM IS STILL IN A SAFE STATE
  • Executing safety algorithm shows that sequence
    ltP1, P3, P4, P0, P2gt satisfies safety
    requirement. (as do ltP1, P3, P4, P2, P0gt , ltP1,
    P3, P2, P4, P0gt , and ltP1, P3, P2, P0, P4gt)

26
Example -- P4 Requests (3,3,0)
  • Can request for (3,3,0) by P4 be granted?
  • Assume initial state of system
  • Check that Request lt Available (that is,
    (3,3,0)lt(3,3,2) true.
  • Then check to see is the system is still in
    safe state?
  • Allocation Max Need
    Available
  • A B C A B C A B
    C A B C
  • P0 0 1 0 7 5 3
    7 4 3 3 3 2 0 0 2
  • P1 2 0 0 3 2 2
    1 2 2
  • P2 3 0 2 9 0 2
    6 0 0
  • P3 2 1 1 2 2 2
    0 1 1
  • P4 0 0 2 3 3 2 4 3 3 4 3
    1 1 0 1

27
Example -- P4 Requests (3,3,0)
  • Can request for (3,3,0) by P4 be granted?
  • Assume initial state of system
  • Check that Request lt Available (that is,
    (3,3,0)lt(3,3,2) true.
  • Then check to see is the system is still in
    safe state?
  • Allocation Max Need
    Available
  • A B C A B C A B
    C A B C
  • P0 0 1 0 7 5 3
    7 4 3 3 3 2 0 0 2
  • P1 2 0 0 3 2 2
    1 2 2
  • P2 3 0 2 9 0 2
    6 0 0
  • P3 2 1 1 2 2 2
    0 1 1
  • P4 0 0 2 3 3 2 4 3 3 4 3
    1 1 0 1
  • NO system is not in a safe state (need of every
    process gt available)
  • If the resources were allocated, there would be
    no sequence in which all of the programs could
    run to completion

28
Example -- P0 Requests (0,2,0)
  • Can request for (0,2,0) by P0 be granted?
  • Assume initial state of system
  • Check that Request lt Available (that is,
    (0,2,0)lt(3,3,2) true.
  • Then check to see is the system is still in
    safe state?
  • Allocation Max Need
    Available
  • A B C A B C A B
    C A B C
  • P0 0 1 0 0 3 0 7 5 3 7 4
    3 7 2 3 3 3 2 3 1 2
  • P1 2 0 0 3 2 2
    1 2 2
  • P2 3 0 2 9 0 2
    6 0 0
  • P3 2 1 1 2 2 2
    0 1 1
  • P4 0 0 2 4 3 3
    4 3 1

29
Example -- P0 Requests (0,2,0)
  • Can request for (0,2,0) by P0 be granted?
  • Assume initial state of system
  • Check that Request lt Available (that is,
    (0,2,0)lt(3,3,2) true.
  • Then check to see is the system is still in
    safe state?
  • Allocation Max Need
    Available
  • A B C A B C A B
    C A B C
  • P0 0 1 0 0 3 0 7 5 3 7 4
    3 7 2 3 3 3 2 3 1 2 7 5 3
  • P1 2 0 0 3 2 2
    1 2 2 7 2 3
  • P2 3 0 2 9 0 2
    6 0 0 10
    5 5
  • P3 2 1 1 2 2 2
    0 1 1 5 2 3
  • P4 0 0 2 4 3 3
    4 3 1
    10 5 7
  • Yes possibilities ltP3,P1,P0,P2,P4gt,
    ltP3,P1,P0,P4,P2gt, ltP3,P1,P2,P0,P4gt
  • What if request was for (0,3,0)

30
Example -- P0 Requests (0,2,0)
  • Can request for (0,2,0) by P0 be granted?
  • Assume initial state of system
  • Check that Request lt Available (that is,
    (0,2,0)lt(3,3,2) true.
  • Then check to see is the system is still in
    safe state?
  • Allocation Max Need
    Available
  • A B C A B C A B
    C A B C
  • P0 0 1 0 0 3 0 7 5 3 7 4
    3 7 2 3 3 3 2 3 1 2 7 5 3
  • P1 2 0 0 3 2 2
    1 2 2 7 2 3
  • P2 3 0 2 9 0 2
    6 0 0 10
    5 5
  • P3 2 1 1 2 2 2
    0 1 1 5 2 3
  • P4 0 0 2 4 3 3
    4 3 1
    10 5 7
  • Yes possibilities ltP3,P1,P0,P2,P4gt,
    ltP3,P1,P0,P4,P2gt, ltP3,P1,P2,P0,P4gt
  • What if request was for (0,3,0) NO UNSAFE
    STATE
  • Available (3,0,2) and no process could be
    guaranteed to run

31
Deadlock Detection
  • Bankers algorithm is expensive
  • Sometimes it is cheaper to
  • Allow the system to enter a deadlock state
  • Detect the deadlock (using a detection algorithm)
  • Recover from the deadlock (rollback or other
    recovery scheme)
  • Involves use of
  • Resource allocation graphs and graph theory and
    algorithms
  • ////

32
Single Instance of Each Resource Type
  • Maintain a wait-for graph
  • Nodes are processes.
  • Pi ? Pj if Pi is waiting for Pj.
  • (Graph looks similar to graphs used at
    beginning of session)
  • Periodically invoke an algorithm that searches
    for a cycle in the graph.
  • An algorithm to detect a cycle in a graph
    requires an order of n2 operations, where n is
    the number of vertices in the graph.
  • So, expensive
  • For handling multiple instances of some resource
    types, first look at resource allocation graph
    (instead of a wait-for graph)

33
Resource-Allocation Graph
  • Similar to resource-allocation graphs used for
    deadlock avoidance but since this is for
    detection, do not have to include a priori claims
    information (so no claim edges)
  • Have a set of vertices V and set of edges E
  • Use to describe relationships between processes
    and resources
  • V is partitioned into two types
  • P P1, P2, , Pn, the set consisting of all
    the processes in the system.
  • R R1, R2, , Rm, the set consisting of all
    resource types in the system.
  • Request edge directed edge Pi ? Rj
  • Assignment edge directed edge Rj ? Pi

34
Resource Allocation Graph Symbols
Process Resource type with four
instances Process requesting resource of this
type Resource is allocated to process(doesnt
matter which one)
35
Basic Concept
  • Construct a resource allocation graph for system
  • If graph contains no cycles -- then no
    deadlock.
  • If graph contains a cycle -- then
  • If only one instance per resource type, then
    deadlock.
  • If several instances per resource type,
    possibility of deadlock.

36
Example of a Resource Allocation Graph
No cycles, so no deadlock Note no claim
edges, only request and allocation
37
Resource Allocation Graph With A Deadlock
? Cycle (actually 2) AND Deadlock
38
Resource Allocation Graph With A Cycle But No
Deadlock
No deadlock because have multiple instances of
R2 P2 or P4 can run, release its instance of
the resource, and then the cycle will be broken
39
Evaluation Allocation Wait-for Graph
  • To simplify and improve speed, and if single
    instance of each resource type use
    WAIT-FOR-GRAPH
  • Produce a Wait-For Graph by removing the resource
    nodes from resource allocation graph
  • If cycle (and single instance of resources) gt
    deadlock

40
Resource-Allocation Graph and Wait-for Graph
Resource-Allocation Graph
Corresponding wait-for graph
41
Detection Single Instance of Resource Type
  • Maintain a wait-for graph
  • Nodes are processes
  • Pi gt Pj if Pi is waiting for Pj
  • Periodically invoke an algorithm that searches
    for cycle
  • Cycle deadlock
  • Algorithm expensive O(n2) , where nnumber of
    vertices
  • Plus overhead to maintain graph
  • But still cheaper than general algorithm
  • When should the detection algorithm be invoked?
  • Depends on how often a deadlock may occur
  • How many processes will need to be rolled back
    (one for each disjoint cycle)

42
Detection -- Several Instances of Resource Type
  • Available A vector of length m indicates the
    number of available resources of each type.
  • m distinct types of resources
  • Value of entry instances of that type
  • Allocation An n x m matrix defines the number
    of resources of each type currently allocated to
    each process.
  • n number of processes (i.e., one row for 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 resource type. Rj.
  • NOTE this resembles earlier Bankers Algorithm
    without Max

43
Detection Algorithm
  • Let Work and Finish be vectors of length m and n,
    respectively Initialize
  • m number of distinct kinds of resources, n
    number of processes
  • (a) Work Available
  • (b) For i 1,2, , n, if Allocationi ? 0, then
    Finishi false otherwise, Finishi
    true.
  • 2. Find an index i such that both
  • (a) Finishi false // Not done yet
  • (b) Requesti ? Work // Can get all resources
    needed // Work is single dim., with same
    // format as row in Request
  • If no such i exists, go to step 4.

44
Detection Algorithm (Cont.)
  • 3. Work Work Allocationi
  • Finishi truego to step 2.
  • 4. If Finishi false, for some i, 0lt i lt n,
    then the system is in deadlock state. Moreover,
    if Finishi false, then Pi is deadlocked.

Algorithm requires an order of O(m n2)
operations to detect whether the system is in
deadlocked state (where n number of resource
types and m number of resources of each type).
45
Example of Detection Algorithm
  • Five processes P0 through P4 three resource
    types A (7 instances), B (2 instances), and C (6
    instances).
  • Snapshot at time T0
  • Allocation Request Available
  • A B C A B C A B C
  • P0 0 1 0 0 0 0 0 0 0
  • P1 2 0 0 2 0 2
  • P2 3 0 3 0 0 0
  • P3 2 1 1 1 0 0
  • P4 0 0 2 0 0 2
  • Looks VERY similar to Bankers Algorithm seen
    earlier
  • Bankers Algorithm used Allocation, Max,
    Need, and Available
  • Request here is same as Need was there, and
    Max is gone
  • Difference that was before, this is while
    processes running

46
Example of Detection Algorithm
  • Five processes P0 through P4 three resource
    types A (7 instances), B (2 instances), and C (6
    instances).
  • Snapshot at time T0
  • Allocation Request Available
  • A B C A B C A B C
  • P0 0 1 0 0 0 0 0 0 0
  • P1 2 0 0 2 0 2
  • P2 3 0 3 0 0 0
  • P3 2 1 1 1 0 0
  • P4 0 0 2 0 0 2
  • Question is still the same is there a sequence
    in which the processes can run and receive all
    the resources they need ?
  • Sequence ltP0, P2, P3, P1, P4gt , ltP2, P0, P3, P1,
    P4gt, ltP0, P2, P4, P3, P1gt , etc. will result in
    Finishi true for all i.

47
Example (Cont.)
  • What if P2 requests an additional instance of
    type C.
  • Allocation Request Available
  • A B C A B C A B C
  • P0 0 1 0 0 0 0 0 0 0
  • P1 2 0 0 2 0 2
  • P2 3 0 3 0 0 0 0 0 1
  • P3 2 1 1 1 0 0
  • P4 0 0 2 0 0 2
  • State of system?

48
Example (Cont.)
  • What if P2 requests an additional instance of
    type C.
  • Allocation Request Available
  • A B C A B C A B C
  • P0 0 1 0 0 0 0 0 0 0
  • P1 2 0 0 2 0 2
  • P2 3 0 3 0 0 0 0 0 1
  • P3 2 1 1 1 0 0
  • P4 0 0 2 0 0 2
  • State of system?
  • Can reclaim resources held by process P0, but
    there are insufficient resources to fulfill other
    processes requests.
  • Deadlock exists, consisting of processes P1, P2,
    P3, and P4.

49
Detection-Algorithm Usage
  • QUESTION When, and how often, to invoke depends
    on
  • How often a deadlock is likely to occur?
  • How many processes will need to be rolled back?
  • One for each disjoint cycle
  • If detection algorithm is invoked arbitrarily,
    there may be many cycles in the resource graph
  • So will not be able to tell which of the many
    deadlocked processes caused the deadlock
    will have to roll back many processes.
  • If invoke whenever an allocation request is made
  • Only one process at most will ever have to be
    rolled back
  • BUT deadlock algorithm requires O(mn2) worst
    case
  • Also rollback / recovery quite expensive, too
  • Becomes a little different than Bankers
    Algorithm (i.e., becomes preemption, not
    detection)

50
Recovery from Deadlock Process Termination
  • Process termination kill the offending
    processes
  • Abort all deadlocked processes -- OR --
  • Abort one process at a time until the deadlock
    cycle is eliminated.
  • BUT termination is nasty business
  • Previous execution time all wasted
  • May leave data in unusable / incoherent state
    (roll back transactions ?)
  • Will leave jobs partially completed (e.g.,
    printing, copying tape, etc.)
  • Determining which process to terminate and
    checking if cycle is broken is COSTLY
  • In which order should the processes be aborted?
  • Priority of the process.
  • How long process has run, and how much longer
    until it finishes.
  • Resources the process has used.
  • Resources process needs to complete.
  • How many processes will need to be terminated.
  • Is process interactive or batch?

51
Recovery from Deadlock Resource Preemption
  • Rollback some proccess(es) and preempt their
    resources
  • Selecting a victim not easy
  • Involves both questions of resources and
    processes, similar to process termination
    questions
  • Least costly
  • Priority, length of time deadlocked, ripple
    effect, resources held
  • Rollback return to some safe state, restart
    process for that state
  • Finding safe state is not easy must keep data
    coherent
  • Could terminate, but costly (thats why trying to
    rollback, so dont have to kill process, i.e.,
    rollback to very beginning)
  • Starvation must avoid, and insure same process
    is not always rolled back

52
Combined Approach to Deadlock Handling
  • Can combine the three basic approaches
  • prevention
  • avoidance
  • detection
  • and allow the use of the optimal approach for
    each of resources in the system.
  • Partition resources into hierarchically ordered
    classes.
  • Use most appropriate technique for handling
    deadlocks within each class.
  • Remember / count the cost

53
The Real World and Deadlock Handling
  • Many / most systems do not try to handle
    deadlocks at low levels of the OS programmers
    must avoid them!
  • Running deadlock prevention / detection /
    avoidance / recovery algorithms simply too
    expensive
  • O(n2)
  • Too great an impact on overall system performance
  • Develop protocols, locks, ordering of resources
  • Use critical sections / monitors in code
  • Shared resources
  • Data / physical resources
  • Deadlocks

54
Suggested Approack to Deadlock Handling
  • At high levels of the system
  • Low levels enforce high level locks
  • Ability to cancel deadlocked jobs manually
  • Timeouts
  • At lower levels, can also use
  • Queues for printer, comm, tape, disk, etc.
  • Asynchronous instead of synchronous ops
  • I/O scheduling algorithms to avoid starvation
  • Avoidance / heirarchical protocols (order)
  • Timeout values and good error recovery
  • State machines / monitors
  • OO techninques (object enforces CS thread safe)
  • Order resources / locks protocols
  • Have traps to catch

55
Approaches to Deadlock Handling -2-
  • Deadlocks are often caused by
  • Inadequate documentation
  • Poor design (complexity)
  • Shoddy reviews
  • Insufficient testing
  • Dont let this happen to you

56
End of Chapter 7
Write a Comment
User Comments (0)
About PowerShow.com