Kinesia Online Course
Operating Systems
Kinesia LLC, 2003

  1. Introduction
  2. Processes
  3. Inter Process Communication
  4. Deadlocks
  5. Memory Management
 
  1. File Systems
  2. Protection and Security
  3. I/O Systems



Deadlocks
  1. Introduction

    processes are blocked, waiting on events that could never happen

    different from starvation ( the associated resource is in continuous use )

  2. System Model
  3. A system consists of a finite number of resources to be distributed among a number of competing processes.
  4. Resources can only be used by a single process at any single instance of time
  5. Resources can be of different types, e.g. memory space, CPU cycles, files, I/O devices
  6. A process must request a resource before using it, and must release the resource after using it
  7. A normal operation consists of a sequence of events:
    • Request: if request cannot be granted immediately, process must wait until it can acquire the resource
    • Use: operate on the resource ( e.g. printing )
    • Release: release the resource

      Examples:

      open file, read file, close file
      allocate memory, use memory, free memory
  8. A set of processes is in a deadlock state when every process in the set is waiting for an event that can be caused by onother process in the set.
  9. Deadlock modeling

    Four necessary conditions for deadlock:

    e.g. Resources are two books: Java Programming, OS Principles,
           need the two books to finish project

    Resource-allocation graphs


    the process is holding the resource
     
    the process is requesting the resource

    cycle ~ deadlock

    Deadlock

    if graph contains no cycles => no process is in deadlock

    if graph contains cycles => deadlock may exist

    Reduction of Resource Graph

    If a process's resource requests can be granted, then we say that the graph can be reduced by that process
    ( i.e. the process is allowed to complete its execution and return all its resources )

    If a graph can be reduced by all the processes, then there is no deadlock.
    If a graph cannot be reduced by all the processes, the irreducible processes constitute the set of deadlock processes in the graph
    An example of reduction of an RAG ( Resource Allocation Graph )

    Example of RAG with cycle but no deadlock

    A deadlock example in Java:

    DeadlockExamp.java

  10. Deadlock handling strategies

  11. Ignore the problem altogether
  12. Prevention -- use a protocol to ensure that the system will never enter a deadlock state
  13. Detection and recovery -- allow the system to enter a deadlock state and then recover
  14. Dynamic avoidance -- by careful resource allocation
  15. a) The Ostrich Algorithm

    pretend there's no problem ( Windows, UNIX )
    undetected deadlock may result in deterioration of the system performance; eventually, the system will stop functioning and will need to be restarted manually

    b) Detection and Recovery

    system monitors requests and releases resources

    check resource graph to see if cycle exists; kill a process if necessary

    or if a process has been blocked for, say one hour, kill it

    c) Deadlock Prevention

  16. mutual exclusion
    printer cannot be shared by 2 processes
    read-only file -- sharable
    A process never has to wait for a sharable resource
    In general, it is not possible to prevent deadlocks by denying mutual-exclusion condition

  17. Hold and wait
    have all processes request all their resources before start execution -- inefficient, also, process don't know in advance resources needed

    before a process can request any additional resources, it must release temporarily all the resources that it currently holds, if successful, it can get teh original resources back
    may have starvation

  18. No preemption
    allows preemption
    OH, what if its printer

  19. Circular wait

    order resource types
    process requests resources in an increasing order

    Let R = { R1, R2, ..., Rm } be the set of resource types

    F: R → N ( set of natural numbers )

    e.g.

      F ( tape drive ) = 1
      F ( disk drive ) = 5
      F ( printer ) = 12

    Suppose a process first requests Ri, it can request Rj only if F(Rj) > F(Ri)

    Under this constraint, circular-wait condition cannot hold

    Proof

    by contradiction

    Suppose circular wait holds for
    { P0, P1, ..., Pn }

    Pi is waiting for Ri which is held by Pi+1

    Because Pi+1 is holding Ri while requesting Ri+1, we have

      F( Ri ) < F( Ri+1 ) for all i
      => F( R0 ) < F( R1 ) < ... < F( Rn ) < F( R0 )
      => F( R0 ) < F( R0 )
    which is impossible.
    Thus circular wait does not exist.
  20. d) Deadlock Avoidance

    In prevention strategy, we restrain how request can be made.

    Here, we want to develop an algorithm to avoid deadlock by making the right choice all the time

  21. Banker's Algorithm for Single Resource Type

    Dijkstra's Banker's Algorithm is an approach to trying to give processes as much as is possible, while guaranteeing no deadlock.

    safe state -- a state is safe if the system can allocate resources to each process in some order and still avoid a deadlock.

    bank:

      credits available = 10 units
      Process   Using
      ( allocated )  
      Maximum needs
      P0 06
      P1 05
      P2 04
      P3 07

    At time t0, the system is safe.

    At time t1:

      credits available = 2 units
      Process   Using
      ( allocated )  
      Maximum
      needs
        Maximum
      requests
      P0 165
      P1 154
      P2 242
      P3 473

    credits left = 2 ( i.e. available = 2 ), state is still safe

    delay any requests from P0, P1, P3

    can grant further requests to P2, why?


      credits available = 1 unit
      Process   Using
      ( allocated )  
      Maximum
      needs
        Maximum
      requests
      P0 165
      P1 253
      P2 242
      P3 473

    This is an unsafe state; with only one credit left, if all processes request their maximum units, all need to be blocked and wait → deadlock

    Of course, unsafe state may not lead to deadlock but the bank cannot count on this.

    The banker's algorithm is to consider each request as it occurs,
    if granting the request would result in a safe state, request is granted
    otherwise, it is postponed

  22. Resource Trajectories

    A deadlock path

    • P2 requests R2
    • The OS grants the request of P2
    • P1 requests R1
    • The OS grants the request of P1
    • P2 requests R1
    • The OS cannot grant the request of P2, because R1 is locked.
    • P1 requests R2
    • The OS cannot grant the request of P1, because R2 is locked.
    • No further progress is possible, i.e. we have a deadlock.

    An avoidance path

    • P2 requests R2
    • The OS grants the request of P2
    • P1 requests R1
    • The OS postpones granting the request of P1, because it would lead to an unsafe state.
    • P2 requests R1
    • The OS grants the request of P2
    • P2 releases R1
    • The OS grants the request of P1 for R1
    • P1 requests R2
    • The OS cannot grant the request of P1, because it R2 is still locked by P2.
    • P2 releases R2
    • The OS grants the request of P1 for R2
    • P1 releases R2
    • P1 releases R1
  23. Banker's Algorithm for Multiple Resource Type

    Let

    n = # of processes in system
    m = # of resource types

    Example

      a b c d
    EXISTING = ( 6 3 4 2 )
    POSSESSED = ( 5 3 2 2 )
    AVAILABLE = ( 1 0 2 0 )

    Notation

    for two vectors X, Y of length m,
    X ≤ Y if and only if X[i] ≤ Y[i]   for all i = 1, 2, ..., m

    e.g. A = ( 1, 7, 3, 2 ), and B = ( 0, 3, 1, 1 ) then B ≤ A

    X < Y if X ≤ Y and X ≠ Y
    i.e. at least one element of X is strictly smaller than the corresponding element of Y

    Safe Algorithm

    1. Assume m resource types, n processes
      define two matrices
        AVAILABLE = ( ... )   of length m

        TERMINATE = ( ... )   of length n

    2. initialize
        AVAILABLE = available resources

        TERMINATE[i] = false   for i = 1, 2, .. n ( i.e. no process is terminated )

    3. look for an i such that
        a. TERMINATE[i] = false     ( consider one element of TERMINATE )
        b. NEED[i] ≤ AVAILABLE     ( consider one row of NEED )

      if no such i exists, ( i.e. either all terminated or may lead to deadlock ), go to step 5

    4. Set
        TERMINATE[i] = ture     ( i.e. we can run the process to the end )
        AVAILABLE = AVAILABLE + ALLOCATED[i]     ( the terminated process can return all the resources )
      go to step 3

    5. if TERMINATE[i] == true for all i, the system is safe, otherwise it is not safe

    In our example, is the state safe ?

    Now suppose P5 wants 2 c ( e.g. printers )

    Suppose we grant the 2 c to P5, will the state be safe? Let's check:
    ALLOCATED=
    3 0 1 1
    0 1 0 0
    1 1 1 0
    1 1 0 1
    0 0 2 0
     NEED =
    1 1 0 0
    0 1 1 2
    3 1 0 0
    0 0 1 0
    2 1 0 0

    Available = ( 1000 )

    Then the remaining resources ( AVAILABLE ) are not sufficient to satisfy the max need of any process to run to the end. Thus the state is not safe. Therefore the request of P5 must not be granted at this moment.

    Note that an unsafe state does not necesarily lead to deadlock.

    Weaknesses of the Banker's Algorithm

  24. requires users to state their maximum needs in advance

  25. required fixed number of resources to allocate

  26. requires population of users fixed

  27. requires all tasks return all resources within a finite time