hello i am having some trouble
with one of my funtions its probably the executeAll funtion or my print function. its not print the correct information to the console window. this is some of the teachers notes" 2. Run the Simulation – To serve a job… we pop the task off the front of the queue, we deduct TIME_SLICE from its remaining run time, check to see if it’s done. If it’s done, we print information about the task and move to the next task. If it’s NOT done, we insert it into the next lowest priority queue"
the queueFile.txt includes
0 50 // call this 1
1 100 //call this 2
2 125 //call this 3
3 150 // call this 4
4 175 // call this 5
and should print
for 1 - "Total time Needed is" 50
"Time it took to finish is" 50
for 2 - "Total time Needed is" 125
"Time it took to finish is" 150 // because every time it runs it . would add 50
for 3 - "Total time Needed is" 150
"Time it took to finish is" 150
.....
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
|
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <queue>
#include <fstream>
#include <string>
#include <ctime>
using namespace std;
const int timeSlice = 50;
struct Task
{
int pID;
int initialPriority;
int currentPriority;
int totalRunTime;
int remainingRunTime;
};
// function prototype
void readFromFile();
void executeAll();
void printQueue(int queue);
queue<Task>PriorityQueues[5];
void main()
{
readFromFile();
executeAll();
//printQueue(0);
cin.get();
cin.ignore();
}
//================================================================
// Function: readFromFile
// Description: Reads from file and puts them in the queue of arrays
//
// Arguments:
// None
// Return value:
// NR
//=================================================================
void readFromFile()
{
int pIDIndex = 1; // temp variables
int totalRunTimeTemp = 0;
ifstream queueFile("queueFile.txt");
while (!queueFile.eof())
{
if (queueFile.is_open())
{
Task tempTask;
tempTask.pID = pIDIndex;
pIDIndex++;
queueFile >> tempTask.initialPriority;
queueFile >> tempTask.totalRunTime;
tempTask.currentPriority = tempTask.initialPriority;
tempTask.remainingRunTime = tempTask.totalRunTime;
PriorityQueues[tempTask.initialPriority].push(tempTask);
}
else
cout << "plz check file" << endl;
}
queueFile.close();
}
//================================================================
// Function: printQueue
// Description: print
//
// Arguments:
// None
// Return value:
// NR
//=================================================================
void printQueue(int queueIndex)
{
string timeMs = "ms";
time_t currentTime;
struct tm currentLocal;
currentTime = time(NULL);
currentLocal = *localtime(¤tTime);
cout << "process ID is" << PriorityQueues[queueIndex].front().pID << endl;
cout << "Initisl Priority is " << PriorityQueues[queueIndex].front().initialPriority << endl;
cout << "Final Priority is " << PriorityQueues[queueIndex].front().currentPriority << endl;
cout << "Total time Needed is " << PriorityQueues[queueIndex].front().totalRunTime <<timeMs<< endl;
cout << "Time it took to finish is " << PriorityQueues[queueIndex].front().remainingRunTime <<timeMs<< endl;
cout << "Total Time to Finish is " << currentLocal.tm_hour << ":" << currentLocal.tm_min << ":" << currentLocal.tm_sec << endl;
cout << endl;
cout << endl;
}
//================================================================
// Function: readFromFile
// Description: Reads from file and puts them in the queue of arrays
//
// Arguments:
// None
// Return value:
// NR
//=================================================================
void executeAll()
{
int i = 0;
while (i != 4) // for 0-3
{
while (!PriorityQueues[i].empty())
{
PriorityQueues[i].front().remainingRunTime = PriorityQueues[i].front().remainingRunTime - timeSlice;
PriorityQueues[i].front().totalRunTime = PriorityQueues[i].front().totalRunTime + timeSlice;
if (PriorityQueues[i].front().remainingRunTime > 0)
{
PriorityQueues[i].front().currentPriority++;
PriorityQueues[i + 1].push(PriorityQueues[i].front());
PriorityQueues[i].pop();
}
else
{
printQueue(i);
PriorityQueues[i].pop();
}
}
i++;
}
while (!PriorityQueues[4].empty()) // for the last one 4
{
PriorityQueues[4].front().remainingRunTime = PriorityQueues[4].front().remainingRunTime - timeSlice;
PriorityQueues[4].front().totalRunTime = PriorityQueues[4].front().totalRunTime + timeSlice;
if (PriorityQueues[4].front().remainingRunTime <= 0)
{
printQueue(4);
PriorityQueues[4].pop();
}
}
}
| |