chapter 3: deadlocks. cmps 111, fall 2007 2 overview ✦ resources ✦ why do deadlocks occur? ✦...

32
Chapter 3: Deadlocks

Upload: dulcie-wilkerson

Post on 03-Jan-2016

222 views

Category:

Documents


0 download

TRANSCRIPT

  • Chapter 3: Deadlocks

    CMPS 111, Fall 2007

    OverviewResourcesWhy do deadlocks occur?Dealing with deadlocksIgnoring them: ostrich algorithmDetecting & recovering from deadlockAvoiding deadlockPreventing deadlock

    CMPS 111, Fall 2007

    ResourcesResource: something a process usesUsually limited (at least somewhat)Examples of computer resourcesPrintersSemaphores / locksTables (in a database)Processes need access to resources in reasonable orderTwo types of resources:Preemptable resources: can be taken away from a process with no ill effectsNonpreemptable resources: will cause the process to fail if taken away

    CMPS 111, Fall 2007

    When do deadlocks happen?SupposeProcess 1 holds resource A and requests resource BProcess 2 holds B and requests ABoth can be blocked, with neither able to proceedDeadlocks occur when Processes are granted exclusive access to devices or software constructs (resources)Each deadlocked process needs a resource held by another deadlocked processProcess 1Process 2DEADLOCK!

    CMPS 111, Fall 2007

    Using resourcesSequence of events required to use a resourceRequest the resourceUse the resourceRelease the resource Cant use the resource if request is deniedRequesting process has optionsBlock and wait for resourceContinue (if possible) without it: may be able to use an alternate resourceProcess fails with error codeSome of these may be able to prevent deadlock

    CMPS 111, Fall 2007

    What is a deadlock?Formal definition: A set of processes is deadlocked if each process in the set is waiting for an event that only another process in the set can cause.Usually, the event is release of a currently held resourceIn deadlock, none of the processes canRunRelease resourcesBe awakened

    CMPS 111, Fall 2007

    Four conditions for deadlockMutual exclusionEach resource is assigned to at most one processHold and waitA process holding resources can request more resourcesNo preemptionPreviously granted resources cannot be forcibly taken awayCircular waitThere must be a circular chain of 2 or more processes where each is waiting for a resource held by the next member of the chain

    CMPS 111, Fall 2007

    Resource allocation graphsResource allocation modeled by directed graphsExample 1:Resource R assigned to process AExample 2:Process B is requesting / waiting for resource SExample 3:Process C holds T, waiting for UProcess D holds U, waiting for TC and D are in deadlock!

    CMPS 111, Fall 2007

    Dealing with deadlockHow can the OS deal with deadlock?Ignore the problem altogether!Hopefully, itll never happenDetect deadlock & recover from itDynamically avoid deadlockCareful resource allocationPrevent deadlockRemove at least one of the four necessary conditionsWell explore these tradeoffs

    CMPS 111, Fall 2007

    Getting into deadlockABCAcquire RAcquire SRelease RRelease SAcquire SAcquire TRelease SRelease TAcquire TAcquire RRelease TRelease RABCRSTAcquire RABCRSTAcquire SABCRSTAcquire TABCRSTAcquire SABCRSTAcquire TABCRSTAcquire RDEADLOCK!

    CMPS 111, Fall 2007

    Not getting into deadlockMany situations may result in deadlock (but dont have to)In previous example, A could release R before C requests R, resulting in no deadlockCan we always get out of it this way?Find ways to:Detect deadlock and reverse itStop it from happening in the first place

    CMPS 111, Fall 2007

    The Ostrich AlgorithmPretend theres no problemReasonable if Deadlocks occur very rarely Cost of prevention is highUNIX and Windows take this approachResources (memory, CPU, disk space) are plentifulDeadlocks over such resources rarely occurDeadlocks typically handled by rebootingTrade off between convenience and correctness

    CMPS 111, Fall 2007

    Detecting deadlocks using graphsProcess holdings and requests in the table and in the graph (theyre equivalent)Graph contains a cycle deadlock!Easy to pick out by looking at it (in this case)Need to mechanically detect deadlockNot all processes are deadlocked (A, C, F not in deadlock)

    ProcessHoldsWantsARSBTCSDUS,TETVFWSGVU

    CMPS 111, Fall 2007

    Deadlock detection algorithmGeneral idea: try to find cycles in the resource allocation graphAlgorithm: depth-first search at each nodeMark arcs as theyre traversedBuild list of visited nodesIf node to be added is already on the list, a cycle exists!Cycle deadlockFor each node N in the graph { Set L = empty list unmark all arcs Traverse (N,L)}If no deadlock reported by now, there isnt any

    define Traverse (C,L) { If C in L, report deadlock! Add C to L For each unmarked arc from C { Mark the arc Set A = arc destination /* NOTE: L is a local variable */ Traverse (A,L) }}

    CMPS 111, Fall 2007

    Resources with multiple instancesPrevious algorithm only works if theres one instance of each resourceIf there are multiple instances of each resource, we need a different methodTrack current usage and requests for each processTo detect deadlock, try to find a scenario where all processes can finishIf no such scenario exists, we have deadlock

  • Detection with One Resource of Each Type (2)Data structures needed by deadlock detection algorithm

    CMPS 111, Fall 2007

    Deadlock detection algorithmHoldWantcurrent=avail;for (j = 0; j < N; j++) {for (k=0; k