-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpuScheduler.cpp
More file actions
160 lines (139 loc) · 4.61 KB
/
cpuScheduler.cpp
File metadata and controls
160 lines (139 loc) · 4.61 KB
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
#include <iostream>
#include <unordered_map>
#include <vector>
#include <fstream>
#include <string>
#include <sstream>
#include <limits>
using namespace std;
void readFile(const string& fileName, unordered_map<string, vector<int>>& taskMap, int& numberOfTasks)
{
ifstream inFile(fileName);
if (!inFile) exit(0);
string line;
while (getline(inFile, line))
{
istringstream lineStream(line);
string keyComma, valueString;
// Get the key/TaskName
getline(lineStream, keyComma, ',');
// Clean String
string key = keyComma;
key.erase(key.find_last_not_of(" \n\r\t") + 1);
// Read values
while (getline(lineStream, valueString, ','))
{
valueString.erase(0, valueString.find_first_not_of(" "));
int value = stoi(valueString);
taskMap[key].push_back(value);
}
numberOfTasks++;
}
inFile.close();
}
int main(int argc, char* argv[])
{
if (argc < 2)
{
cerr << "FAIL";
return 1;
}
unordered_map<string, vector<int>> taskMap;
int numberOfTasks = 0;
readFile(argv[1], taskMap, numberOfTasks);
int totalBT = 0;
// Calculate the Total Burst Time
for (const auto& pair : taskMap)
{
totalBT += pair.second[2];
}
cout << "Number of Tasks: " << numberOfTasks << endl;
cout << "Total Burst Time: " << totalBT << endl;
unordered_map<string, vector<string>> finalOutput;
unordered_map<string, int> remainingTime;
// Initialize remaining time for each task
for (const auto& pair : taskMap)
{
remainingTime[pair.first] = pair.second[2];
// This is for Start, Preempted, Resumed, End
finalOutput[pair.first].resize(4);
}
string currentTask = "";
int currentTime = 0;
while (currentTime < totalBT)
{
string taskWithHighestPriority;
int highestPriority = numeric_limits<int>::min();
int earliestArrival = numeric_limits<int>::max();
// Check which tasks have arrived and what the highest priority is checking with arrival time if needed
for (const auto& task : taskMap)
{
if (task.second[0] <= currentTime && remainingTime[task.first] > 0)
{ // Arrived and remaining time
if (task.second[1] > highestPriority || (task.second[1] == highestPriority && task.second[0] < earliestArrival))
{
highestPriority = task.second[1];
earliestArrival = task.second[0];
taskWithHighestPriority = task.first;
}
}
}
// Actually recording what happens as the burst time increases
// Check to see if there is something to run
if (!taskWithHighestPriority.empty())
{
// If the current task is preempted
if (currentTask != taskWithHighestPriority)
{
if (!currentTask.empty())
{
finalOutput[currentTask][1] = "Preempted at: " + to_string(currentTime);
}
if (finalOutput[taskWithHighestPriority][0].empty())
{
finalOutput[taskWithHighestPriority][0] = to_string(currentTime); // Start time
} else
{
finalOutput[taskWithHighestPriority][2] = "Resumed at: " + to_string(currentTime);
}
}
// Executing the task by taking away a burst time second
remainingTime[taskWithHighestPriority]--;
// Update current task
currentTask = taskWithHighestPriority;
// If this task finishes
if (remainingTime[taskWithHighestPriority] == 0)
{
finalOutput[taskWithHighestPriority][3] = "End: " + to_string(currentTime + 1);
// Clear current task if finished
currentTask.clear();
}
}
else
{
// No task to run
currentTask.clear();
}
// Increment time
currentTime++;
}
// Print final output
for (const auto& pair : finalOutput)
{
cout << "Task: " << pair.first << ", Start: " << pair.second[0];
if (!pair.second[1].empty())
{
cout << ", " << pair.second[1];
}
if (!pair.second[2].empty())
{
cout << ", " << pair.second[2];
}
if (!pair.second[3].empty())
{
cout << ", " << pair.second[3];
}
cout << endl;
}
return 0;
}