deadlocks 05iiit

Upload: sajjala-bala-krishna

Post on 10-Apr-2018

236 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/8/2019 Deadlocks 05iiit

    1/49

    8.1

    Chapter 8: Deadlocks

    Outline:Deadlock problemSystem ModelDeadlock CharacterizationMethods for Handling DeadlocksDeadlock PreventionDeadlock AvoidanceDeadlock DetectionRecovery from DeadlockCombined Approach to Deadlock Handling

  • 8/8/2019 Deadlocks 05iiit

    2/49

    8.2

    The Deadlock Problem

    A set of blocked processes each holding a resource andwaiting to acquire a resource held by another process inthe set.Example

    System has 2 tape drives.P 1 and P 2 each hold one tape drive and each needs anotherone.

    Examplesemaphores A and B, initialized to 1

    P 0 P 1

    wait (A); wait(B)wait (B); wait(A)

  • 8/8/2019 Deadlocks 05iiit

    3/49

    8.3

    Bridge Crossing Example

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

  • 8/8/2019 Deadlocks 05iiit

    4/49

    8.4

    System Model

    A set of processes is in a deadlock state when every process inthe set is waiting for an event that can be caused by anotherprocess in the set.Events:

    Resource acquisition and release.Resource types R 1, R2, . . ., R n

    Physical resources:CPU cycles, memory space, I/O devicesLogical resources: files, semaphores, and monitors

    Each resource type R i has W i instances.Each process utilizes a resource as follows:

    Request: if the request is not granted , then it must wait.Use: The process can operate on the resource.Release: The process releases the resources.

    Multi-threaded programs are good candidates for deadlockbecause multiple threads compete for shared resources.

  • 8/8/2019 Deadlocks 05iiit

    5/49

    8.5

    Deadlock Characterization

    Mutual exclusion: only one process at a time can use at leastone resource.Hold and wait: a process holding at least one resource iswaiting to acquire additional resources held by other processes.No preemption: Resources can not be preempted; that is aresource can be released only voluntarily by the process holdingit, after that process has completed its task.Circular wait: there exists a set { P 0, P 1, , P 0} of waitingprocesses such that P 0 is waiting for a resource that is held byP 1, P 1 is waiting for a resource that is held by

    P 2, , P n 1 is waiting for a resource that is held byP n, and P 0 is waiting for a resource that is held by P 0.

    Deadlock can arise if four conditions hold simultaneously.

    ALL FOUR CONDITIONS MUST HOLD.

  • 8/8/2019 Deadlocks 05iiit

    6/49

    8.6

    Resource-Allocation Graph

    V is partitioned into two types:P = {P 1, P 2, , P n}, the set consisting of all the processes inthe system.

    R = {R 1, R2, , Rm}, the set consisting of all resource typesin the system.

    request edge directed edge P 1 R j assignment edge directed edge R j P i

    A set of vertices V and a set of edges E .

    A tool to describe the deadlock.

  • 8/8/2019 Deadlocks 05iiit

    7/498.7

    Resource-Allocation Graph (Cont.)

    Process

    Resource Type with 4 instances

    P i requests instance of R j

    P i is holding an instance of R j

    P i

    P i

    R j

    R j

  • 8/8/2019 Deadlocks 05iiit

    8/498.8

    Example of a Resource Allocation Graph

  • 8/8/2019 Deadlocks 05iiit

    9/498.9

    Resource Allocation Graph With A Deadlock

  • 8/8/2019 Deadlocks 05iiit

    10/498.10

    Resource Allocation Graph With A Cycle But No Deadlock

  • 8/8/2019 Deadlocks 05iiit

    11/498.11

    Basic Facts

    If graph contains no cycles no deadlock.

    If graph contains a cycle if only one instance per resource type, then deadlock.if several instances per resource type, possibility ofdeadlock.

  • 8/8/2019 Deadlocks 05iiit

    12/498.12

    Methods for Handling Deadlocks

    Three waysEnsure that the system will never enter a deadlock state.

    Allow the system to enter a deadlock state and thenrecover.

    Ignore the problem and pretend that deadlocks never occurin the system; used by most operating systems, includingUNIX.

  • 8/8/2019 Deadlocks 05iiit

    13/498.13

    Methods for Handling Deadlocks

    To ensure that deadlocks never occur, the system canuse either deadlock prevention and deadlock-avoidanceschemes.

    Deadlock prevention: set of methods for ensuring that atleast one of the necessary conditions can not hold.Deadlock avoidance: Requires that operating system begiven in advance additional information concerning whichresources a process will request and use during its lifetime.

    Deadlock detection: examines the state of the system todetermine whether a deadlock ahs occurred or not.

    Alternatively, assume that deadlock would not occur.Resort to manual recovery when performance degradesdue to deadlock.

  • 8/8/2019 Deadlocks 05iiit

    14/498.14

    Deadlock Prevention

    Deadlock prevention is a set of methods for ensuringthat at least one of the necessary conditions can nothold.

  • 8/8/2019 Deadlocks 05iiit

    15/498.15

    Deadlock Prevention

    Mutual Exclusion It is not possible to preventdeadlocks through ME, as some resources areintrinsically non-sharable.Hold and Wait must guarantee that whenever aprocess requests a resource, it does not hold any other

    resources.Protocol 1: Requires a process to request and beallocated all its resources before it begins execution.

    System calls requesting resources for a processprecede all other system calls.

    Protocol 2: Allow process to request resources onlywhen the process has none.A process can request some resources and usethem. Before, it can request additional resources, itmust release all the resources that it is currentlyallocated.

    Restrain the ways request can be made.

  • 8/8/2019 Deadlocks 05iiit

    16/49

    8.16

    Deadlock Prevention

    Hold and Wait ExampleConsider a process that copies data from tape drive to a disk fileand then prints the results to the printer.Protocol1: If all the resources are requested at the beginning of aprocess, the process must request the tape drive, disk file, andprinter.

    It will the printer during entire execution, even though it needsat the end.

    Protocol 2: It copies data from tape drive to disk file and releasesthem. It gain requests disk file and printer.Disadvantages:

    Resource utilization is very lowResource may be allocated but unused for a long time.

    Starvation is possibleA process that needs several resources may have to waitindefinitely, at least one of the resources that it needs is alwaysallocated to another process.InefficientDelays process initiationFuture resource requirements must be known

  • 8/8/2019 Deadlocks 05iiit

    17/49

    8.17

    Deadlock Prevention (Cont.)

    No Preemption Protocol: If a process that is holding some resourcesrequests another resource that cannot be immediatelyallocated to it, then all resources currently being held arereleased.

    Preempted resources are added to the list of resources forwhich the process is waiting.Process will be restarted only when it can regain its oldresources, as well as the new ones that it is requesting.Possible for CPU registers and memory space whose statecan be restored later, but not possible for printers and tapedrives.

  • 8/8/2019 Deadlocks 05iiit

    18/49

    8.18

    Deadlock Prevention (Cont.)

    Circular Wait F:R N is a one-to-one function, where N is a set of naturalnumbers.Protocol 1: impose a total ordering of all resource types,and require that each process requests resources in an

    increasing order of enumeration.A process initially can request any number of instancesof type Ri. Then the process can request instances of aresource type Rj if and only if F(Rj) >F(Ri).

    Protocol 2 : Whenever a process requests an instance ofresource type Rj, it has released any other resources Risuch that F(Ri) >=F(Rj).Ordering

  • 8/8/2019 Deadlocks 05iiit

    19/49

    8.19

    Deadlock Avoidance

    Deadlock prevention algorithmsLow device utilization, and reduced system throughput.

    Alternative method is get additional information about theprocesses

    Resources currently available, resources currently allocatedto a process, and future requests and releases of eachprocess.

    Various algorithms differ about amount and type ofinformation required.

  • 8/8/2019 Deadlocks 05iiit

    20/49

    8.20

    Deadlock Avoidance

    Simplest and most useful model requires that eachprocess declare the maximum number of resources ofeach type that it may need.

    The deadlock-avoidance algorithm dynamically examinesthe resource-allocation state to ensure that there cannever be a circular-wait condition.

    Resource-allocation state is defined by the number ofavailable and allocated resources, and the maximumdemands of the processes.

    Requires that the system has some additional a priori informationavailable.

  • 8/8/2019 Deadlocks 05iiit

    21/49

    8.21

    Safe State

    When a process requests an available resource, system mustdecide if immediate allocation leaves the system in a safe state .

    System is in safe state if there exists a safe sequence of allprocesses.

    Sequence < P 1, P 2, , P n> is safe if for each P i, the resourcesthat Pi can still request can be satisfied by currently available

    resources + resources held by all the P j , with j

  • 8/8/2019 Deadlocks 05iiit

    22/49

    8.22

    Basic Facts

    If a system is in safe state no deadlocks.

    If a system is in unsafe state possibility of deadlock.

    Avoidance ensure that a system will never enter anunsafe state.

  • 8/8/2019 Deadlocks 05iiit

    23/49

    8.23

    Safe, Unsafe , Deadlock State

  • 8/8/2019 Deadlocks 05iiit

    24/49

    8.24

    Example

    Consider a system with 12 magnetic tape drives and three processesP0,P1,P2.P0 requires 10 tape drives, P1 may need 4, and P2 may need up to 9tape drives.Suppose at time t0, P0 is holding 5 tape drives, P1 holding 2, and P2 isholding 2 tape drives.There are 3 free tape drives

    Maximum needs allocated current needsP0 10 5 5P1 4 2 2P2 9 2 7

    At t0, system is in safe condition. satisfies satisfy safety condition.Suppose, at t1, P2 requests and is allocated 1 more tape drive.

    The system is no longer in safe state.The mistake was in granting the request of P2 for one more tape drive.The main idea of avoidance algorithm is to ensure the system is alwaysin a safe state.

  • 8/8/2019 Deadlocks 05iiit

    25/49

    8.25

    Two algorithms for DeadlockAvoidance

    Resource-Allocation Graph algoritmBankers algoritm

  • 8/8/2019 Deadlocks 05iiit

    26/49

    8.26

    Resource-Allocation Graph AlgorithmIf we have a resource-allocation (RA) system with one instance ofeach resource type, we can use this approach.

    Claim edge P i R j indicated that process P j may request resourceR j ; represented by a dashed line.Claim edge converts to request edge when a process requests aresource.When a resource is released by a process, assignment edgereconverts to a claim edge.Resources must be claimed a priori in the system.Before the process starts executing, all its claim edges must appear inthe RA graph.

    Suppose a process Pi requests a resource Rj. The request can begranted if converting the request edge Pi Rj to an assignmentedge Rj Pi does not result in the formation of a cycle in the RAgraph.

  • 8/8/2019 Deadlocks 05iiit

    27/49

    8.27

    Resource-Allocation Graph AlgorithmCycle detection algorithm is used to detect a cycle.If there are n processes detecting a cycle requires an order of n*noperations.

  • 8/8/2019 Deadlocks 05iiit

    28/49

    8.28

    Resource-Allocation Graph For Deadlock Avoidance

    Suppose P2 requests R2

  • 8/8/2019 Deadlocks 05iiit

    29/49

  • 8/8/2019 Deadlocks 05iiit

    30/49

    f

  • 8/8/2019 Deadlocks 05iiit

    31/49

    8.31

    Data Structures for the Bankers Algorithm

    Avai lable : Vector of length m . If available [ j ] = k , there are k instances of resource type R j available.Max: n x m matrix. If Max [i,j ] = k , then process P i may request atmost k instances of resource type R j .Alloca t ion : n x m matrix. If Allocation[ i,j ] = k then P i is currentlyallocated k instances of R j. Need: n x m matrix. If Need [i,j ] = k , then P i may need k moreinstances of R j to complete its task.

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

    Notation:Let X and Y be vectors of length n.We say X

  • 8/8/2019 Deadlocks 05iiit

    32/49

    8.32

    Resource-Request Algorithm for Process P i

    Request = request vector for process P i . If Request i [ j ] = k

    then process P i wants k instances of resource type R j .1. If Request i Need i go to step 2. Otherwise, raise error

    condition, since process has exceeded its maximum claim.2. If Request i Available , go to step 3. Otherwise P i must

    wait, since resources are not available.3. Pretend to allocate requested resources to P i by modifying

    the state as follows: Available = Available = Request i ; Allocation i = Allocation i + Request i ;Need i = Need i Request i;;

    If safe the resources are allocated to P i .

    If unsafe

    P i must wait, and the old resource-allocationstate is restored

  • 8/8/2019 Deadlocks 05iiit

    33/49

    8.33

    Safety Algorithm

    1. Let Work and Finish be vectors of length m and n ,

    respectively. Initialize:Work = AvailableFinish [i ] = false for i - 1,3, , n.

    2. Find and i such that both:(a) Finish [i ] = false

    (b) Need i Work If no such i exists, go to step 4.3. Work = Work + Allocation i

    Finish [i ] = true go to step 2.

    4. If Finish [i ] == true for all i , then the system is in a safestate.

    Safety algorithm may require an order of m*n 2 operations.

  • 8/8/2019 Deadlocks 05iiit

    34/49

    8.34

    Example of Bankers Algorithm

    5 processes P 0 through P 4; 3 resource types A (10 instances), B (5instances, and C (7 instances).Snapshot at time T 0:

    Allocation Max Available A B C A B C A B C

    P 0 0 1 0 7 5 3 3 3 2P 1 2 0 0 3 2 2P 2 3 0 2 9 0 2P 3 2 1 1 2 2 2

    P 4 0 0 2 4 3 3

  • 8/8/2019 Deadlocks 05iiit

    35/49

    8.35

    Example (Cont.)

    The content of the matrix. Need is defined to be Max Allocation.

    Need A B C

    P 0 7 4 3

    P 1 1 2 2P 2 6 0 0P 3 0 1 1P 4 4 3 1

    The system is in a safe state since the sequence < P 1, P 3, P 4,P 2, P 0> satisfies safety criteria.

  • 8/8/2019 Deadlocks 05iiit

    36/49

    8.36

    Example P 1 Request (1,0,2) (Cont.)

    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

    P 0 0 1 0 7 4 3 2 3 0P 1 3 0 2 0 2 0P 2 3 0 1 6 0 0P 3 2 1 1 0 1 1P 4 0 0 2 4 3 1

    Executing safety algorithm shows that sequence < P 1, P 3, P 4,P 0, P 2> satisfies safety requirement.Can request for (3,3,0) by P 4 be granted?Can request for (0,2,0) by P 0 be granted?

  • 8/8/2019 Deadlocks 05iiit

    37/49

    8.37

    Deadlock Detection

    Allow system to enter deadlock state

    Detection algorithmResource allocation graph algorithmDetection algorithm (similar to Bankers algoritm)

    Recovery scheme

  • 8/8/2019 Deadlocks 05iiit

    38/49

    8.38

    Single Instance of Each ResourceGraph based approach

    Maintain wait-for graphNodes are processes.P i P j if P i is waiting for P j .

    Periodically invoke an algorithm that searches for a cycle

    in the graph.

    An algorithm to detect a cycle in a graph requires anorder of n2 operations, where n is the number of verticesin the graph.

    R All i G h d W i f G h

  • 8/8/2019 Deadlocks 05iiit

    39/49

    8.39

    Resource-Allocation Graph and Wait-for Graph

    Resource-Allocation Graph Corresponding wait-for graph

  • 8/8/2019 Deadlocks 05iiit

    40/49

    l h

  • 8/8/2019 Deadlocks 05iiit

    41/49

    8.41

    Detection Algorithm

    1. Let Work and Finish be vectors of length m and n ,respectively Initialize:

    (a) Work = Available (b) For i = 1,2, , n , if Allocation i 0, then

    Finish [i] = false;otherwise, Finish [i] = true .

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

    If no such i exists, go to step 4.

    i Al i h (C )

  • 8/8/2019 Deadlocks 05iiit

    42/49

    8.42

    Detection Algorithm (Cont.)

    3. Work = Work + Allocation i Finish [i ] = true go to step 2.

    4. If Finish [i ] == false, for some i , 1 i n , then the system is indeadlock state. Moreover, if Finish [i ] == false , then P i isdeadlocked.

    Algorithm requires an order of O( m x n2) operations to detectwhether the system is in deadlocked state.

  • 8/8/2019 Deadlocks 05iiit

    43/49

    E l (C )

  • 8/8/2019 Deadlocks 05iiit

    44/49

    8.44

    Example (Cont.)

    P 2 requests an additional instance of type C .Request A B C

    P 0 0 0 0P

    12 0 1

    P 2 0 0 1P 3 1 0 0P 4 0 0 2

    State of system?

    Can reclaim resources held by process P 0, but insufficientresources to fulfill other processes; requests.Deadlock exists, consisting of processes P 1, P 2, P 3, and P 4.

  • 8/8/2019 Deadlocks 05iiit

    45/49

    R f D dl k P T i i

  • 8/8/2019 Deadlocks 05iiit

    46/49

    8.46

    Recovery from Deadlock: Process Termination

    Several alternatives exist.Inform the operatorAutomatic: system will recover from the deadlock automaticallyTwo options exist for automatically breaking a deadlock

    Process terminationResource preemption

    R f D dl k P T i i

  • 8/8/2019 Deadlocks 05iiit

    47/49

    8.47

    Recovery from Deadlock: Process Termination

    Abort all deadlocked processes.More expensive

    Abort one process at a time until the deadlock cycle is eliminated.Overhead: after the abort of each process detection algorithm have to beinvoked.

    Aborting a process is not easy.Printing or updating a file

    In which order should we choose to abort? (should incur minimum cost)Priority of the process.How long process has computed, and how much longer to completion.Resources the process has used.Resources process needs to complete.How many processes will need to be terminated.Is process interactive or batch?

    R f D dl k R P i

  • 8/8/2019 Deadlocks 05iiit

    48/49

    8.48

    Recovery from Deadlock: Resource Preemption

    We preempt some resources from one process and giveit to other processes. Until the deadlock cycle is broken.For preemption three issues are to be addressed

    Selecting a victim minimize cost.Number of resources the deadlocked process is

    holding, time consumed by it for execution.Rollback return to some safe state, restart processfor that state.

    Rollback as far as necessary to break the deadlock.

    Starvation same process may always be picked asvictim,Solution: include number of rollbacks in cost factor.

    Deadlock Handling: summary

  • 8/8/2019 Deadlocks 05iiit

    49/49

    Deadlock Handling: summary

    Three basic approachesPrevention : Use some protocol to prevent deadlocks,ensuring the system will never enter a deadlock state.

    Low resource utilizationAvoidance: Use a priori information; do not allow the

    system to enter unsafe state.Needs a priori information about future requests.Detection : Allow the system to enter deadlock state: thenrecover.

    Select the victim based on the cost factors.