Information Technology Reference
In-Depth Information
//
//Astateissafeiffthereexistsasafesequenceofgrants
//thatwouldallowallthreadstoeventuallyreceivetheir
//maximumresourceneeds
//
bool
ResourceMgr::isSafe()
{
intj;
inttoBeAvail[]=copyavail[];
intneed[][]=max[][]-alloc[][]; //need[i][j]initializedtomax[i][j]-alloc[i][j]
boolfinish[]=[false,false,false,...]; //finish[j]istrueifthreadjisguaranteedtofinish
while(true){
j=anythreadIDs.t.((finish[j]==false)&&(foralli:need[i][j]<=toBeAvail[i]));
if(nosuchjexists){
if(forallj:finish[j]==true){
returntrue;
}
else{
returnfalse;
}
}
else{
//Threadjwilleventuallyfinishandreturnitscurrent
//allocationtothepool
finish[j]=true;
foralli: toBeAvail[i]=toBeAvail[i]+alloc[i][j];
}
}
}
//
//Hypotheticallygrantrequestandseeifresultingstate
//issafe.
//
bool
ResourceMgr::wouldBeSafe(intresourceID,intthreadID)
{
boolret=false;
avail[resourceID]--;
alloc[resourceID][threadID]++;
if(isSafe()){
ret=true;
}
avail[resourceID]++;
alloc[resourceID][threadID]--;
returnret;
}
Figure6.11: Banker's algorithm test on system state (pseudo-code.) Fig-
ure 6.10 provides the high-level pseudo-code of the banker's algorithm and and
Figure 6.9 defines the state on which these tests work.
Search WWH ::




Custom Search