From 16d952f6fef599be066ed4340b6aed673a928495 Mon Sep 17 00:00:00 2001
From: LakshmiSrikumar <141216093+LakshmiSrikumar@users.noreply.github.com>
Date: Sat, 12 Oct 2024 16:07:46 +0530
Subject: [PATCH] Update non_preemptive_sjf_scheduling.cpp
---
.../non_preemptive_sjf_scheduling.cpp | 24 ++++++++++++-------
1 file changed, 15 insertions(+), 9 deletions(-)
diff --git a/cpu_scheduling_algorithms/non_preemptive_sjf_scheduling.cpp b/cpu_scheduling_algorithms/non_preemptive_sjf_scheduling.cpp
index 0b0de343d..d1935a4c8 100644
--- a/cpu_scheduling_algorithms/non_preemptive_sjf_scheduling.cpp
+++ b/cpu_scheduling_algorithms/non_preemptive_sjf_scheduling.cpp
@@ -6,8 +6,9 @@
* scheduling policy that selects for execution the waiting process with the
* smallest execution time. SJN is a non-preemptive algorithm. Shortest
* remaining time is a preemptive variant of SJN.
- * @link https://www.guru99.com/shortest-job-first-sjf-scheduling.html
- * @author [Lakshmi Srikumar](https://github.com/LakshmiSrikumar)
+ *
+ * detailed description on SJF scheduling
+ * Author : Lakshmi Srikumar
*/
#include /// for sorting
@@ -42,9 +43,8 @@ using std::vector;
*/
template
bool sortcol(tuple& t1, tuple& t2) {
- if (get<1>(t1) < get<1>(t2)) {
- return true;
- } else if (get<1>(t1) == get<1>(t2) && get<0>(t1) < get<0>(t2)) {
+ if (get<1>(t1) < get<1>(t2) ||
+ (get<1>(t1) == get<1>(t2) && get<0>(t1) < get<0>(t2))) {
return true;
}
return false;
@@ -180,6 +180,12 @@ class SJF {
// Waiting time = Turnaround time - Burst time
get<5>(cur) = get<4>(cur) - get<2>(cur);
+ // Turnaround time >= Burst time
+ assert(get<4>(cur) >= get<2>(cur));
+
+ // Waiting time is never negative
+ assert(get<5>(cur) >= 0);
+
result.push_back(cur);
schedule.pop();
}
@@ -284,8 +290,8 @@ static void test() {
}
// Print processes before scheduling
- cout << "Processes before SJF scheduling:" << endl;
- readyQueue.printResult(input);
+ // cout << "Processes before SJF scheduling:" << endl;
+ // readyQueue.printResult(input);
// Add processes to the queue
for (uint32_t i{}; i < n; i++) {
@@ -297,8 +303,8 @@ static void test() {
auto finalResult = readyQueue.scheduleForSJF();
// Print processes after scheduling
- cout << "\nProcesses after SJF scheduling:" << endl;
- readyQueue.printResult(finalResult);
+ // cout << "\nProcesses after SJF scheduling:" << endl;
+ // readyQueue.printResult(finalResult);
}
cout << "All the tests have successfully passed!" << endl;
}