📄 project1.cpp
字号:
#pragma hdrstop
#include <condefs.h>
#include <iostream.h> // C++ standard I/O
#include <fstream.h> // C++ file I/O
#include <string> // C++ string functions
#include "Banker.h" // defines the banker
//---------------------------------------------------------------------------
USEUNIT("Banker.cpp");
//---------------------------------------------------------------------------
ofstream ofsLog1;
ofstream ofsLog2;
void tryRequest(Banker *pBanker, int iPID, int iF, int iP, int iM)
{
cout << "PID " << iPID << " requests (";
cout << iF << ", " << iP << ", " << iM << ") ";
// Try to request memory; see if the Banker can be spoofed
// into allowing an unsafe system condition
try
{
pBanker->request(iPID, iF, iP, iM);
cout << "...made it." << endl;
}
catch(BankerUnsafePidException)
{
cout << "...UNSAFE: Invalid PID." << endl;
}
catch(BankerUnsafeContractException)
{
cout << "...UNSAFE: Violates PID contract." << endl;
}
catch(BankerUnsafeTotalException)
{
cout << "...UNSAFE: Exceeds total system resources." << endl;
}
catch(BankerUnsafeCommittedException)
{
cout << "...UNSAFE: Resources would be overcommitted." << endl;
}
}
void tryMemoryTest(Banker *pBanker, bool bHello, bool bLimits, bool bRequest)
{
static int i=0;
cout << "BEGIN MEMORY TEST " << i << endl;
cout << "pBanker points to " << pBanker << endl;
if(bHello==true)
{
cout << "calling helloWorld()" << endl;
pBanker->helloWorld();
cout << "helloWorld() succeeded." << endl;
}
if(bLimits==true)
{
cout << "calling showLimits()" << endl;
pBanker->showLimits();
cout << "...showLimits() succeeded." << endl;
}
if(bRequest==true)
{
cout << "calling request()" << endl;
pBanker->request(1, 0, 0, 0);
cout << "...request() succeeded." << endl;
}
cout << "...completed memory test " << i++ << endl;
cout << endl << endl;
}
#pragma argsused
int main(int argc, char **argv)
{
string sLog1 = "bankerDebug1.txt";
string sLog2 = "bankerDebug2.txt";
ofsLog1.open(sLog1.c_str());
ofsLog2.open(sLog2.c_str());
Banker * pBanker = new Banker(10, 5, 7); // create the banker
// assign PIDs and max resources required for each
pBanker->openAccount(0, 7, 5, 3);
pBanker->openAccount(1, 3, 2, 2);
pBanker->openAccount(2, 9, 0, 2);
pBanker->openAccount(3, 2, 2, 2);
pBanker->openAccount(4, 4, 3, 3);
// try series of requests as suggested by the textbook, page 222
tryRequest(pBanker, 0, 0, 1, 0);
tryRequest(pBanker, 1, 2, 0, 0);
tryRequest(pBanker, 2, 3, 0, 2);
tryRequest(pBanker, 3, 2, 1, 1);
tryRequest(pBanker, 4, 0, 0, 2);
tryRequest(pBanker, 1, 1, 0, 2);
tryRequest(pBanker, 4, 3, 3, 0);
tryRequest(pBanker, 0, 0, 2, 0);
cout << "...done with Silberschatz part..." << endl;
cout << endl << endl;
cout << "Begin trying other things." << endl;
tryRequest(pBanker, 1, 0, 0, 0);
tryRequest(pBanker, 9, 0, 0, 0);
tryRequest(pBanker, 1, 0, 4, 0);
cout << endl << endl << endl;
cout << "Begining memory tests; pBanker initially " << pBanker << endl;
cout << endl;
tryMemoryTest(pBanker, true, true, true); // test 0
delete pBanker;
tryMemoryTest(pBanker, true, true, false); // test 1
pBanker = (Banker *)new int[sizeof (Banker)];
tryMemoryTest(pBanker, true, true, false); // test 2
pBanker = (Banker *)2346735687;
tryMemoryTest(pBanker, true, false, false); // test 3
return 0; // success
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -