Deadlock 6

Embed Size (px)

Citation preview

  • 8/14/2019 Deadlock 6

    1/53

    mb 1

    Deadlock

    CPSC 355/655

    version 10/2002

  • 8/14/2019 Deadlock 6

    2/53

    mb 2

    Deadlock

    Deadlock exists if a process is waiting for

    an event which can never occur. e.g., two processes each waiting for the other

    to set a flag, or

    to send a message, or

    to give up a resource

    Our focus: contention over resources processes waiting for requested resources

    10/1999

  • 8/14/2019 Deadlock 6

    3/53

    mb 3

    Indefinite Postponement

    where the timing is such that a process does not get to do what it is trying to do, or

    does not get whatever it is trying to get,

    indefinitely,

    but there is a possibility that it may eventually succeed

    e.g., a process stuck in the ready queue because of priority

    in a busy wait, trying to enter a critical section, unable to

    because of timing

    10/1999

  • 8/14/2019 Deadlock 6

    4/53

    mb 4

    Deadlock versusIndefinite Postponement

    Indefinite Postponement exampleA: while (TSET LOCK); B: while (TSET LOCK);

    manipulate X; manipulate X;

    LOCK FALSE; LOCK FALSE;

    Deadlock example

    C: begin critical section X D: begin critical section Y

    begin critical section Y begin critical section Xmanipulate X & Y manipulate X & Y

    end critical section Y end critical section X

    end critical section X end critical section Y

    10/1999

  • 8/14/2019 Deadlock 6

    5/53

    mb 5

    Resource Allocation Graph

    RAG shows

    which processes have requested which resources and are currently waiting for them to be granted

    which resources are owned by which processes

    i.e., which resources have been granted to whichprocesses

    A I

    process A requests resource I

    B II

    process B owns resource II10/2000

  • 8/14/2019 Deadlock 6

    6/53

    mb 6

    RAG ExampleA owns I and IIB requests IV

    C owns III and requests ID owns IV and requests IIIE requests I, II, and III

    A

    B

    C

    D

    E

    I

    II

    III

    IV

    10/2000

  • 8/14/2019 Deadlock 6

    7/53

    mb 7

    Directed Cycles

    Process Pi owns resource Ri

    and requests Ri+1(mod n)

    fori = 0, 1, ..., n-1.

    P0

    P1

    R0 R1

    R2

    P2

    Pn-1

    ...

    10/2000

  • 8/14/2019 Deadlock 6

    8/53

    mb 8

    Multiple Resources

    There may be multiple resources of the same type - I.e.,

    equivalent resources.

    Dots in the resource box indicate how many when there are

    multiple equivalent resources.

    two resources

    three resources

    10/2000

  • 8/14/2019 Deadlock 6

    9/53

    mb 9

    Directed Cyclesand Multiple Resources

    A directed cycle does not guarantee deadlock

    if there are multiple resources of the same type.

    No deadlock exists here.

    C II

    A

    BI

    What about here?

    C II

    A

    BI

    10/2000

  • 8/14/2019 Deadlock 6

    10/53

    mb 10

    Conditions Necessary forDeadlock

    Exclusive use A process is allowed exclusive access to resources it has

    been granted.

    Hold and wait When a process requests a resource and is waiting for it, it

    will not relinquish control over resources it already owns.

    No preemption Nothing can take away resources from a process which

    wishes to retain them.

    Circular wait A directed cycle exists in the RAG.

    10/2000

  • 8/14/2019 Deadlock 6

    11/53

    mb 11

    Deadlock Exists if ...

    The four necessary conditions hold, Exclusive use

    Hold and wait

    No preemption

    Circular wait

    AND

    there is no way to break the directed cycle byhaving a needed resource become availablewithout violating the other 3 necessary conditions.

    10/1999

  • 8/14/2019 Deadlock 6

    12/53

    mb 12

    Cycles May Be Broken

    If a process finishes using a resource andvoluntarily gives it up, then this is not aviolation of any of the necessary conditions.

    Exclusive useHold and wait

    No preemption

    Circular wait

    10/1999

  • 8/14/2019 Deadlock 6

    13/53

    mb 13

    There is no deadlock if ...

    A series of processes

    can finish

    voluntarily giving up resources

    which are then reassigned

    eventually freeing up one in each cycle

    C

    DB

    A

    E

    C

    DB

    A

    E

    DB

    C

    DB

    A

    DB

    A

    10/1999

  • 8/14/2019 Deadlock 6

    14/53

    mb 14

    Dealing with Deadlock

    Three approaches Detection and Recovery

    detect deadlock when it occurs

    somehow break deadlock and allow most processes tocontinue useful work

    Prevention

    determine how resources are managed focuses on the resources owned by the requesting process and the

    available resources

    will not take into account the entire state of the current system

    Avoidance

    determine if resource can be granted based on current state of entire system

    which processes own which resources10/2000

  • 8/14/2019 Deadlock 6

    15/53

    mb 15

    Proactive vs ReactiveApproaches

    Proactive keep deadlock from happening

    Prevention

    Avoidance

    Reactive

    act in response to deadlock Detection and Recovery

    10/1999

  • 8/14/2019 Deadlock 6

    16/53

    mb 16

    Deadlock Detection

    Does a deadlock exist now? Assume that there will be no further new requests for

    resources.

    Graph theory approach applied to the RAG prune off processes which have all requested resources

    allocated

    assume these processes have now finished

    reassign their resources

    repeat until

    all processes are pruned, or

    deadlock

    10/1999

  • 8/14/2019 Deadlock 6

    17/53

    mb 17

    Reallocating Resources

    many ways to reassign resources optimal strategy: always give to a process which willhave all its requested resources

    D

    C

    B

    A

    10/1999

  • 8/14/2019 Deadlock 6

    18/53

    mb 18

    Detection Example 1I

    D

    C

    BA

    III

    II

    I

    D

    C

    BAIII

    II

    I

    D

    C

    B III

    II

    I

    III

    II

    I

    B III

    II

    10/1999

  • 8/14/2019 Deadlock 6

    19/53

    mb 19

    Detection Example 2

    I

    D

    C

    BA

    IV

    III

    II

    I

    D

    C

    BA

    IV

    III

    II

    I

    D

    C

    B

    IV

    III

    II

    I

    D

    C

    IV

    III

    II

    I

    C

    IV

    III

    II

    I

    IV

    III

    II

    10/1999

  • 8/14/2019 Deadlock 6

    20/53

    mb 20

    Detection Example 3

    I

    D

    C

    B

    A IIIII

    IV

    V

    VI VIIE

    F I

    D

    C

    B

    A IIIII

    IV

    V

    VI VIIE

    FI

    C

    B

    A IIIII

    IV

    V

    VI VIIE

    FI

    C

    B

    A IIIII

    IV

    V

    VI VII

    F

    10/2000

    I

    C

    B

    A IIIII

    IV

    V

    VI VII

  • 8/14/2019 Deadlock 6

    21/53

    mb 21

    Ingredients for Detection

    Processes P1, P2, ...,Pn

    Resources R1, R2, ...,Rm

    OWNik = the number ofRkowned by Pi

    REQik = the number ofRkrequested by Pi

    FREEk = the number ofRkavailable

    FINi = boolean indicating whetherPi is finished

    10/2000

  • 8/14/2019 Deadlock 6

    22/53

    mb 22

    Detection Algorithm

    continue true

    while (continue)find i such that FINi = false and REQik FREEk for all k

    if i does not exist then

    continue false

    else

    FREEk FREEk + OWNik for all kFINi true

    end-if

    end-while

    if there exists i such that FINi

    = false then

    return DEADLOCK

    else

    return NO DEADLOCK

    end-if

    10/1999

  • 8/14/2019 Deadlock 6

    23/53

    mb 23

    Detection withOne Resource of Each Type

    A directed cycle is sufficient. assuming that the other necessary conditions hold

    But finding a cycle in a graph with N nodestakes O(N2) time.

    Reduce graph to WAIT FOR graph. eliminating many nodes

    10/1999

  • 8/14/2019 Deadlock 6

    24/53

    mb 24

    WAIT FOR Graphs

    BA

    BA

    reduces to WAIT FOR graph

    10/2000

    Y

    X

    Z

    X

    Z

    Y

    reduces to WAIT FOR graph

  • 8/14/2019 Deadlock 6

    25/53

  • 8/14/2019 Deadlock 6

    26/53

    mb 26

    Deadlock Recovery

    Choose a victim process either terminate it or

    preempt its resources.

    one or many in a deadlock

    Process preemption take away one or all resources

    it must be able to recover

    starvation possible

    10/1999

  • 8/14/2019 Deadlock 6

    27/53

    mb 27

    Which Process?

    can process recover

    how much time has process used

    how much additional time needed to finish

    which resources are being used

    which resources are needed

    priority

    how many more processes will have to be victims

    10/1999

  • 8/14/2019 Deadlock 6

    28/53

    mb 28

    Choosing a Victim

    CBA

    CBA CBA

    10/1999

  • 8/14/2019 Deadlock 6

    29/53

    mb 29

    Deadlock Prevention

    DENY one of the 4 necessary conditions

    deny exclusive use

    deny hold and wait

    deny no preemption

    deny circular wait

    Choose any one of these.

    Adopt rules governing resources which preventthis one condition from ever occurring.

    10/1999

  • 8/14/2019 Deadlock 6

    30/53

  • 8/14/2019 Deadlock 6

    31/53

    mb 31

    Deny Hold and Wait

    e.g., require all resources to be

    requested at the same time at job level before or when process starts

    at sub-job (step) level

    give up all resources at end of step

    request all resources for next step

    can be very wasteful can lead to low resource usage

    may lead to indefinite postponement most practical in batch environment

    in other environments, applicable to swap files

    10/1999

  • 8/14/2019 Deadlock 6

    32/53

    mb 32

    Deny No Preemption

    e.g., take away resources which are neededby another process

    process must either be able to return resources to same state, or

    roll back to an earlier state

    may lead to indefinite postponement

    applicable to CPU

    real memory

    10/2002

  • 8/14/2019 Deadlock 6

    33/53

    mb 33

    Deny Circular Wait

    e.g., order resources and require that

    they be granted in that order order resources

    R0, R1, R2, R3, ...

    R0 < R1 < R2 < R3 < ... require that ifRi < Rk ,

    then Ri can only be requested before Rk

    applicable to internal resources PCB

    page tables

    kernel semaphores in Linux7/2001

  • 8/14/2019 Deadlock 6

    34/53

    mb 34

    Proof by Contradiction

    R0 < R1 < R2 < R3 < ...

    ifRi < Rk , then Ri can only be requested before Rk

    so ifPm owns Rs and is requesting Rt, this impliesthat

    Rs < Rt

    if there is a circular waitP0 owns R and is requesting R

    P1 owns R and is requesting R

    ...Pn-1 owns R and is requesting R

    this implies

    R < R < R < ... < R < R 10/2000

  • 8/14/2019 Deadlock 6

    35/53

    mb 35

    Denying Circular Wait

    P0 owns R and is requesting R

    P1 owns R and is requesting R

    ...Pn-1 owns R and is requesting R

    this implies

    R < R < R < ... < R < R

    Po

    P1

    P2

    R

    R

    R

    Pn-1

    ...

    10/2000

  • 8/14/2019 Deadlock 6

    36/53

    mb 36

    Deadlock Prevention Rules

    Applicability of rules is determined by which resources one process is requesting, and/or

    which resources are available, and/or

    which resources the process has, and/or

    the type of a resource

    They are applied without respect to which

    resources are owned by other processes.

    2/2002

  • 8/14/2019 Deadlock 6

    37/53

    mb 37

    Deadlock AvoidanceRequests for resources are

    granted, or denied (postponed)

    depending on the overall state of the system.

    A resource is granted if this cannot lead to adeadlock in the future.

    Knowledge about all future resource needs ofeach process is required.

    A process must declare its maximum resource needs

    when it starts. 10/1999

  • 8/14/2019 Deadlock 6

    38/53

    mb 38

    Claim Edges

    IA

    IIB

    A owns I

    B is requesting II

    IIIC C will request III

    10/2000

  • 8/14/2019 Deadlock 6

    39/53

    mb 39

    Avoidance vs Prevention

    Avoidance would deny II to B once A owns I.

    Prevention might allow the following.

    Assume A & B each need both I & II and A owns I.

    A

    B

    I II

    A

    B

    I II

    10/2000

  • 8/14/2019 Deadlock 6

    40/53

    mb 40

    Safe States

    Assume that we know each process maximum resource needs

    each process will finish once its has all the resource itneeds and release all resources

    The system state is Safe if available resourcescan be allocated to processes in some order sothat

    each process will finish

    no deadlock will occur

    An Unsafe state may lead to deadlock. may not currently be deadlock

    10/2002

  • 8/14/2019 Deadlock 6

    41/53

    mb 41

    What is Unsafe?

    Granting ownership ofII to B would make thestate Unsafe.

    A

    B

    I II

    A

    B

    I II

    10/2000

  • 8/14/2019 Deadlock 6

    42/53

    mb 42

    Safe Example

    I

    C

    B

    A

    II

    III

    10/2000

    I

    C

    B

    A

    II

    III

    I

    C

    B

    A

    II

    III

    I

    C

    B

    A

    II

    III

    I

    C

    B

    A

    II

    III

    I

    C

    B

    A

    II

    III

  • 8/14/2019 Deadlock 6

    43/53

    mb 43

    Unsafe Examples

    IV

    F

    E

    D

    V

    VI

    10/2000

    VII

    K

    H

    G

  • 8/14/2019 Deadlock 6

    44/53

    mb 44

    Bankers Algorithm

    Bank

    has money in several currencies currencies can not be exchanged

    will loan it out

    but only if available funds are sufficient

    will not possibly lead to deadlock

    Customers have various needs

    credit line specifies the maximum need

    request funds in stages

    will finish when all needs are fulfilled

    and return all funds to bank

    10/1999

  • 8/14/2019 Deadlock 6

    45/53

    mb 45

    Bankers Algorithm(for 1 type of resource)

    To determine if a request should be granted toprocess Pk:

    ALLOCATEDi = number of resources already allocated to process PiNEED

    i

    = number of additional resources needed by Pi

    to finish

    AVAIL = number of resources available

    REQUEST = current request

    1 If REQUEST > AVAIL, then deny request because not enough

    resources.

    2 Pretend to allocate request and apply the Safety algorithm.

    3 If Safety Algorithm returns SAFE, then grant request otherwisedeny it.

    10/2000

    Safety Algorithm

  • 8/14/2019 Deadlock 6

    46/53

    mb 46

    Safety Algorithm

    (for 1 type of resource)

    Safety algorithm

    continue true

    while (continue)

    find i such that FINi = false and Ni AV

    if i does not exist then

    continue falseelse

    AV AV + AiFINi true

    end-while

    if there exists i such that FINi = false then

    return UNSAFE

    else

    return SAFE

    Pretend to grant resource

    Ak = ALLOCk + REQUEST

    Nk

    = NEEDk

    - REQUEST

    AV = AVAIL - REQUEST

    Ai = ALLOCATEDi for i k

    Ni = NEEDi for i k

    FINi = false for all i

    10/2000

  • 8/14/2019 Deadlock 6

    47/53

  • 8/14/2019 Deadlock 6

    48/53

    mb 48

    Applying Safety Algorithm #2

    Capital = 10

    Customer Credit Current

    Line Loan

    A 5 3

    B 6 4

    C 4 0

    Cash left (AVAIL) = 3

    C requests 2. Should this be granted?

    Would new state be Safe?

    New Still

    Loan Need

    3 2

    4 2

    2 2

    Cash left = 1

    UNSAFE because

    all customers need

    at least 2 more

    No

    10/1999

  • 8/14/2019 Deadlock 6

    49/53

    mb 49

    Applying Safety Algorithm #3

    Note that a state is SAFE only if all can finish.

    Capital = 10

    Customer Credit Current

    Line Loan

    A 2 1

    B 6 3

    C 9 3

    Cash left (AVAIL) = 3

    C requests 2. Should this be granted?

    Would new state be Safe?

    New Still

    Loan Need

    1 1

    3 3

    5 4

    Cash left = 1

    UNSAFE because

    A can finish: AVAIL = 2

    but no one else can

    No

    10/1999

    A l i th B k Al ith

  • 8/14/2019 Deadlock 6

    50/53

    mb 50

    Needs

    Customer Request Capital A B C D E Granted?

    20 10 6 8 6 10

    A 5 15 5 6 8 6 10 yes

    B 3 12 5 3 8 6 10 yes

    C 4 8 5 3 4 6 10 yes

    D 3 5 5 3 4 3 10 yes

    E 4 1 5 3 4 3 6 no

    C 2 3 5 3 2 3 10 yesA 2 1 3 3 2 3 10 no

    B 2 1 5 1 2 3 10 yes

    D 1 0 5 1 2 2 10 no

    B 1 0 5 0 2 3 10 yes6 5 - 2 3 10

    E 4 2 5 - 2 3 6 yes

    A 2 0 3 - 2 3 10 no

    D 1 1 5 - 2 2 6 no

    Applying the Bankers Algorithm

    7/2001

  • 8/14/2019 Deadlock 6

    51/53

    mb 51

    General Case

    Many resources of many types!

    Banker has many currencies!

    AVAIL and REQUEST are vectors.

    one entry for each resource type

    AVAILm and REQUESTm

    So are ALLOCATEDi and NEEDi.

    ALLOCATEDim

    NEEDim

    NEEDi AVAIL is true if for all m

    NEEDim AVAILm10/1999

  • 8/14/2019 Deadlock 6

    52/53

    mb 52

    Special Case:One resource of each type

    A request will not be granted if a directed cycle would be created.

    A

    B

    I II

    A

    B

    I II

    10/2000

  • 8/14/2019 Deadlock 6

    53/53

    mb 53

    Combined Approach

    Internal resources PCBs, page tables, process headers,

    can be ordered, prevention

    CPU, real memory prevention via deny no preemption

    Swap space

    prevention, allocate all at the beginning

    Non-shareable job resources dedicated disk drives, tapes

    avoidance, or prevention, allocate all together

    Shareable job resources disks; make shareable through drivers

    Logical job resources

    locks; detection (and recovery) 10/1999