├── main.exe ├── README.md ├── libkoolplot.a ├── makefile ├── output.txt ├── plot.py ├── organizeFileForMatlabInput ├── Plotstream.h ├── PlotData.h ├── koolplot.h ├── new_main.cpp └── main.cpp /main.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/flextra19/PageReplacement_Algorithm_Compare/HEAD/main.exe -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PageReplacementAlgorithm_C- 2 | Implementing FIFO, OPT, LRU, ClockPRA algorithms in C++. 3 | -------------------------------------------------------------------------------- /libkoolplot.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/flextra19/PageReplacement_Algorithm_Compare/HEAD/libkoolplot.a -------------------------------------------------------------------------------- /makefile: -------------------------------------------------------------------------------- 1 | gcc.exe -x c++ -mwindows -o main.o main.c; 2 | gcc.exe -mwindows -o main.exe main.o -lkoolplot 3 | -lgdi32 -lcomdlg32 -luuid -loleaut32 -lole32 -lstdc++ -lsupc++ -------------------------------------------------------------------------------- /output.txt: -------------------------------------------------------------------------------- 1 | no : FIFO : LRU : OPT : CLOCK 2 | 2 : 823.219 : 901.114 : 822.906 : 823.219 3 | 3 : 738.912 : 889.167 : 738.06 : 738.205 4 | 4 : 659.225 : 873.345 : 654.138 : 656.991 5 | 5 : 581.985 : 852.552 : 571.897 : 577.79 6 | 6 : 509.286 : 826.422 : 492.178 : 501.965 7 | 7 : 440.683 : 792.523 : 416.837 : 429.226 8 | 8 : 376.896 : 751.802 : 345.706 : 360.242 9 | 9 : 317.583 : 702.957 : 278.896 : 296.074 10 | 10 : 264.023 : 647.598 : 216.545 : 237.471 11 | 11 : 215.717 : 584.537 : 163.617 : 185.189 12 | 12 : 173.953 : 517.224 : 121.233 : 140.142 13 | 13 : 137.545 : 447.322 : 86.204 : 103.996 14 | 14 : 106.785 : 375.259 : 61.89 : 75.821 15 | 15 : 81.947 : 303.307 : 45.352 : 55.5 16 | 16 : 62.743 : 232.441 : 34.421 : 41.67 17 | 17 : 47.981 : 166.052 : 27.334 : 32.521 18 | 18 : 37.501 : 106.773 : 23.451 : 27.193 19 | 19 : 30.249 : 61.605 : 21.389 : 24.005 20 | 20 : 24.685 : 34.663 : 20.544 : 21.816 21 | -------------------------------------------------------------------------------- /plot.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | file1 = open("output.txt","r") 5 | outputs = file1.readlines() 6 | fifo = [] 7 | lru = [] 8 | opt = [] 9 | clock = [] 10 | size = [] 11 | 12 | for i, output in enumerate(outputs) : 13 | if i > 0 : 14 | size.append(i+1) 15 | x = output.split(" : ") 16 | fifo.append(float(x[1])) 17 | lru.append(float(x[2])) 18 | opt.append(float(x[3])) 19 | clock.append(float(x[4])) 20 | 21 | print(size, fifo, opt, lru) 22 | 23 | 24 | plt.plot(size, fifo, linestyle='-', marker='*', color='red', label='FIFO') 25 | plt.plot(size, lru, linestyle='-', marker='*', color='blue', label='LRU') 26 | plt.plot(size, opt, linestyle='-', marker='*', color='purple', label='OPT') 27 | plt.plot(size, clock, linestyle='-', marker='*', color='green', label='CLOCK') 28 | 29 | plt.xlabel("Number of frames allocated") 30 | plt.ylabel("Page faults per 1000 references") 31 | plt.title("Comparison of Fixed-Allocation,Local Page Replacement Algorithms") 32 | 33 | plt.legend() 34 | 35 | plt.show() -------------------------------------------------------------------------------- /organizeFileForMatlabInput: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | 6 | using namespace std; 7 | 8 | int main() 9 | { 10 | string fileName; 11 | ifstream inFile; 12 | ofstream outFile; 13 | int num; 14 | int FIFOnum[30]; 15 | int LRUnum[30]; 16 | int OPTnum[30]; 17 | 18 | ofstream outFile1("outputFIFO.txt"); 19 | ofstream outFile2("outputLRU.txt"); 20 | ofstream outFile3("outputOPT.txt"); 21 | 22 | for(int i=1; i<31; i++) 23 | { 24 | fileName = "./myData/simdata-"+to_string(i)+".txt"; 25 | inFile.open(fileName); 26 | if(inFile)//open successfully 27 | { 28 | while(inFile >> num) 29 | { 30 | FIFOnum[i-1]=num; 31 | 32 | while(inFile >> num) 33 | { 34 | LRUnum[i-1]=num; 35 | 36 | while(inFile >> num) 37 | { 38 | OPTnum[i-1]=num; 39 | } 40 | }} 41 | inFile.close(); 42 | } 43 | else 44 | cout<<"\nError opening file: "<< fileName; 45 | } 46 | 47 | for(int i=0; i<30; i++) 48 | { 49 | outFile1< 18 | #include "PlotData.h" 19 | 20 | enum Rounding 21 | { 22 | DOWN, 23 | ANY, 24 | UP 25 | }; 26 | 27 | class Plotstream 28 | { 29 | public: 30 | Plotstream(const char * title = "2D Plot", int width = 640, int height = 0); 31 | void plot(const Plotdata & x, const Plotdata & y, int colour = GREEN); 32 | 33 | private: 34 | int winWidth, winHeight; 35 | int plotWidth, plotHeight; 36 | char * winTitle; 37 | double lo_x; double hi_x; // Highest and lowest data values in x 38 | double lo_y; double hi_y; // Highest and lowest points on y axis 39 | double x_range; double y_range; // Ranges of x values and y values 40 | double x_scale; double y_scale; // Scales of graph drawing to screen pixels 41 | bool plotStarted; // True while plotting is going on 42 | bool marked; // True when a marker is visible 43 | int lastX; 44 | int lastY; // Location of last marker drawn 45 | int colour; // Current drawing colour 46 | int lastColour; // Previous drawing colour 47 | 48 | /* Convert graph x value to screen coordinate */ 49 | int X(double x) const; 50 | /* Convert graph y value to screen coordinate */ 51 | int Y(double y) const; 52 | /* Convert screen coordinate to graph x value */ 53 | double plotX(int screenX) const; 54 | /* Convert screen coordinate to graph y value */ 55 | double plotY(int screenY) const; 56 | /* Round(est) double within one pixel in X */ 57 | Rounding nearRoundX(double & val, Rounding direction = ANY) const; 58 | /* Round(est) double within one pixel in Y */ 59 | Rounding nearRoundY(double & val, Rounding direction = ANY) const; 60 | /* True if values given are within x and y graph ranges */ 61 | bool withinRange(double xVal, double yVal) const; 62 | /* Draws the axes */ 63 | void drawAxes(); 64 | /* Draw the data */ 65 | void drawFunc(const Plotdata & x, const Plotdata & y); 66 | /* Watches the mouse and prints cursor coords if clicked */ 67 | bool watchMouse(); 68 | // Draw a marker at the current cursor position. Erase if erase is true 69 | void drawMarker(bool erase = false); 70 | // Draw a single point at the requested coordinates 71 | void drawSinglePoint(double xCoord, double yCoord); 72 | // Set new foreground colour, store last colour 73 | void setFgColor(int fgColour); 74 | // Reset foreground colour to last colour 75 | void resetFgColor(void); 76 | // Check whether the stream holds a command at this point (colour change 77 | // or single point), and handle the command if it does. 78 | void handleCommand( dataIterator &x_it, dataIterator &y_it); 79 | }; 80 | 81 | #endif 82 | -------------------------------------------------------------------------------- /PlotData.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Class Plotdata 3 | * 4 | * Stores data for one 2D plotting axis 5 | * 6 | * Provides operators and mathematical functions to map a Plotdata 7 | * object to another using unary and binary functions. 8 | * 9 | * Provides insertion and extraction of special commands into and from 10 | * Plotdata pairs as static operations. 11 | * Available commands are: 12 | * colourChange (to change the foreground colour of graphs) 13 | * singlePoint (to draw a single point of data as a special marker) 14 | * 15 | * Author: jlk 16 | * Version: 1.1 17 | * Date: July 2005 18 | * 19 | * This file is in the public domain and can be used without any 20 | * restriction. 21 | */ 22 | 23 | #ifndef Plotdata_H 24 | #define Plotdata_H 25 | 26 | #include 27 | #include 28 | #include 29 | 30 | using namespace std; 31 | 32 | 33 | // Unary function pointer type accepted by Plotdata (double parameter) 34 | typedef double (*Func)(double operand); 35 | #define func_ref Func & 36 | 37 | // Binary function pointer type accepted by Plotdata 38 | typedef double (*BinFunc)(double operand1, double operand2); 39 | #define binfunc_ref BinFunc & 40 | 41 | /** When true defines a range to be logarithmic */ 42 | typedef bool LogSpace; 43 | 44 | /** Define data iterator */ 45 | typedef vector::const_iterator dataIterator; 46 | 47 | /** Define range space: LOGarithmic or LINear */ 48 | typedef enum RangeSpace 49 | { 50 | LIN = false, 51 | LOG = true 52 | }; 53 | 54 | /** Remove case sensitivity for Plotdata class */ 55 | class Plotdata; 56 | typedef Plotdata plotdata; 57 | 58 | /** #define Plotdata & as a type (avoid using confusing '&' symbols in C) */ 59 | #define plotdata_ref plotdata & 60 | #define Plotdata_ref Plotdata & 61 | 62 | /** Number of points in a plot */ 63 | enum Grain 64 | { 65 | EXTRA_FINE = 901, 66 | FINE = 501, 67 | MEDIUM = 301, 68 | COARSE = 201, 69 | GROSS = 101 70 | }; 71 | 72 | const double NOPLOT = nan(""); // Any point of this value will not be plotted 73 | 74 | const int NOCOLOR = -1; 75 | const int RESETCOLOR = -2; 76 | 77 | class Plotdata 78 | { 79 | // -------------------------------------------------------------- 80 | // OPERATORS 81 | 82 | friend ostream & operator << (ostream & out, const Plotdata & pd); 83 | friend istream & operator >> (istream & in, Plotdata & pd); 84 | friend Plotdata operator + (double op1, const Plotdata & pd); 85 | friend Plotdata operator - (double op1, const Plotdata & pd); 86 | friend Plotdata operator * (double op1, const Plotdata & pd); 87 | friend Plotdata operator / (double op1, const Plotdata & pd); 88 | 89 | public: 90 | 91 | Plotdata operator + (const Plotdata &) const; 92 | Plotdata operator + (double) const; 93 | Plotdata operator - (const Plotdata &) const; 94 | Plotdata operator - (double) const; 95 | Plotdata operator * (const Plotdata &) const; 96 | Plotdata operator * (double) const; 97 | Plotdata operator / (const Plotdata &) const; 98 | Plotdata operator / (double) const; 99 | Plotdata operator ^ (double) const; 100 | Plotdata operator - ( ) const; 101 | Plotdata & operator << (const Plotdata &); // concatenate 102 | Plotdata & operator << (double); // add a double to the data 103 | 104 | // -------------------------------------------------------------- 105 | // Constructors 106 | inline Plotdata(): data(MEDIUM), userFunction(0), 107 | userBinFunction(0){} 108 | Plotdata(double min, double max); 109 | Plotdata(double min, double max, Grain grain); 110 | Plotdata(const double *array, int dataSize); 111 | inline Plotdata(size_t s): data(s), userFunction(0), 112 | userBinFunction(0){} 113 | inline Plotdata(vector d): data(d), 114 | userFunction(0), userBinFunction(0){}; 115 | // Member Functions 116 | void insert(const double array[], int dataSize); 117 | inline size_t size() const {return data.size();} 118 | inline void point(double p) {data.push_back(p);} 119 | void plotRange(double min, double max, 120 | int numPoints, LogSpace isLog = false); 121 | 122 | double min( )const; 123 | double max( )const; 124 | inline void clear( ) {data.clear();} 125 | inline const vector & getData() const{return data;} 126 | inline Func & userfunc() { return userFunction; } 127 | inline BinFunc & userBinfunc() { return userBinFunction; } 128 | 129 | Plotdata doFunc(Func aFunction) const; 130 | Plotdata doBinFunc(BinFunc aFunction, double operand2) const; 131 | Plotdata doBinFunc(double operand2) const; 132 | 133 | // -------------------------------------------------------------- 134 | // Class (static) functions 135 | 136 | // Retrieves the maximum of each of x and y in a Plotdata pair 137 | // Values corresponding to a NAN in the other Plotdata are not included 138 | static void max(const Plotdata &x, const Plotdata &y, 139 | double &xMax, double &yMax); 140 | // Retrieves the minimum of each of x and y in a Plotdata pair 141 | // Values corresponding to a NAN in the other Plotdata are not included 142 | static void min(const Plotdata &x, const Plotdata &y, 143 | double &xMin, double &yMin); 144 | // Retrieve the single point coordinates requested by special command 145 | static bool singlePoint(double &xCoord, double &yCoord, 146 | dataIterator &x, dataIterator &y); 147 | // Insert singlePoint coordinates into plot data pair 148 | static void singlePoint( Plotdata &x, Plotdata &y, 149 | double xCoord, double yCoord); 150 | // Retrieve the colour change requested by special command 151 | static int colorChange(dataIterator &x, dataIterator &y); 152 | // Insert special command for a colour change 153 | static void colorChange(Plotdata &x, Plotdata &y, int colour); 154 | // Insert special command to reset colour to before last change 155 | static void colorReset(Plotdata &x, Plotdata &y); 156 | // Return true if theColour is a valid colour or RESETCOLOR 157 | static bool isColor(int theColour); 158 | // Insert a single (invisible) point in plotdata pair 159 | static void soliton(Plotdata &x, Plotdata &y, double xval, double yval ); 160 | 161 | 162 | private: 163 | vector data; 164 | Func userFunction; // Any user-designed unary function 165 | BinFunc userBinFunction; // Any user-designed binary function 166 | }; 167 | 168 | #endif 169 | 170 | -------------------------------------------------------------------------------- /koolplot.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Header file to the "koolplot" library 3 | * 4 | * Provides basic, but also very simple-to-use 2D data plotting for 5 | * C and C++ programs using the WinBGIm graphics library. 6 | * 7 | * Example: 8 | * #include 9 | * int main() 10 | * { 11 | * plotdata x(0, 360), y = sin(x * M_PI / 180); 12 | * plot(x, y); 13 | * return 0; 14 | * } 15 | * 16 | * The "plot" function opens a window and displays the graph. The window 17 | * is closed when any key is pressed. 18 | * 19 | * Author: jlk 20 | * Version: 1.1 21 | * Date: July 2005 22 | * 23 | * This file is in the public domain and can be used without any 24 | * restriction. 25 | */ 26 | 27 | #ifndef KOOLPLOT_H 28 | #define KOOLPLOT_H 29 | 30 | #include "Plotstream.h" 31 | 32 | /* 33 | The following colour are already defined: 34 | RGB( 0, 0, 0 ); // BLACK 35 | RGB( 0, 0, 128); // BLUE 36 | RGB( 0, 128, 0 ); // GREEN 37 | RGB( 0, 128, 128 ); // CYAN 38 | RGB( 128, 0, 0 ); // RED 39 | RGB( 128, 0, 128 ); // MAGENTA 40 | RGB( 128, 128, 0 ); // BROWN 41 | RGB( 192, 192, 192 ); // LIGHTGREY 42 | RGB( 128, 128, 128 ); // DARKGREY 43 | RGB( 128, 128, 255 ); // LIGHTBLUE 44 | RGB( 128, 255, 128 ); // LIGHTGREEN 45 | RGB( 128, 255, 255 ); // LIGHTCYAN 46 | RGB( 255, 128, 128 ); // LIGHTRED 47 | RGB( 255, 128, 255 ); // LIGHTMAGENTA 48 | RGB( 255, 255, 0 ); // YELLOW 49 | RGB( 255, 255, 255 ); // WHITE 50 | 51 | Below, define some extra web colours useful in graphs 52 | */ 53 | 54 | const int CRIMSON = COLOR(220, 20, 60); 55 | const int CHOCOLATE = COLOR(210, 105, 30); 56 | const int GOLDENROD = COLOR(218, 165, 32); 57 | const int DARKORANGE = COLOR(255, 140, 0); 58 | const int REDRED = COLOR(255, 0, 0); 59 | const int DARKSLATEGRAY = COLOR(47, 79, 79); 60 | const int DEEPPINK = COLOR(255, 20, 147); 61 | const int TURQUOISE = COLOR(64, 224, 208); 62 | const int DARKVIOLET = COLOR(148, 0, 211); 63 | const int BEIGE = COLOR(245, 245, 220); 64 | const int MEDIUMSEAGREEN = COLOR(60, 179, 113); 65 | const int LIMEGREEN = COLOR(50, 205, 50); 66 | const int DARKGREEN = COLOR(0, 100, 0); 67 | const int MEDIUMBLUE = COLOR(0, 0, 205); 68 | const int BLUEBLUE = COLOR(0, 0, 255); 69 | const int DODGERBLUE = COLOR(30, 144, 255); 70 | 71 | /* -------------------------------------------------------- */ 72 | 73 | /** 74 | * Plot x..y data 75 | */ 76 | void plot(const plotdata_ref x, const plotdata_ref y); 77 | 78 | /** 79 | * Plot x..y data, specify curve colour 80 | */ 81 | void plot(const plotdata_ref x, const plotdata_ref y, int colour); 82 | 83 | /** 84 | * Plot x..y data, specify label 85 | */ 86 | void plot(const plotdata_ref x, const plotdata_ref y, char *label); 87 | 88 | /** 89 | * Plot x..y data, specify label and curve colour 90 | */ 91 | void plot(const plotdata_ref x, const plotdata_ref y, char *label, int colour); 92 | 93 | /** 94 | * Plot x..y data, specify curve colour, then label 95 | */ 96 | void plot(const plotdata_ref x, const plotdata_ref y, int colour, char *label); 97 | 98 | /** 99 | * Plot f(x) function (use together with f(x) function) 100 | */ 101 | void plot(const plotdata_ref x, func_ref f); 102 | 103 | /** 104 | * Plot f(x) function, specify curve colour (use together with f(x) function) 105 | */ 106 | void plot(const plotdata_ref x, func_ref f, int colour); 107 | 108 | /** 109 | * Plot f(x) function, specify label (use together with f(x) function) 110 | */ 111 | void plot(const plotdata_ref x, func_ref f, char *label); 112 | 113 | /** 114 | * Plot f(x) function, specify label and curve colour (use together with f(x) function) 115 | */ 116 | void plot(const plotdata_ref x, func_ref f, char *label, int colour); 117 | 118 | /** 119 | * Plot f(x) function, specify curve colour, then label (use together with f(x) function) 120 | */ 121 | void plot(const plotdata_ref x, func_ref f, int colour, char *label); 122 | 123 | /** 124 | * Installs a user-defined unary function of x as f(x) 125 | * example: plotdata x(-6, 6); 126 | * f(x) = sinc; // if sinc is a function defined by the user. 127 | * plot(x, f(x)); // Plots the graph of sinc(x) from -6 to 6 128 | */ 129 | func_ref f(plotdata_ref x); 130 | 131 | /** 132 | * Installs a user-defined binary function of x as f2(x) 133 | * example: plotdata x(-270, 270); 134 | * f2(x) = tanLessThan; // tanLessThan is a user-defined binary function. 135 | */ 136 | binfunc_ref f2(plotdata_ref x); 137 | 138 | /** 139 | * Calculates a user-defined binary function of x as y = f2(x, double_value) 140 | * example: plotdata x(-270, 270); 141 | * f2(x) = tanLessThan; // tanLessThan -user-defined binary function. 142 | * plot(x, f2(x, 10)); // Plots the graph of tanLessThan(x, 10) 143 | */ 144 | plotdata f2(const plotdata_ref x, double op2); 145 | 146 | /** 147 | * Adds next point to plotting data 148 | * -You add x and y coordinates in 2 separate plotdatas then call plot(). 149 | * plot() will draw the curve joining all the data points. 150 | * 151 | * @param pd the plotdata to add the point to 152 | * @param val value of the point to add 153 | */ 154 | 155 | void point(plotdata_ref pd, double val); 156 | 157 | /** 158 | * Adds next point to plotting data 159 | * 160 | * Same as above, but will take both axes and coordinates in one call. 161 | * 162 | * @param xAxis the x plotdata to add the point to 163 | * @param xCoord value of the x coordinate to add 164 | * @param yAxis the y plotdata to add the point to 165 | * @param yCoord value of the y coordinate to add 166 | */ 167 | void point(plotdata_ref xAxis, plotdata_ref yAxis, 168 | double xCoord, double yCoord); 169 | 170 | /** 171 | * Insert array of data points into plotting data 172 | * 173 | * @param axis the plotdata to add the points to 174 | * @param array The array of points to insert 175 | * @param numToInsert the number of points to add from the array. 176 | */ 177 | void insert(plotdata_ref axis, const double array[], int numToInsert); 178 | 179 | /** Sets the bottom left corner of the graph axes 180 | * xmin and ymin should be less than or equal to any coordinate 181 | * on the graph. 182 | */ 183 | void axesBotLeft(plotdata_ref x, plotdata_ref y, 184 | double xmin, double ymin); 185 | 186 | /** Sets the top right corner of the graph axes 187 | * xmax and ymax should be larger than or equal to any coordinate 188 | * on the graph. 189 | */ 190 | void axesTopRight(plotdata_ref x, plotdata_ref y, double xmax, double ymax); 191 | 192 | /** Adds visible mark to plot, centered on coordinates xpos, ypos */ 193 | void addMark(plotdata_ref x, plotdata_ref y, double xpos, double ypos); 194 | 195 | /** Adds visible mark at coordinates xpos, ypos, specify colour */ 196 | void addMark(plotdata_ref x, plotdata_ref y, double xpos, double ypos, int colour); 197 | 198 | /** Set colour of graph to requested colour */ 199 | void setColor(plotdata_ref x, plotdata_ref y, int colour); 200 | 201 | /** Reset colour of graph to last colour before change */ 202 | void resetColor(plotdata_ref x, plotdata_ref y); 203 | 204 | /** 205 | * Clear previous data from an axis. 206 | * @param the axis to clear data from 207 | */ 208 | void clear(plotdata_ref pd); 209 | 210 | /** 211 | * Breaks the current plot data at this point. 212 | * 213 | * Later, when plotting, the last point of the data previously 214 | * entered will not be joined to the first point of the next data. 215 | * This allows plotting more than one function on a single graph 216 | * using the "point" method of data entry. 217 | * @param xAxis the x data to break 218 | * @param yAxis the y data to break 219 | */ 220 | void breakplot(plotdata_ref x, plotdata_ref y); 221 | 222 | /** 223 | * Print plotting data on axis "anAxis" to standard output (for debugging) 224 | */ 225 | void printplotdata(const plotdata_ref anAxis); 226 | 227 | 228 | /**********************************************************************/ 229 | /*________ Maths functions that may be used to define functions ______*/ 230 | 231 | /* 232 | * Return new data, the sine of the original data 233 | * 234 | * @param pd the original plotdata 235 | */ 236 | plotdata sin(const plotdata_ref pd); 237 | 238 | /** 239 | * Return new data, the cosine of the original data 240 | * 241 | * @param pd the original plotdata 242 | */ 243 | plotdata cos(const plotdata_ref pd); 244 | 245 | /** 246 | * Return new data, the tan of the original data 247 | * 248 | * @param pd the original plotdata 249 | */ 250 | plotdata tan(const plotdata_ref pd); 251 | 252 | /** 253 | * Return new data, the asin of the original data 254 | * 255 | * @param pd the original plotdata 256 | */ 257 | plotdata asin(const plotdata_ref pd); 258 | 259 | /** 260 | * Return new data, the acos of the original data 261 | * 262 | * @param pd the original plotdata 263 | */ 264 | plotdata acos(const plotdata_ref pd); 265 | 266 | 267 | /** 268 | * Return new data, the atan of the original data 269 | * 270 | * @param pd the original plotdata 271 | */ 272 | plotdata atan(const plotdata_ref pd); 273 | 274 | /** 275 | * Return new data, the hyperbolic sine of the original data 276 | * 277 | * @param pd the original plotdata 278 | */ 279 | plotdata sinh(const plotdata_ref pd); 280 | 281 | /** 282 | * Return new data, the hyperbolic cosine of the original data 283 | * 284 | * @param pd the original plotdata 285 | */ 286 | plotdata cosh(const plotdata_ref pd); 287 | 288 | /** 289 | * Return new data, the hyperbolic tan of the original data 290 | * 291 | * @param pd the original plotdata 292 | */ 293 | plotdata tanh(const plotdata_ref pd); 294 | 295 | /** 296 | * Return new data, the square root of the original data 297 | * 298 | * @param pd the original plotdata 299 | */ 300 | plotdata sqrt(const plotdata_ref pd); 301 | 302 | /** 303 | * Return new data, the absolute value of the original data 304 | * 305 | * @param pd the original plotdata 306 | */ 307 | plotdata fabs(const plotdata_ref pd); 308 | 309 | /** 310 | * Return new data, the natural logarithm of the original data 311 | * 312 | * @param pd the original plotdata 313 | */ 314 | plotdata log(const plotdata_ref pd); 315 | 316 | /** 317 | * Return new data, the log (base 10)l of the original data 318 | * 319 | * @param pd the original plotdata 320 | */ 321 | plotdata log10(const plotdata_ref pd); 322 | 323 | /** 324 | * Return new data, the exponential of the original data 325 | * 326 | * @param pd the original plotdata 327 | */ 328 | plotdata exp(const plotdata_ref pd); 329 | 330 | /** 331 | * Return new data, the power "exp" of the original data 332 | * 333 | * @param pd the original plotdata 334 | * @param exp value of the exponent to raise the data to. 335 | */ 336 | plotdata pow(const plotdata_ref pd, double exp); 337 | 338 | #endif 339 | 340 | -------------------------------------------------------------------------------- /new_main.cpp: -------------------------------------------------------------------------------- 1 | 2 | // Main function 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | using namespace std; 16 | 17 | int totalExp = 1000; 18 | int totalPage = 1000; 19 | 20 | bool isExistsOL(vector frameList, string page)//func_lru,func_opt 21 | { 22 | for(int i=0; i < frameList.size(); i++) { 23 | if(frameList.at(i)==page) 24 | { 25 | return true; 26 | } 27 | } 28 | return false; 29 | } 30 | 31 | int func_fifo(deque pageList, int frameSize){ 32 | list frameList; 33 | list::iterator iter; 34 | int faults = 0; 35 | 36 | while(pageList.size()!=0){ 37 | // if set holds less pages than capacity. 38 | // insert page into the set one by one until the size of set reaches capacity or all page requests are processed. 39 | if(frameList.size()::iterator iter=frameList.begin(); iter!=frameList.end(); iter++){ 43 | if(*iter==pageList.front()){ 44 | flag = true; 45 | break; 46 | } 47 | } 48 | if(flag==false){ // if not exist, increase fault 49 | frameList.push_back(pageList.front()); 50 | pageList.pop_front(); // next page 51 | faults++; 52 | } 53 | else { 54 | pageList.pop_front(); // next page 55 | } 56 | } 57 | else if(frameList.size()==frameSize){ 58 | // if current page is present in set, do nothing. 59 | bool flag = false; 60 | for(iter=frameList.begin(); iter!=frameList.end(); iter++) { 61 | if(pageList.front()==*iter){ 62 | flag=true; 63 | break; 64 | } 65 | } 66 | if(flag==true) 67 | pageList.pop_front(); 68 | else { 69 | // remove the first page from the queue as it was the first to be entered in the memory 70 | // push back the current page into the set 71 | frameList.pop_front(); 72 | frameList.push_back(pageList.front()); 73 | pageList.pop_front(); 74 | faults++; 75 | } 76 | } 77 | } 78 | frameList.clear(); 79 | return faults; 80 | } 81 | 82 | int func_lru(deque pageList, int frameSize){ 83 | int faults = 0; 84 | vector frameList; 85 | vector popedList; 86 | vector::iterator iter; 87 | int decNum; 88 | 89 | while(pageList.size()!=0){ 90 | // if set holds less pages than capacity. 91 | // maintain the recent occurred page in a map called popedList 92 | if(frameList.size()0&&cnt>=0){ 118 | for(cnt=totalPage-pageList.size()-1; cnt>=0; cnt--) { // looping popedList upside down 119 | if(isExistsOL(frameList, popedList[cnt])) // if exist in frameList 120 | { 121 | bool flag = true; 122 | int num1 = totalPage-pageList.size()-1; // overalled page count 123 | // find the page in the set 124 | for(int i=num1; i<=cnt; i++) 125 | if(popedList[i]==popedList[cnt]){ 126 | flag = false; 127 | break; 128 | } 129 | 130 | // find the page in the set 131 | if(flag) { 132 | decNum--; 133 | changePage=popedList[cnt]; 134 | } 135 | else 136 | continue; 137 | } 138 | } 139 | } 140 | // exchange the frameSize 141 | // replace the found page with current page. 142 | for(iter=frameList.begin(); iter!=frameList.end(); iter++){ 143 | if(changePage==*iter){ 144 | frameList.erase(iter); 145 | frameList.insert(iter,pageList.front()); 146 | 147 | } 148 | } 149 | popedList.push_back(pageList.front()); 150 | pageList.pop_front(); 151 | faults++; 152 | } 153 | } 154 | } 155 | frameList.clear(); 156 | popedList.clear(); 157 | return faults; 158 | } 159 | 160 | int func_opt( deque pageList, int frameSize){ 161 | int faults = 0; 162 | vector frameList; 163 | vector::iterator iter; 164 | deque::iterator iter1; 165 | int decNum; 166 | 167 | while(pageList.size()!=0){ 168 | // if set holds less pages than capacity. 169 | // insert page into the set one by one until the size of set reaches capacity or all page requests are processed. 170 | if(frameList.size()0&&referFlag==true){ 194 | for(iter1=pageList.begin(); iter1!=pageList.end(); iter1++) 195 | { 196 | if(isExistsOL(frameList, *iter1)){ // (2) 197 | int num = pageList.size(); 198 | // check if page in memory 199 | int it=0; 200 | for(int i=(num-1); i pageList, int frameSize) { 234 | int frames[frameSize], use_flag[frameSize], fault, cnt, found, i, j; 235 | for(i=0; i distribution (10); 279 | 280 | std::cout << "some Poisson-distributed results (mean=10)" << endl; 281 | 282 | float fifo[21] = {0}; 283 | float lru[21] = {0}; 284 | float opt[21] = {0}; 285 | float clock[21] = {0}; 286 | 287 | for (int i=0; i pageList; //store the randomly generated pages 290 | for (int j=0; j 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | using namespace std; 16 | 17 | int totalExp = 1000; 18 | int totalPage = 1000; 19 | 20 | int FIFO( deque , int); 21 | int LRU( deque , int); 22 | int OPT( deque , int); 23 | int clockPRA(deque , int); 24 | bool isExists(list , string);//FIFO 25 | bool isExists(vector , string);//LRU,OPT 26 | bool isAppearOnce(vector &, string, int,int);//lRU 27 | bool isAppearOnce(deque &, string, int);//OPT 28 | 29 | int FIFO(deque myList, int pageFrame) 30 | { 31 | int countPageFaults = 0; 32 | list pageFrameList; 33 | list::iterator iter; 34 | 35 | while(myList.size()!=0) 36 | { 37 | // if set holds less pages than capacity. 38 | // insert page into the set one by one until the size of set reaches capacity or all page requests are processed. 39 | if(pageFrameList.size() myList, int pageFrame) 81 | { 82 | int countPageFaults = 0; 83 | vector pageFrameList; 84 | vector popedMyList; 85 | vector::iterator iter; 86 | int tempNum; 87 | 88 | while(myList.size()!=0) 89 | { 90 | // if set holds less pages than capacity. 91 | // maintain the recent occurred page in a map called popedMyList 92 | if(pageFrameList.size()0&&i>=0) 124 | { 125 | for(i=totalPage-myList.size()-1; i>=0; i--) // looping popedMyList upside down 126 | { 127 | if(isExists(pageFrameList, popedMyList[i])) // if exist in pageFrameList 128 | { 129 | int num1 = totalPage-myList.size()-1; // overalled page count 130 | if(isAppearOnce(popedMyList, popedMyList[i], num1, i)) // find the page in the set 131 | { 132 | tempNum--; 133 | changePage=popedMyList[i]; 134 | } 135 | else 136 | { 137 | continue; 138 | } 139 | } 140 | else 141 | { 142 | continue; 143 | } 144 | } 145 | } 146 | // exchange the pageFrame 147 | // replace the found page with current page. 148 | for(iter=pageFrameList.begin(); iter!=pageFrameList.end(); iter++) 149 | { 150 | if(changePage==*iter) 151 | { 152 | pageFrameList.erase(iter); 153 | pageFrameList.insert(iter,myList.front()); 154 | 155 | } 156 | } 157 | popedMyList.push_back(myList.front()); 158 | myList.pop_front(); 159 | countPageFaults++; 160 | } 161 | } 162 | } 163 | pageFrameList.clear(); 164 | popedMyList.clear(); 165 | return countPageFaults; 166 | } 167 | 168 | int OPT( deque myList, int pageFrame) 169 | { 170 | int countPageFaults = 0; 171 | vector pageFrameList; 172 | vector::iterator iter; 173 | deque::iterator iter1; 174 | int tempNum; 175 | 176 | while(myList.size()!=0) 177 | { 178 | // if set holds less pages than capacity. 179 | // insert page into the set one by one until the size of set reaches capacity or all page requests are processed. 180 | if(pageFrameList.size()0&&uselessPage==true) 212 | { 213 | for(iter1=myList.begin(); iter1!=myList.end(); iter1++) 214 | { 215 | if(isExists(pageFrameList, *iter1)) // (2) 216 | { 217 | int num = myList.size(); 218 | if(isAppearOnce(myList,*iter1, num)) 219 | { 220 | tempNum--; 221 | changePage=*iter1; 222 | } 223 | } 224 | else // (1) 225 | { 226 | changePage=*iter1; 227 | uselessPage = false; 228 | break; 229 | } 230 | } 231 | } 232 | //exchange the pageFrame 233 | for(iter=pageFrameList.begin(); iter!=pageFrameList.end(); iter++) 234 | { 235 | if(changePage==*iter) 236 | {pageFrameList.insert(iter,myList.front()); 237 | pageFrameList.erase(iter); 238 | } 239 | } 240 | myList.pop_front(); 241 | countPageFaults++; 242 | } 243 | } 244 | } 245 | pageFrameList.clear(); 246 | return countPageFaults; 247 | } 248 | 249 | int clockPRA(deque myList, int pageFrame) { 250 | int frames[pageFrame], use[pageFrame], fault, locat, found, i, j; 251 | for(i=0; i tempList, string iter1) 290 | { 291 | list::iterator iter; 292 | for(iter=tempList.begin(); iter!=tempList.end(); iter++) 293 | { 294 | if(*iter==iter1) 295 | return true; 296 | } 297 | return false; 298 | } 299 | 300 | bool isExists(vector tempList, string iter1)//LRU,OPT 301 | { 302 | vector::iterator iter; 303 | for(int i=0; i < tempList.size(); i++) 304 | { 305 | if(tempList.at(i)==iter1) 306 | { 307 | return true; 308 | } 309 | } 310 | return false; 311 | } 312 | 313 | bool isAppearOnce(vector &tempList, string iter1, int num, int curNum )//LRU will use this function 314 | { 315 | for(int i=num; i<=curNum; i++) 316 | { 317 | if(tempList[i]==iter1) 318 | { 319 | return false; 320 | } 321 | } 322 | return true; 323 | } 324 | 325 | bool isAppearOnce(deque &tempList, string iter1, int num)//OPT will use this function 326 | { 327 | int n=0; 328 | for(int i=(num-1); i distribution (10); 346 | 347 | std::cout << "some Poisson-distributed results (mean=10)" << endl; 348 | 349 | float fifo[21] = {0}; 350 | float lru[21] = {0}; 351 | float opt[21] = {0}; 352 | float clock[21] = {0}; 353 | 354 | for (int i=0; i myList; //store the randomly generated pages 357 | for (int j=0; j