6
6
* scheduling policy that selects for execution the waiting process with the
7
7
* smallest execution time. SJN is a non-preemptive algorithm. Shortest
8
8
* remaining time is a preemptive variant of SJN.
9
- * @link https://www.guru99.com/shortest-job-first-sjf-scheduling.html
10
- * @author [Lakshmi Srikumar](https://github.com/LakshmiSrikumar)
9
+ * <a href="https://www.guru99.com/shortest-job-first-sjf-scheduling.html">
10
+ * detailed description on SJF scheduling </a>
11
+ * <a href="https://github.com/LakshmiSrikumar">Author : Lakshmi Srikumar </a>
11
12
*/
12
13
13
14
#include < algorithm> // / for sorting
@@ -42,9 +43,8 @@ using std::vector;
42
43
*/
43
44
template <typename S, typename T, typename E>
44
45
bool sortcol (tuple<S, T, E>& t1, tuple<S, T, E>& t2) {
45
- if (get<1 >(t1) < get<1 >(t2)) {
46
- return true ;
47
- } else if (get<1 >(t1) == get<1 >(t2) && get<0 >(t1) < get<0 >(t2)) {
46
+ if (get<1 >(t1) < get<1 >(t2) ||
47
+ (get<1 >(t1) == get<1 >(t2) && get<0 >(t1) < get<0 >(t2))) {
48
48
return true ;
49
49
}
50
50
return false ;
@@ -180,6 +180,12 @@ class SJF {
180
180
// Waiting time = Turnaround time - Burst time
181
181
get<5 >(cur) = get<4 >(cur) - get<2 >(cur);
182
182
183
+ // Turnaround time >= Burst time
184
+ assert (get<4 >(cur) >= get<2 >(cur));
185
+
186
+ // Waiting time is never negative
187
+ assert (get<5 >(cur) >= 0 );
188
+
183
189
result.push_back (cur);
184
190
schedule.pop ();
185
191
}
@@ -284,8 +290,8 @@ static void test() {
284
290
}
285
291
286
292
// Print processes before scheduling
287
- cout << " Processes before SJF scheduling:" << endl;
288
- readyQueue.printResult (input);
293
+ // cout << "Processes before SJF scheduling:" << endl;
294
+ // readyQueue.printResult(input);
289
295
290
296
// Add processes to the queue
291
297
for (uint32_t i{}; i < n; i++) {
@@ -297,8 +303,8 @@ static void test() {
297
303
auto finalResult = readyQueue.scheduleForSJF ();
298
304
299
305
// Print processes after scheduling
300
- cout << " \n Processes after SJF scheduling:" << endl;
301
- readyQueue.printResult (finalResult);
306
+ // cout << "\nProcesses after SJF scheduling:" << endl;
307
+ // readyQueue.printResult(finalResult);
302
308
}
303
309
cout << " All the tests have successfully passed!" << endl;
304
310
}
0 commit comments