-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
354 lines (297 loc) · 10.4 KB
/
main.cpp
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <queue>
#include <set>
#include <unordered_map>
#include <numeric>
#include <climits>
using namespace std;
class Job {
public:
int id;
int burst;
int arrival;
int remaining;
int wait;
int turnaround;
Job(int id, int burst, int arrival)
: id(id), burst(burst), arrival(arrival), remaining(burst), wait(0), turnaround(0) {}
};
class Execution {
public:
int id;
int start;
int end;
Execution(int id, int start, int end)
: id(id), start(start), end(end) {}
};
void calculateWaitAndTurnaround(vector<Job>& jobs) {
for (auto& j : jobs) {
j.turnaround = j.wait + j.burst;
}
}
void printAverageTimes(vector<Job>& jobs, ofstream& outputFile) {
int totalWait = 0, totalTurnaround = 0;
for (const auto& process : jobs) {
totalWait += process.wait;
totalTurnaround += process.turnaround;
}
long long size = jobs.size();
outputFile << "Average Waiting Time: " << (float)totalWait / size << endl;
outputFile << "Average Turnaround Time: " << (float)totalTurnaround / size << endl;
}
bool increasingID(Job& j1, Job& j2) {
return j1.id < j2.id;
}
void printGanttChart(vector<Execution>& executions, vector<Job>& jobs, ofstream& outputFile) {
sort(jobs.begin(), jobs.end(), increasingID);
int totalExecutions = executions.size();
int totalJobs = jobs.size();
vector<vector<char>> ganttChart(totalJobs + 1, vector<char>(executions[totalExecutions - 1].end, '-'));
for(int i=0; i<totalJobs; i++) {
for(int j = jobs[i].arrival; j<jobs[i].turnaround + jobs[i].arrival; j++) {
ganttChart[jobs[i].id][j] = '.';
}
}
for(int i=0; i<totalExecutions; i++) {
for(int j=executions[i].start; j<executions[i].end; j++) {
ganttChart[executions[i].id][j] = '*';
}
}
outputFile << "Gantt Chart:" << endl;
outputFile << " ";
for(int j=0; j<executions[totalExecutions - 1].end + 1; j++) {
outputFile << j%10 << " ";
}
outputFile << "\n";
for(int i=1; i<totalJobs + 1; i++) {
outputFile << "J" << jobs[i - 1].id << " ";
for(int j=0; j<executions[totalExecutions - 1].end; j++) {
if(ganttChart[i][j] == '-') {
outputFile << "| ";
} else if(ganttChart[i][j] == '.'){
outputFile << "|.";
} else {
outputFile << "|*";
}
}
outputFile << "|\n";
}
}
bool SortByArrival(Job& a, Job& b){
return a.arrival < b.arrival;
}
void fcfs(vector<Job> jobs, ofstream& outputFile) {
vector<Execution> executions;
int time_marker = 0;
sort(jobs.begin(), jobs.end(), SortByArrival);
for (auto& j : jobs) {
if (time_marker < j.arrival) {
time_marker = j.arrival;
}
j.wait = max(0, time_marker - j.arrival);
executions.push_back(Execution(j.id, time_marker, time_marker + j.burst));
time_marker += j.burst;
}
calculateWaitAndTurnaround(jobs);
outputFile << "FCFS Scheduling:" << endl;
printAverageTimes(jobs, outputFile);
printGanttChart(executions, jobs, outputFile);
}
void sjf(vector<Job> jobs, ofstream& outputFile) {
sort(jobs.begin(), jobs.end(), SortByArrival);
vector<Execution> executions;
int currentTime = 0;
int completedJobs = 0;
int n = jobs.size();
vector<bool> isJobCompleted(n, false);
while (completedJobs < n) {
int shortestJobIndex = -1;
int shortestBurstTime = INT_MAX;
for (int i = 0; i < n; i++) {
if (jobs[i].arrival <= currentTime && !isJobCompleted[i] && jobs[i].burst < shortestBurstTime) {
shortestBurstTime = jobs[i].burst;
shortestJobIndex = i;
}
}
if (shortestJobIndex == -1) {
currentTime++;
continue;
}
Job& j = jobs[shortestJobIndex];
j.wait = max(0, currentTime - j.arrival);
executions.push_back(Execution(j.id, currentTime, currentTime + j.burst));
currentTime += j.burst;
j.turnaround = j.wait + j.burst;
isJobCompleted[shortestJobIndex] = true;
completedJobs++;
}
calculateWaitAndTurnaround(jobs);
outputFile << "SJF Scheduling:" << endl;
printAverageTimes(jobs, outputFile);
printGanttChart(executions, jobs, outputFile);
}
void srtf(vector<Job> jobs, ofstream& outputFile) {
int n = jobs.size();
vector<Execution> executions;
int currentTime = 0;
int completed = 0;
int shortestIndex = 0;
int shortestRemainingTime = INT_MAX;
bool check = false;
while (completed != n) {
for (int i = 0; i < n; ++i) {
if (jobs[i].arrival <= currentTime && jobs[i].remaining > 0) {
if (jobs[i].remaining < shortestRemainingTime) {
shortestRemainingTime = jobs[i].remaining;
shortestIndex = i;
check = true;
}
}
}
if (!check) {
currentTime++;
continue;
}
executions.push_back(Execution(jobs[shortestIndex].id, currentTime, currentTime + 1));
jobs[shortestIndex].remaining--;
shortestRemainingTime = jobs[shortestIndex].remaining;
if (shortestRemainingTime == 0) {
shortestRemainingTime = INT_MAX;
}
if (jobs[shortestIndex].remaining == 0) {
completed++;
check = false;
jobs[shortestIndex].wait = max(0, currentTime + 1 - jobs[shortestIndex].arrival - jobs[shortestIndex].burst);
jobs[shortestIndex].turnaround = jobs[shortestIndex].wait + jobs[shortestIndex].burst;
}
currentTime++;
}
calculateWaitAndTurnaround(jobs);
outputFile << "SRTF Scheduling:" << endl;
printAverageTimes(jobs, outputFile);
printGanttChart(executions, jobs, outputFile);
}
void roundRobin(vector<Job> jobs, int quantum, ofstream& outputFile) {
vector<Execution> executions;
int currentTime = 0;
queue<int> readyQueue;
int Jobs_completed = 0;
for (auto& j : jobs) {
j.remaining = j.burst;
}
int n = jobs.size();
vector<int>arrival(n);
for (int i = 0; i < n; i++) {
arrival[i] = jobs[i].arrival;
}
while (Jobs_completed != jobs.size()) {
for (int i = 0; i < n; i++) {
if (arrival[i] <= currentTime && arrival[i] != -1) {
readyQueue.push(i);
arrival[i] = -1;
}
}
if (readyQueue.empty()) {
currentTime++;
continue;
}
int current = readyQueue.front();
readyQueue.pop();
int timeSlice = min(quantum, jobs[current].remaining);
executions.push_back(Execution(jobs[current].id, currentTime, currentTime + timeSlice));
jobs[current].remaining -= timeSlice;
currentTime += timeSlice;
for (int i = 0; i < n; i++) {
if (arrival[i] <= currentTime && arrival[i] != -1) {
readyQueue.push(i);
arrival[i] = -1;
}
}
if (jobs[current].remaining > 0) {
readyQueue.push(current);
} else {
Jobs_completed++;
jobs[current].wait = currentTime - jobs[current].arrival - jobs[current].burst;
}
}
calculateWaitAndTurnaround(jobs);
outputFile << "Round Robin Scheduling (Quantum = 2):" << endl;
printAverageTimes(jobs, outputFile);
printGanttChart(executions, jobs, outputFile);
}
string predictBestAlgorithm(vector<Job>& jobs) {
float totalBurstTime = accumulate(jobs.begin(), jobs.end(), 0.0f, [](float sum, const Job& j) {
return sum + j.burst;
});
float avgBurstTime = totalBurstTime / jobs.size();
bool allBurstTimesSame = all_of(jobs.begin(), jobs.end(), [&](const Job& j) {
return j.burst == jobs[0].burst;
});
if (allBurstTimesSame) {
return "FCFS";
}
float burstTimeVariance = 0;
for (const auto& j : jobs) {
burstTimeVariance += (j.burst - avgBurstTime) * (j.burst - avgBurstTime);
}
burstTimeVariance /= jobs.size();
float totalArrivalTime = accumulate(jobs.begin(), jobs.end(), 0.0f, [](float sum, const Job& j) {
return sum + j.arrival;
});
float avgArrivalTime = totalArrivalTime / jobs.size();
float arrivalTimeVariance = 0;
for (const auto& j : jobs) {
arrivalTimeVariance += (j.arrival - avgArrivalTime) * (j.arrival - avgArrivalTime);
}
arrivalTimeVariance /= jobs.size();
int longBurstCount = count_if(jobs.begin(), jobs.end(), [&](const Job& j) {
return j.burst > avgBurstTime;
});
if (burstTimeVariance < avgBurstTime && arrivalTimeVariance < avgArrivalTime) {
return "SJF";
} else if (burstTimeVariance < avgBurstTime) {
return "SRTF";
} else if (longBurstCount > jobs.size() / 2) {
return "SRTF";
} else {
return "SRTF";
}
}
int main() {
ifstream inputFile("input.txt");
ofstream outputFile("output.txt");
if (!inputFile.is_open()) {
cerr << "Error reading input file 🥲" << endl;
return 0;
}
if (!outputFile.is_open()) {
cerr << "Error reading output file 🥲" << endl;
return 0;
}
vector<Job> jobs;
int id, burst, arrival;
while (inputFile >> id >> burst >> arrival) {
jobs.push_back(Job(id, burst, arrival));
}
if (jobs.empty()) {
outputFile << "No jobs found in input file" << endl;
return 0;
} else {
for (const auto& j : jobs) {
cerr << "Loaded Job - ID: " << j.id << ", Burst Time: " << j.burst << ", Arrival Time: " << j.arrival << endl;
}
}
string predictedAlgorithm = predictBestAlgorithm(jobs);
outputFile << "Best algorithm predicted is " << predictedAlgorithm << endl;
outputFile << endl;
fcfs(jobs, outputFile);
sjf(jobs, outputFile);
srtf(jobs, outputFile);
roundRobin(jobs, 2, outputFile);
inputFile.close();
outputFile.close();
return 0;
}