uconn codePage replacement simulator
Page replacement simulator
CSE 258 · 2003 · C++ (MFC)
An operating-systems exercise: read a stream of page references from a file, pick a replacement strategy, and track which page is evicted each time memory is full and how many page faults the strategy causes.
Line tokenizing used Richard Case's MFC CToken class (1999), which isn't shown.
Source code
2 files, 454 lines, exactly as written apart from line endings.
paging.cpp · 63 lines
//Page replacement strategies main program
//Stephen Bassoli
//CSE 258
#include <fstream.h>
#include <stdio.h>
#include "stdafx.h"
#include "Mem.h"
#include "Token.h"
int main()
{
char filename[100]; //= "hw3-1.txt";
char ofilename[100]; //= "output.txt";
char line[200];
int choice;
CString cline;
int** jobPages = new int*[30];
for (int x = 0 ; x < 30 ; x++)
jobPages[x] = new int[200];
int i = 0, j = 1;
cout << "Enter the name of the input file: ";
cin >> filename;
cout << "Enter the name of the output file: ";
cin >> ofilename;
cout << "What kind of replacement strategy?\n1) Random\n2) FIFO\n3) LRU\n4) LFU\n";
cin >> choice;
ifstream infile(filename);
ofstream outfile(ofilename);
while (infile.getline(line,200,'\n'))
{
if (line[0] == 'J')
{
jobPages[i][0] = j - 1;
i++;
j = 1;
}
else
{
cline = line;
CToken tok(cline);
tok.SetToken(" ");
tok.GetNextToken();
jobPages[i][j] = atoi(tok.GetNextToken());
j++;
}
}
jobPages[i][0] = j - 1;
jobPages[0][0] = i;
Mem memory(jobPages,outfile);
memory.loadJob();
memory.runJobsWith(choice - 1);
outfile << endl;
for (int k = 1; k <= i ; k++)
{
outfile << "Number of steps for job " << k << ": " << memory.stepsPerJob[k] << endl;
outfile << "Number of page faults for job " << k << ": " << memory.faultsPerJob[k] << endl << endl;
}
outfile << endl << "Total number of page faults: " << memory.numOfPageFaults << endl;
cout << "Steps taken to complete: " << memory.step << endl;
cout << "Total number of page faults: " << memory.numOfPageFaults << endl;
outfile.close();
return 0;
}