Docstoc

Banker

Document Sample
Banker Powered By Docstoc
					                                              Banker's Algorithm
                                              Deadlock Avoidance
Must maintain the following data structures where there are M resource types and N processes.

          AVAIL[M] = Array of M system resources (how many of each type are on the system).
          MAX[N,M] = Matrix of maximum resources needed for each of N processes.
          ALLOC[N,M] = Matrix of all resources currently allocated to each process.
          NEEDS[N,M] = Matrix of all possible future needed resources for each process.

When a new process comes along, it must provide it's MAX values. The system should then
copy those values into the NEEDS matrix and place zeros in the ALLOC matrix for the new
process. You should note that at any point in time MAX = ALLOC + NEEDS.

Now, when a process P needs a resource R, it must request it through the Banker's Algorithm:

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
If (Request (P,R) <= NEEDS[P,R])                                                                 // is it a valid request?
           If (Request(P,R) <= AVAIL[R])                                                         // is the resource available?
                      AVAIL[R] = AVAIL[R] - 1;
                      ALLOC[P,R] = ALLOC[P,R] + 1;
                      NEEDS[P,R] = NEEDS[P,R] - 1;
                      If (Safe() )
                                Allocate Resources;
                      Else
                                Restore AVAIL, ALLOC and NEEDS;
                                Wait and try again later;
           Else
                      Wait for AVAIL to increase and try again later;
Else
           Request was in error (Bigger than MAX);
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

The Safety Algorithm maintains an array FINISHED[N] of bits indicating if each process can
finish running under the current state of the system.

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Safe()
For (I=0; I < N; I++)
           FINISH[I] = FALSE;
While (There exists an I where (FINISH[I] = = FALSE) && (NEEDS[I,*] <= AVAIL[*]))
           AVAIL[*] = AVAIL[*] + ALLOC[I,*];
           FINISH[I] = TRUE;
If (FINISH[I] = = TRUE for all I)
           Return (SAFE);
Else
           Return (UNSAFE);
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:5
posted:8/13/2012
language:English
pages:1