www.pudn.com > 模拟银行家算法.zip > Banker.cpp


#include  
#include  
#include "Banker.h" 
 
extern ofstream ofsLog1; 
extern ofstream ofsLog2; 
 
Banker::Banker(int i1, int i2, int i3) 
{ 
  ofsLog2 << "constructing banker" << endl; 
  RMax.iFiles = i1; 
  RMax.iPages = i2; 
  RMax.iMutex = i3; 
  RInUse.iFiles = 0; 
  RInUse.iPages = 0; 
  RInUse.iMutex = 0; 
 
  ofsLog2 << "Banker:  RMax fpm " << i1 << i2 << i3 << endl; 
} 
 
void Banker:: helloWorld() 
{ 
  cout << "Banker:: Hello World!" << endl; 
} 
 
void Banker :: showLimits() 
{ 
  cout << "Banker:: showLimits currently "; 
  cout << "(" << RInUse.iFiles << ", " << RInUse.iPages << ", " << RInUse.iMutex << ")"; 
  cout << " max "; 
  cout << "(" << RMax.iFiles   << ", " <<   RMax.iPages << ", " <<   RMax.iMutex << ")"; 
  cout << endl; 
} 
 
void Banker::closeAccount(int iPid) 
{ 
    ofsLog1 << "Banker::close " << iPid << "..."; 
    ofsLog2 << "Banker::close " << iPid << "..."; 
 
    if(RMapMax.find(iPid)== RMapMax.end()) 
    { 
        ofsLog1 << "...NO SUCH PID." << endl; 
        ofsLog2 << "...NO SUCH PID." << endl; 
        throw BankerUnsafePidException(); 
    } 
    ofsLog1 << "...found it." << endl; 
    ofsLog2 << "...found it." << endl; 
 
    RInUse.iFiles -= RMapInUse[iPid].iFiles; 
    RInUse.iPages -= RMapInUse[iPid].iPages; 
    RInUse.iMutex -= RMapInUse[iPid].iMutex; 
} 
 
void Banker::openAccount(int iPid, int iF, int iP, int iM) 
{ 
    ofsLog1 << "Banker::openAccount() "; 
    ofsLog1 << iPid << " " << iF << " " << iP << " " << iM << endl; 
    ofsLog2 << "Banker::openAccount() "; 
    ofsLog2 << iPid << " " << iF << " " << iP << " " << iM << endl; 
 
    RMapMax[iPid].iFiles = iF; 
    RMapMax[iPid].iPages = iP; 
    RMapMax[iPid].iMutex = iM; 
 
    RMapInUse[iPid].iFiles = 0; 
    RMapInUse[iPid].iPages = 0; 
    RMapInUse[iPid].iMutex = 0; 
 
    ofsLog2 << "Banker::openAccount for pid " << iPid << endl; 
    ofsLog2 << "...max files " << RMapMax[iPid].iFiles << endl; 
    ofsLog2 << "...max pages " << RMapMax[iPid].iPages << endl; 
    ofsLog2 << "...max mutex " << RMapMax[iPid].iMutex << endl; 
    ofsLog2 << "...current files " << RMapInUse[iPid].iFiles << endl; 
    ofsLog2 << "...current pages " << RMapInUse[iPid].iPages << endl; 
    ofsLog2 << "...current mutex " << RMapInUse[iPid].iMutex << endl; 
 
    ofsLog2 << "Banker::openAccount...RMapMax.size now " << RMapMax.size() << endl; 
} 
 
void cout3(int i1, int i2, int i3) 
{ 
  ofsLog2 << " " << i1 << " " << i2 << " " << i3 << " "; 
} 
 
void Banker::isSafe(int iPid, int iF, int iP, int iM) 
{ 
	// take your best shot... 
 
} 
 
void Banker::request(int iPid, int iF, int iP, int iM) 
{ 
  ofsLog2 << "      Banker::request()" << iPid; 
  ofsLog2 << " f " << iF << " p " << iP << " m " << iM << endl; 
  ofsLog1 << "      Banker::request()" << iPid; 
  ofsLog1 << " f " << iF << " p " << iP << " m " << iM << endl; 
 
  isSafe(iPid, iF, iP, iM); 
 
  RMapInUse[iPid].iFiles += iF; 
  RMapInUse[iPid].iPages += iP; 
  RMapInUse[iPid].iMutex += iM; 
 
  RInUse.iFiles += iF; 
  RInUse.iPages += iP; 
  RInUse.iMutex += iM; 
 
  ofsLog2 << "      NOW USING " << RInUse.iFiles; 
  ofsLog2 << " " << RInUse.iPages; 
  ofsLog2 << " " << RInUse.iMutex << endl; 
  ofsLog1 << "      NOW USING " << RInUse.iFiles; 
  ofsLog1 << " " << RInUse.iPages; 
  ofsLog1 << " " << RInUse.iMutex << endl; 
} 
 
Banker :: ~Banker() {}