Skip to content

Instantly share code, notes, and snippets.

@sheki
Created November 21, 2015 03:59
Show Gist options
  • Select an option

  • Save sheki/3a2cb8d559583702aea5 to your computer and use it in GitHub Desktop.

Select an option

Save sheki/3a2cb8d559583702aea5 to your computer and use it in GitHub Desktop.
goodcode
#include <iostream>
#include <cstring>
#include <sstream>
#include <cstdio>
#include <string>
#include <vector>
#include <cctype>
#include <queue>
#include <map>
#include <fstream>
#include <iomanip>
#include <stdlib.h>
#include <string>
#include <ctype.h>
#include <unistd.h>
using namespace std;
int mapcount = 0;
int unmapcount = 0;
typedef struct instr{
int rw;
unsigned int page;
}instruction;
vector<int> randvals;
vector<unsigned int> rev_frametable;
vector<instr*> instructions;
vector<unsigned int> frametable;
vector<unsigned int> pagetable(64,0);
unsigned int isPresent(unsigned int p) {
return ((p >> 31) | 0);
}
void setFrameNumber(unsigned int& p, int f) {
p = (p & 0xf0000000) | f;
}
unsigned int getFrameNumber(unsigned int p) {
return ((p << 4) >> 4);
}
int ofs = 0;
int algoType = 0, optType=0;
int quant1;
int myrandom(int burst) {
if (ofs > randvals.size()){
ofs = 0;
}
return (1 + (randvals[ofs++] % burst));
}
int main(int argc, char* argv[]) {
ifstream iFile, rFile;
bool verbose = 0;
int numFrames;
string line;
quant1=0;
int aflag, bflag;
const char* cvalue;
char c;
if (argc < 3) {
printf("Invalid number of parameters.\n");
return 1;
}
while ((c = getopt (argc, argv, "aof:")) != -1)
switch (c){
case 'a':
aflag = 1;
break;
case 'o':
bflag = 1;
break;
case 'f':
cvalue = optarg;
numFrames = atoi(cvalue);
break;
case '?':
if (optopt == 'c')
fprintf (stderr, "Option -%c requires an argument.\n", optopt);
else if (isprint (optopt))
fprintf (stderr, "Unknown option `-%c'.\n", optopt);
else
fprintf (stderr,
"Unknown option character `\\x%x'.\n",
optopt);
return 1;
default:
abort ();
}
iFile.open(argv[argc - 2]);
if (iFile == NULL) {
fprintf(stderr, "Instruction file not found.\n");
return 1;
}
rFile.open(argv[argc - 1]);
if (rFile == NULL) {
fprintf(stderr, "Random file not found.\n");
return 1;
}
while(std::getline(rFile, line)){
const char * c = line.c_str();
int num = atoi(c);
randvals.push_back(num);
}
randvals.erase(randvals.begin());
while(std::getline(iFile, line)){
int i;
unsigned int p;
instr ins;
const char * buf = line.c_str();
if (buf[0] != '#'){
sscanf(buf, "%d%u", &i, &p);
ins.page = p;
ins.rw = i;
instructions.push_back(&ins);
}
}
cout << instructions.size() <<endl;
for (int i=0; i<instructions.size();i++){
cout << instructions[i]->rw<<endl;
}
rev_frametable = vector <unsigned int>(numFrames, -1);
int instrCount = 0;
numFrames = 15;
while(instructions.size()>0){
instr* ins = instructions.front();
unsigned int pageIndex = ins->page;
instructions.erase(instructions.begin());
cout << ins->rw<<" "<<pageIndex<<endl;
int frameNo = frametable.size();
cout << frametable.size()<<endl;
if(!isPresent(pagetable[pageIndex])){
if(frametable.size()<numFrames){
frametable.push_back(frameNo);
rev_frametable[frameNo] = pageIndex;
mapcount++;
setFrameNumber(pageIndex,frameNo);
cout << pageIndex<<" "<<getFrameNumber(pageIndex) <<endl;
}
else{
rev_frametable[0] = pageIndex ;
}
}
else{
cout<<"hello"<<endl;
}
instrCount++;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment