void nextFit(PCBStruct *newProcess,vector <string> &memoryTable,vector <Spans*> &spanVector) { findSpans(memoryTable,spanVector); int spacesNeeded = newProcess->getMemoryNeeded(); int difference = 0; int range = 0; Spans* current = NULL; for(int i = 0; i < spanVector.size();i++) { current = spanVector[i]; difference = current->getEnding() - current->getBeginning() + 1; if(difference >= spacesNeeded) { range = current->getBeginning() + spacesNeeded; for(int x = current->getBeginning(); x <= spacesNeeded; x++) { memoryTable[x] = newProcess->getProcessName(); } } } findSpans(memoryTable,spanVector); }
//joins contigious spans together void coalescing(vector <Spans*> spansVector) { int firstBeginning = 0; int firstEnding = 0; int secondBeginning = 0; int secondEnding = 0; Spans* current = NULL; Spans* next = NULL; for(int i = 0; i < spansVector.size();i++) { current = spansVector[i]; next = spansVector[i+1]; firstBeginning = current->getBeginning(); firstEnding = current->getEnding(); secondBeginning = next->getBeginning(); secondEnding = next->getEnding(); if(firstEnding+1 == secondBeginning) { spansVector[i] = NULL; spansVector[i+1] = NULL; Spans* newSpan = new Spans(firstBeginning,firstEnding,true); newSpan->setBeginning(firstBeginning); newSpan->setEnding(secondEnding); spansVector[i] = newSpan; removeNULLsFromVector(spansVector); } } }
void compareSpacesNeededToSpan(PCBStruct *newProcess,vector <string> &memoryTable,vector <Spans*> &spanVector) { int spacesNeeded = newProcess->getMemoryNeeded(); int spacesDifference = 0; int spacesInSpan = 0; vector<int> spacesDifferenceVector; Spans* current = NULL; for(int i = 0; i < spanVector.size();i++) { current = spanVector[i]; spacesInSpan = current->getEnding() - current->getBeginning() + 1; if(spacesInSpan > spacesNeeded) { spacesDifference = spacesInSpan-spacesNeeded; spacesDifferenceVector.push_back(spacesDifference); } else { spacesDifferenceVector.push_back(-1); } } bestFit(newProcess,memoryTable,spanVector,spacesDifferenceVector); worstFit(newProcess,memoryTable,spanVector,spacesDifferenceVector); }
void organizeVector(vector <Spans*> vectorToOrganize) { int sizeOfVector = vectorToOrganize.size(); Spans* smaller = NULL; Spans* currentSpan = NULL; Spans* nextSpan = NULL; for(int i = 0; i < sizeOfVector; i++) { currentSpan = vectorToOrganize[i]; nextSpan = vectorToOrganize[i]; for(int x = 0; x < sizeOfVector; x++) { if(nextSpan->getBeginning() < currentSpan->getBeginning()) { smaller = nextSpan; vectorToOrganize[x+1] = vectorToOrganize[x]; vectorToOrganize[x] = smaller; } } } }
void worstFit(PCBStruct *newProcess,vector <string> &memoryTable,vector <Spans*> &spanVector, vector<int> differenceVector) { int spacesNeeded = newProcess->getMemoryNeeded(); int biggest = differenceVector[0]; int range = 0; int index = 0; Spans* biggestSpan = NULL; for(int i = 1; i < differenceVector.size();i++) { if(differenceVector[i] > biggest) biggest = differenceVector[i]; biggestSpan = spanVector[i]; index = i; } range = biggestSpan->getBeginning() + spacesNeeded; for(int x = biggestSpan->getBeginning(); x < range; x++) { memoryTable[x] = newProcess->getProcessName(); } }
bool firstFit(PCBStruct *newProcess,vector <string> &memoryTable,vector <Spans*> &spanVector) { int spacesNeeded = newProcess->getMemoryNeeded(); int sizeOfTable = memoryTable.size(); int sizeOfVector = spanVector.size(); int difference = 0; int range = 0; int counter = 0; Spans* currentSpan = NULL; for(int a = 0; a < sizeOfVector; a++) { currentSpan = spanVector[a]; difference = currentSpan->getEnding() - currentSpan->getBeginning() + 1; range = currentSpan->getBeginning() + spacesNeeded; // cout << "needed: " << spacesNeeded << "\tdifference: " << difference << endl; if(difference >= spacesNeeded) { counter = currentSpan->getBeginning(); currentSpan->setName(newProcess->getProcessName()); cout << "255" << endl; for(int i = currentSpan->getBeginning(); i < range; i++) { memoryTable[i] = newProcess->getProcessName(); } return true; } else { return false; } } //cout << "259" << endl << endl << endl; // showMemoryTable(newProcess,memoryTable,spanVector); }
void matchVectors(vector <string> &memoryTable, vector <Spans*> spansVector) { int sizeOfTable = memoryTable.size(); int sizeOfVector = spansVector.size(); Spans* current = NULL; for(int i = 0; i < sizeOfVector; i++) { current = spansVector[i]; for(int x = current->getBeginning(); x <= current->getEnding(); x++) { memoryTable[x] = current->getName(); } } }
//joins all contiguious spans together at end of table void condensing(vector <Spans*> spanVector, vector <string> memoryTable) { coalescing(spanVector); int totalFreeSpaces = 0; int difference = 0; Spans* current = NULL; for(int i = 0; i < spanVector.size(); i++) { current = spanVector[i]; difference = current->getEnding() - current->getBeginning() + 1; totalFreeSpaces = totalFreeSpaces + difference; } //move everything in memory table up if the address above it is free for(int a = 1; a < memoryTable.size();a++) { if(memoryTable[a-1] == "free") { memoryTable[a -1] = memoryTable[a]; memoryTable[a] = "free"; } } for(int b = memoryTable.size()-totalFreeSpaces; b < totalFreeSpaces; b++) { memoryTable[b] = "free"; } Spans* newSpan = new Spans(memoryTable.size()-totalFreeSpaces,totalFreeSpaces,free); newSpan->setBeginning(memoryTable.size()-totalFreeSpaces); newSpan->setEnding(totalFreeSpaces); resetSpanVector(spanVector); spanVector.push_back(newSpan); }