Skip to content

Commit 16d952f

Browse files
Update non_preemptive_sjf_scheduling.cpp
1 parent cbbfc88 commit 16d952f

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

cpu_scheduling_algorithms/non_preemptive_sjf_scheduling.cpp

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
* scheduling policy that selects for execution the waiting process with the
77
* smallest execution time. SJN is a non-preemptive algorithm. Shortest
88
* 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>
1112
*/
1213

1314
#include <algorithm> /// for sorting
@@ -42,9 +43,8 @@ using std::vector;
4243
*/
4344
template <typename S, typename T, typename E>
4445
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))) {
4848
return true;
4949
}
5050
return false;
@@ -180,6 +180,12 @@ class SJF {
180180
// Waiting time = Turnaround time - Burst time
181181
get<5>(cur) = get<4>(cur) - get<2>(cur);
182182

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+
183189
result.push_back(cur);
184190
schedule.pop();
185191
}
@@ -284,8 +290,8 @@ static void test() {
284290
}
285291

286292
// 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);
289295

290296
// Add processes to the queue
291297
for (uint32_t i{}; i < n; i++) {
@@ -297,8 +303,8 @@ static void test() {
297303
auto finalResult = readyQueue.scheduleForSJF();
298304

299305
// Print processes after scheduling
300-
cout << "\nProcesses after SJF scheduling:" << endl;
301-
readyQueue.printResult(finalResult);
306+
// cout << "\nProcesses after SJF scheduling:" << endl;
307+
// readyQueue.printResult(finalResult);
302308
}
303309
cout << "All the tests have successfully passed!" << endl;
304310
}

0 commit comments

Comments
 (0)