From 7af8d8ae0a45fb5050aa7184d73e77250b769d87 Mon Sep 17 00:00:00 2001 From: LakshmiSrikumar <141216093+LakshmiSrikumar@users.noreply.github.com> Date: Fri, 11 Oct 2024 00:49:46 +0530 Subject: [PATCH] added a vector to store and print the final result --- non_preemptive_sjf_scheduling.cpp | 59 ++++++++++++++++++++++--------- 1 file changed, 42 insertions(+), 17 deletions(-) diff --git a/non_preemptive_sjf_scheduling.cpp b/non_preemptive_sjf_scheduling.cpp index ea4f98d92..b470ca97d 100644 --- a/non_preemptive_sjf_scheduling.cpp +++ b/non_preemptive_sjf_scheduling.cpp @@ -11,8 +11,7 @@ #include /// for sorting #include /// for assert -#include /// random number generation -#include /// for time +#include /// random number generation #include /// for formatting the output #include /// for IO operations #include /// for std::priority_queue @@ -26,8 +25,6 @@ using std::get; using std::left; using std::make_tuple; using std::priority_queue; -using std::rand; -using std::srand; using std::tuple; using std::unordered_set; using std::vector; @@ -37,8 +34,8 @@ using std::vector; * @tparam S Data type of Process ID * @tparam T Data type of Arrival time * @tparam E Data type of Burst time - * @param t1 First tuple - * @param t2 Second tuple + * @param t1 First tuplet1 + * @param t2 Second tuplet2 * @returns true if t1 and t2 are in the CORRECT order * @returns false if t1 and t2 are in the INCORRECT order */ @@ -260,27 +257,55 @@ class SJF { * @returns void */ static void test() { - for (int i{}; i < 1000; i++) { - srand(time(nullptr)); - uint32_t n = 1 + rand() % 1000; + // A vector to store the results of all processes across all test cases. + vector> finalResult; + + for (int i{}; i < 100; i++) { + std::random_device rd; // Seeding + std::mt19937 eng(rd()); + std::uniform_int_distribution<> distr(1, 100); + + uint32_t n = distr(eng); SJF readyQueue; vector> input(n); + // Generate random arrival and burst times for (uint32_t i{}; i < n; i++) { get<0>(input[i]) = i; - srand(time(nullptr)); - get<1>(input[i]) = 1 + rand() % 10000; - srand(time(nullptr)); - get<2>(input[i]) = 1 + rand() % 10000; + get<1>(input[i]) = distr(eng); // Random arrival time + get<2>(input[i]) = distr(eng); // Random burst time } + // Add processes to the queue for (uint32_t i{}; i < n; i++) { - readyQueue.addProcess(get<0>(input[i]), get<1>(input[i]), - get<2>(input[i])); + readyQueue.addProcess(get<0>(input[i]), get<1>(input[i]), get<2>(input[i])); } - readyQueue.scheduleForSJF(); - //readyQueue.printResult(); + + // Schedule processes using SJF + vector> beforeResult = readyQueue.scheduleForSJF(); + + // Append the results of this test case to the final result + finalResult.insert(finalResult.end(), partialResult.begin(), partialResult.end()); } + + // Now, print the results after all test cases + cout << "Status of all processes post completion is as follows:" << endl; + cout << std::setw(17) << left << "Process ID" << std::setw(17) << left + << "Arrival Time" << std::setw(17) << left << "Burst Time" + << std::setw(17) << left << "Completion Time" << std::setw(17) + << left << "Turnaround Time" << std::setw(17) << left << "Waiting Time" << endl; + + // Loop through the final result and print all process details + for (size_t i{}; i < finalResult.size(); i++) { + cout << std::setprecision(2) << std::fixed << std::setw(17) << left + << get<0>(finalResult[i]) << std::setw(17) << left + << get<1>(finalResult[i]) << std::setw(17) << left + << get<2>(finalResult[i]) << std::setw(17) << left + << get<3>(finalResult[i]) << std::setw(17) << left + << get<4>(finalResult[i]) << std::setw(17) << left + << get<5>(finalResult[i]) << endl; + } + cout << "All the tests have successfully passed!" << endl; }