(Latest Revision: Oct 11, 2007)

EXAMPLE FILE: bankersAlg



Deadlock Avoidance

Information Sheet on the Banker's Algorithm

===========================================================================
GLOSSARY:

n		        numProcs
m		        numRsrceTs
Available[j]		numAvailInst[rsrceT]
Max[i,j]		maxRequest[proc, rsrceT]
Allocation[i,j]		numAlloted[proc, rsrceT]
Need[i,j]		psbleFurthNeed[proc, rsrceT]
Work[j]		        willBeNumAvailInst[rsrceT]
Finish[i]		knowSureToFinish[proc]

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

i.e. psbleFurthNeed[proc, rsrceT]
  == maxRequest[proc, rsrceT] - numAlloted[proc, rsrceT]

===========================================================================
SAFETY ALGORITHM:

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

    FOR proc = 1 TO numProcs DO 
      knowSureToFinish[proc] = false ;
           
2.  Find a proc such that both the following are true:
    a.  knowSureToFinish[proc] == false ;
    b.  FOR rsrceT = 1 TO numRsrceTs
              psbleFurthNeed[proc, rsrceT] <= 
                 willBeNumAvailInst[rsrceT]
        
    IF no such proc exists, THEN go to step 4.

3.  FOR rsrceT = 1 TO numRsrceTs DO
      willBeNumAvailInst[rsrceT] =
           willBeNumAvailInst[rsrceT] + 
               numAlloted[proc, rsrceT] ;

    knowSureToFinish[proc] = true ;

    GO TO step 2
    
4.  IF    FOR proc = 1 TO numProcs,  
                  knowSureToFinish[proc] == true ,
    THEN  the system is safe,
    ELSE  it is not safe.

===========================================================================
THE BANKER'S ALGORITHM:

request[proc, rsrceT] = number of instances of resource type rsrceT 
currently requested by process proc.

1.  IF    FOR rsrceT = 1 TO numRsrceTs 
            request[proc, rsrceT] <= psbleFurthNeed[proc, rsrceT]
    THEN  GOTO step 2
    ELSE  raise error: "exceeded max claim"

2.  IF    FOR rsrceT = 1 TO numRsrceTs 
                        request[proc, rsrceT] <= numAvailInst[rsrceT]
    THEN  GOTO step 3
    ELSE  Block(proc) until request can be granted

3.  FOR rsrceT = 1 TO numRsrceTs DO
    BEGIN
      tmpNumAvailInst[rsrceT] = 
        numAvailInst[rsrceT] - request[proc, rsrceT] ;
      tmpNumAlloted[proc, rsrceT] =
        numAlloted[proc, rsrceT] + request[proc, rsrceT] ;
      tmpPsbleFurthNeed[proc, rsrceT] =
        psbleFurthNeed[proc, rsrceT] - request[proc, rsrceT] ;
    END

4. Now run the safety algorithm on a copy of the current information 
   about the system, except with numAvailInst =
   tmpNumAvailInst, with numAlloted = tmpNumAlloted, and with the
   row of psbleFurthNeed corresponding to process proc
   changed to the corresponding row of tmpPsbleFurthNeed 

   IF    "YES" is the result of running the safety algorithm
   THEN  the current request can be granted
   ELSE  the process making the current request must wait