Skip to content
uconn code

uconn codeMemory placement simulator

Memory placement simulator

CSE 258 · 2003 · C++ (MFC)

The companion to the page replacement simulator: jobs arrive and leave, each placement strategy decides which free hole a job goes into, and the simulator reports where every job landed.

Line tokenizing used Richard Case's MFC CToken class (1999), which isn't shown.

Source code

2 files, 746 lines, exactly as written apart from line endings.

storage.cpp · 77 lines
//Storage.cpp
//Stephen Bassoli
//CSE 258

#include "Memory.h"
#include <afx.h>
#include <ctype.h>
#include <fstream.h>
#include "Token.h"

int main()
{
	char line[200], filename[100], ofilename[100];	
	int i = 0, choice;
	CString lines[200];	
	cout << "Type the name of the input file: ";
	cin >> filename;
	cout << "Type the name of the output file: ";
	cin >> ofilename;
	cout << "1) First-fit\n2) Next-fit\n3) Best-fit\n4) Worst-fit\nSelect fit method: ";
	cin >> choice;
	if ((choice < 1)||(choice > 4))
		cout << "Invalid choice, First-fit will be used\n";
	ifstream infile(filename);
	ofstream outfile(ofilename);
	outfile.clear();

	while (infile.getline(line,200,'\n'))
		lines[i++] = line;
	Memory memory(lines, i, outfile);
	int j =0;
	while ((memory.numProgsLeft)||(memory.currPrograms))
	{
		memory.fixSizes();
		switch (choice)
		{
		case 1:
			if (memory.loadFirstFit());
			else
				memory.clockEdge();
			break;
		case 2:
			if (memory.loadNextFit());
			else
				memory.clockEdge();
			break;	
		case 3:
			if (memory.loadBestFit());
			else
				memory.clockEdge();
			break;
		case 4:
			if (memory.loadWorstFit());
			else
				memory.clockEdge();
			break;
		default:
			if (memory.loadFirstFit());
			else
				memory.clockEdge();
			break;
		}
		memory.fixSizes();
		outfile << endl;
		outfile << "Step: " << memory.stepsDone << endl;
		memory.printProgs();
		memory.printHoles();
		outfile << endl;
		j++;

	}
	outfile.close();
	cout << "Jobs completed in "<< memory.stepsDone << " steps." << endl;
	cout << "Results have been written to " << ofilename << endl;

	return 0;
}

storage.cpp as plain text

memory.h · 669 lines

memory.h as plain text