-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScheduler.h
339 lines (222 loc) · 10.7 KB
/
Scheduler.h
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
/*********************************************
* Implementation of the scheduling algorithms
* - Shortest Job First (SJF) - Preemptive and non preemptive
* - Least Laxity First (LLF)
* - Earliest Deadline First (EDF)
* - First Come First Serve (FCFS)
* - Round Robin
**********************************************/
#include<vector>
#include "sorting.h"
#include "aux.h"
#include "MinProc.h"
std::vector<Process> tmpProcs;
// Create the list of currently ready processes
void getReadyProcs(std::vector<Process>& proclist, std::vector<Process>& readyProcs, int clock) {
for (int i=0; i<proclist.size(); i++) {
if (proclist[i].getReadyTime() <= clock) {
readyProcs.push_back(proclist[i]);
}
}
}
// Shortest Job First
void SJF (std::vector<Process> p) {
tmpProcs = p;
// List of all currently ready processes
std::vector<Process> readyProcs;
int clock = 0; // Time slice clock
printf("\x1B[1mShortest Job First (SJF) - Preemptive\x1B[0m\n");
// While the process list still contains elements
while (!p.empty()) {
readyProcs.clear(); // Emptying the list before filling it
// Creating the list of the currently ready processes
// If a process appears before or at the current clock, add it to the list
getReadyProcs(p, readyProcs, clock);
if (!readyProcs.empty()) {
int minProc = getMinExecTimeProc(readyProcs); // Determine process with the shortest execution time
int indexOfMinProc = getIndexByProcNumber(p, minProc); // Determine the index of the process with the shortest execution
p[indexOfMinProc] -= 1; // Decrementing the execution time of the executed process (See the operation overloading in the class Process)
// If a process is being finished in the current time slice, delete it from the process list
if (p[indexOfMinProc].getExecTime() == 0) {
p.erase(p.begin()+indexOfMinProc);
}
// Mark the execution for the process at the current clock
markExecution(getIndexByProcNumber(tmpProcs, minProc), clock);
}
clock++; // Increment clock
}
}
void SJF_np (std::vector<Process> p) {
tmpProcs = p;
// List of all currently ready processes
std::vector<Process> readyProcs;
int clock = 0; // Time slice clock
int lastProc = -1; // Saves the process number of the lastly executed process
printf("\x1B[1mShortest Job First (SJF) - Non-preemptive\x1B[0m\n");
// While the process list still contains elements
while (!p.empty()) {
readyProcs.clear(); // Emptying the list before filling it
// Creating the list of the currently ready processes
// If a process appears before or at the current clock, add it to the list
getReadyProcs(p, readyProcs, clock);
int minProc;
if (!readyProcs.empty()) {
// If the lastly executed process hasn't finished
if (containsProcess(readyProcs, lastProc))
minProc = lastProc; // Execute the lastly executed process once again
else
minProc = getMinExecTimeProc(readyProcs); // Get process with shortest execution time
lastProc = minProc; // Save the current executed process for the next run
int indexOfMinProc = getIndexByProcNumber(p, minProc); // Get index of the process with the shortest execution time within the process list
p[indexOfMinProc] -= 1; // Decrementing the execution time of the executed process by 1 (see operator overloading in the class Process)
// If the process finishes in the current time slice (Execution time becomes 0)
// delete it from the process list
if (p[indexOfMinProc].getExecTime() == 0) {
p.erase(p.begin()+indexOfMinProc);
}
// Mark the time slice of the current process as executed
markExecution(getIndexByProcNumber(tmpProcs, minProc), clock);
}
clock++; // Increment clock
}
}
// Longest Job First
void LJF (std::vector<Process> p) {
tmpProcs = p;
// List of all currently ready processes
std::vector<Process> readyProcs;
int clock = 0; // Time slice clock
printf("\x1B[1mLongest Job First (SJF) - Preemptive\x1B[0m\n");
// While the process list still contains elements
while (!p.empty()) {
readyProcs.clear(); // Emptying the list before filling it
// Creating the list of the currently ready processes
// If a process appears before or at the current clock, add it to the list
getReadyProcs(p, readyProcs, clock);
if (!readyProcs.empty()) {
int maxProc = getMaxExecTimeProc(readyProcs); // Determine process with the longest execution time
int indexOfMaxProc = getIndexByProcNumber(p, maxProc); // Determine the index of the process with the longest execution
p[indexOfMaxProc] -= 1; // Decrementing the execution time of the executed process (See the operation overloading in the class Process)
// If a process is being finished in the current time slice, delete it from the process list
if (p[indexOfMaxProc].getExecTime() == 0) {
p.erase(p.begin()+indexOfMaxProc);
}
// Mark the execution for the process at the current clock
markExecution(getIndexByProcNumber(tmpProcs, maxProc), clock);
}
clock++; // Increment clock
}
}
// Earliest Deadline First
// The approach is similar to SJF except for the following comments.
void EDF (std::vector<Process> p) {
std::vector<Process> readyProcs;
tmpProcs = p;
int clock = 0;
printf("\x1B[1mEarliest Deadline First (EDF)\x1B[0m\n");
while (!p.empty()) {
readyProcs.clear();
getReadyProcs(p, readyProcs, clock);
if (!readyProcs.empty()) {
int minProc = getMinDeadlineProc(readyProcs); // Determine the process with the earliest deadline
int indexOfMinProc = getIndexByProcNumber(p, minProc); // Determine the index of the process with the earliest deadline
p[indexOfMinProc] -= 1;
// If the deadline minus the current clock becomes less than 0, the deadline couldn't be obeyed.
if (p[indexOfMinProc].getDeadline() - clock > 0)
markExecution(getIndexByProcNumber(tmpProcs, minProc), clock);
else
markExecution(getIndexByProcNumber(tmpProcs, minProc), clock, 2);
if (p[indexOfMinProc].getExecTime() == 0) {
p.erase(p.begin()+indexOfMinProc);
}
}
clock++;
}
}
// Lowest Laxity First
// The approach is similar to SJF except for the following comments.
void LLF (std::vector<Process> p) {
std::vector<Process> readyProcs;
tmpProcs = p;
int clock = 0;
printf("\x1B[1mLeast Laxity First (LLF)\x1B[0m\n");
while (!p.empty()) {
readyProcs.clear();
getReadyProcs(p, readyProcs, clock);
if (!readyProcs.empty()) {
int minProc = getMinLaxityProc(readyProcs); // Determine the process with the least laxity
int indexOfMinProc = getIndexByProcNumber(p, minProc); // Determine the index of the process with the least laxity
p[indexOfMinProc] -= 1;
// If the laxity becomes less than 0, the deadline couldn't be obeyed
if (p[indexOfMinProc].getLaxity() >= 0)
markExecution(getIndexByProcNumber(tmpProcs, minProc), clock);
else
markExecution(getIndexByProcNumber(tmpProcs, minProc), clock, 2);
if (p[indexOfMinProc].getExecTime() == 0) {
p.erase(p.begin()+indexOfMinProc);
}
}
clock++;
}
}
// First Come First Serve
void FCFS (std::vector<Process> p) {
std::vector<Process> readyProcs;
tmpProcs = p;
int clock = 0;
printf("\x1B[1mFirst Come First Serve\x1B[0m\n");
while (!p.empty()) {
readyProcs.clear();
getReadyProcs(p, readyProcs, clock);
if (!readyProcs.empty()) {
int minProc = getMinReadyTimeProc(readyProcs); // Get process with least ready time
int indexOfMinProc = getIndexByProcNumber(p, minProc); // Get process with least ready time within the process list
p[indexOfMinProc] -= 1;
markExecution(getIndexByProcNumber(tmpProcs, minProc), clock);
if (p[indexOfMinProc].getExecTime() == 0) {
p.erase(p.begin()+indexOfMinProc);
}
}
clock++;
}
}
// Round Robin
// In addition to the process list it accepts the length of the time slice
void RoundRobin (std::vector<Process> p, int quant) {
std::vector<Process> readyProcs; // List of the ready processes
tmpProcs = p;
int clock = 0; // Clock for the response time
int wait = 0; // Clock for the classification of the process
printf("\x1B[1mRound Robin - Quantum: %d\x1B[0m\n", quant);
while (!p.empty()) {
readyProcs.clear();
// Sort processes according to their ready times
// Consequently, the first process to arrive will get the first time slice of a round
sortProcesses(p, byReadyTime);
// Generate the list of the currently ready processes
getReadyProcs(p, readyProcs, clock);
if (!readyProcs.empty()) {
// Iteration over the list of the ready processes
for (int i=0; i<readyProcs.size(); i++) {
// For the non-termination of a process
if (readyProcs[i].getExecTime() - quant > 0) {
p[getIndexByProcNumber(p, readyProcs[i].getProcessNumber())] -= quant; // Subtract the time slice from the edited process
for (int a=clock; a<clock+quant; a++)
markExecution(getIndexByProcNumber(tmpProcs, readyProcs[i].getProcessNumber()), a);
clock += quant; // Increase the clock by the time slice
}
else {
for (int a=clock; a<clock+readyProcs[i].getExecTime(); a++)
markExecution(getIndexByProcNumber(tmpProcs, readyProcs[i].getProcessNumber()), a);
clock += readyProcs[i].getExecTime(); // Increase the clock by the remaining execution time
p.erase(p.begin()+getIndexByProcNumber(p, readyProcs[i].getProcessNumber())); // Remove the process from the process list
}
}
wait += quant; // Increasing the clock for queueing processes
}
else {
clock += quant;
wait += quant;
}
}
}