Lect10 Deadlock

Embed Size (px)

Citation preview

  • 8/3/2019 Lect10 Deadlock

    1/22

    OPERATINGSYSTEMS

    (IS ZC362 LECTURE 10)

    DEADLOCK

    1

  • 8/3/2019 Lect10 Deadlock

    2/22

    2

    Deadlock Avoidance

    A decision is made dynamicallywhether the current resourceallocation request will, if granted,potentially lead to a deadlock

    Requires knowledge of futureprocess request

  • 8/3/2019 Lect10 Deadlock

    3/22

    3

    Two Approaches to DeadlockAvoidance

    Do not start a process if its demandsmight lead to deadlock

    Do not grant an incremental resourcerequest to a process if this allocationmight lead to deadlock

  • 8/3/2019 Lect10 Deadlock

    4/22

    Avoidance algorithms

    Single instance of a resourcetype

    Use a resource-allocation graph

    Multiple instances of a resourcetype

    Use the bankers algorithm

    4

  • 8/3/2019 Lect10 Deadlock

    5/22

    5

    Resource-Allocation Graph Algorithm

    3 types of edges

    Request Edge, Assignment Edge, Claim Edge Claim edgePiRj indicated that process Pi may

    request resource Rj; represented by a dashed line.

    Claim edge converts to request edge when a processrequests a resource.

    Request edge converted to an assignment edge whenthe resource is allocated to the process

    When a resource is released by a process, assignmentedge reconverts to a claim edge

    Resources must be claimed a priori in the system. Request PiRj can be granted only if converting the

    request edge PiRj to assignment edge RjPi does

    not result in the formation of cycle in the resource

    allocation graph.

  • 8/3/2019 Lect10 Deadlock

    6/22

    Resource-Allocation Graph

    6

  • 8/3/2019 Lect10 Deadlock

    7/22

    Unsafe State In Resource-AllocationGraph

    7

  • 8/3/2019 Lect10 Deadlock

    8/22

    Resource-Allocation GraphAlgorithm

    Suppose that process Pi requests aresource Rj

    The request can be granted only ifconverting the request edge to anassignment edge does not result inthe formation of a cycle in theresource allocation graph

    8

  • 8/3/2019 Lect10 Deadlock

    9/22

    9

    Bankers Algorithm Applicable to multiple instance of each

    resource type.

    This algorithm is less efficient.

    Each process must a priori claim maximum

    use. When a process requests a resource the

    system must check whether allocation ofthese resources will leave the system in safestate.

    When a process gets all its resources it mustreturn them in a finite amount of time.

  • 8/3/2019 Lect10 Deadlock

    10/22

    Data Structures for the BankersAlgorithm

    Let n= number of processes, and m= number of

    resources types. Available: Vector of length m. If available [j] =

    k, there are kinstances of resource type Rjavailable

    Max: n x m matrix. IfMax[i,j] = k, then processPimay request at most kinstances of resource

    type Rj Allocation: n x m matrix. If Allocation[i,j] = k

    then Pi is currently allocated kinstances ofRj

    Need: n x m matrix. IfNeed[i,j] = k, then Pi may

    need kmore instances ofRjto complete its task

    Need [i,j] = Max[i,j] Allocation [i,j]10

  • 8/3/2019 Lect10 Deadlock

    11/22

  • 8/3/2019 Lect10 Deadlock

    12/22

    Example (Cont.) The content of the matrix Need is defined to be Max

    AllocationNeed

    A B C

    P0 7 4 3

    P1 1 2 2

    P2 6 0 0

    P3 0 1 1

    P4 4 3 1

    The system is in a safe state since the sequence satisfies safety criteria

    12

  • 8/3/2019 Lect10 Deadlock

    13/22

    Example of Bankers Algorithm Allocation Max Need Available

    A B C A B C A B C A B CP0 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

    13

  • 8/3/2019 Lect10 Deadlock

    14/22

    Safety Algorithm1. Let Workand Finish be vectors of

    length m and n, respectively.

    Initialize:Work=AvailableFinish [i] = false for i = 0, 1, ,n- 1

    2. Find an i such that both:(a) Finish [i] = false(b) Needi Work

    If no such i exists, go to step 43. Work= Work+Allocationi

    Finish[i] = truego to step 2

    4. If Finish i == true for all i then14

    R R t Al ith f

  • 8/3/2019 Lect10 Deadlock

    15/22

    Resource-Request Algorithm forProcess PiRequest= request vector for process Pi. IfRequesti[j]

    = kthen process Pi

    wants kinstances of resource

    type Rj

    1.IfRequesti Needigo to step 2. Otherwise, raise

    error condition, since process has exceeded itsmaximum claim

    2.IfRequesti Available, go to step 3. Otherwise Pimust wait, since resources are not available

    3.Pretend to allocate requested resources to Pi by

    modifying the state as follows:

    Available =Available Request;

    Allocationi=Allocationi +

    Requesti; 15

    E l P R t (1 0 2)

  • 8/3/2019 Lect10 Deadlock

    16/22

    Example: P1 Request (1,0,2) Check that Request Available (that is, (1,0,2)

    (3,3,2) true Allocation Need

    Available A B C A B C A B C

    P0 0 1 0 7 4 3 3 3 2

    P1 2 0 0 0 2 0

    P2 3 0 2 6 0 0

    P3 2 1 1 0 1 1

    P4 0 0 2 4 3 1

    Executin safety al orithm shows that sequence