EXAMPLE FILE: ddLckAlg




Deadlock Detection:

If there is one instance of each resource type, check for a
cycle in the resource allocation or wait-for graph.

If there are multiple instances of resource types, the
following algorithm will detect deadlock:

1.  FOR rsrceT = 1 TO numRsrceTs DO
      couldBeAvInst[rsrceT] = 
          numAvailInst[rsrceT] ;

    FOR proc = 1 TO numProcs DO
      IF    FOR rsrceT = 1 TO numRsrceTs
              numAlloted[proc, rsrceT] == 0
      THEN   knowCanFinish[proc] = true  // can't be deadlocked
      ELSE   knowCanFinish[proc] = false ;
           
2.  Find a proc such that both
    a.  knowCanFinish[proc] == false
    b.  FOR rsrceT = 1 TO numRsrceTs
          request[proc, rsrceT] <= couldBeAvInst[rsrceT] ;

    IF    no such proc exists
    THEN  GOTO step 4.

3.  /* pretend that this process terminates after receiving its
       current request, without making any additional requests. */

    FOR rsrceT = 1 TO numRsrceTs
      couldBeAvInst[rsrceT] =
         couldBeAvInst[rsrceT] + numAlloted[proc, rsrceT] ;

    knowCanFinish[proc] = true ;

    GOTO step 2.

4.  IF    FOR proc = 1 TO numProcs
            knowCanFinish[proc] == true
    THEN  system is NOT in deadlock
    ELSE  system is deadlocked

    (furthermore, each proc for which knowCanFinish[proc] ==
     false is deadlocked -- waiting for another process to
     release a resource that it will never release.  The system
     cannot possibly free up enough resources to satisfy ANY of
     the requests of ANY of the proc's with knowCanFinish[proc]
     == false -- even if all processes in the system are willing
     to make no further requests after receiving the allocation
     of their current request.)