Skip to content

Commit 7af8d8a

Browse files
added a vector to store and print the final result
1 parent 8d6f3c1 commit 7af8d8a

File tree

1 file changed

+42
-17
lines changed

1 file changed

+42
-17
lines changed

non_preemptive_sjf_scheduling.cpp

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111

1212
#include <algorithm> /// for sorting
1313
#include <cassert> /// for assert
14-
#include <cstdlib> /// random number generation
15-
#include <ctime> /// for time
14+
#include <random> /// random number generation
1615
#include <iomanip> /// for formatting the output
1716
#include <iostream> /// for IO operations
1817
#include <queue> /// for std::priority_queue
@@ -26,8 +25,6 @@ using std::get;
2625
using std::left;
2726
using std::make_tuple;
2827
using std::priority_queue;
29-
using std::rand;
30-
using std::srand;
3128
using std::tuple;
3229
using std::unordered_set;
3330
using std::vector;
@@ -37,8 +34,8 @@ using std::vector;
3734
* @tparam S Data type of Process ID
3835
* @tparam T Data type of Arrival time
3936
* @tparam E Data type of Burst time
40-
* @param t1 First tuple
41-
* @param t2 Second tuple
37+
* @param t1 First tuple<S,T,E>t1
38+
* @param t2 Second tuple<S,T,E>t2
4239
* @returns true if t1 and t2 are in the CORRECT order
4340
* @returns false if t1 and t2 are in the INCORRECT order
4441
*/
@@ -260,27 +257,55 @@ class SJF {
260257
* @returns void
261258
*/
262259
static void test() {
263-
for (int i{}; i < 1000; i++) {
264-
srand(time(nullptr));
265-
uint32_t n = 1 + rand() % 1000;
260+
// A vector to store the results of all processes across all test cases.
261+
vector<tuple<uint32_t, uint32_t, uint32_t, double, double, double>> finalResult;
262+
263+
for (int i{}; i < 100; i++) {
264+
std::random_device rd; // Seeding
265+
std::mt19937 eng(rd());
266+
std::uniform_int_distribution<> distr(1, 100);
267+
268+
uint32_t n = distr(eng);
266269
SJF<uint32_t, uint32_t, uint32_t> readyQueue;
267270
vector<tuple<uint32_t, uint32_t, uint32_t>> input(n);
268271

272+
// Generate random arrival and burst times
269273
for (uint32_t i{}; i < n; i++) {
270274
get<0>(input[i]) = i;
271-
srand(time(nullptr));
272-
get<1>(input[i]) = 1 + rand() % 10000;
273-
srand(time(nullptr));
274-
get<2>(input[i]) = 1 + rand() % 10000;
275+
get<1>(input[i]) = distr(eng); // Random arrival time
276+
get<2>(input[i]) = distr(eng); // Random burst time
275277
}
276278

279+
// Add processes to the queue
277280
for (uint32_t i{}; i < n; i++) {
278-
readyQueue.addProcess(get<0>(input[i]), get<1>(input[i]),
279-
get<2>(input[i]));
281+
readyQueue.addProcess(get<0>(input[i]), get<1>(input[i]), get<2>(input[i]));
280282
}
281-
readyQueue.scheduleForSJF();
282-
//readyQueue.printResult();
283+
284+
// Schedule processes using SJF
285+
vector<tuple<uint32_t, uint32_t, uint32_t, double, double, double>> beforeResult = readyQueue.scheduleForSJF();
286+
287+
// Append the results of this test case to the final result
288+
finalResult.insert(finalResult.end(), partialResult.begin(), partialResult.end());
283289
}
290+
291+
// Now, print the results after all test cases
292+
cout << "Status of all processes post completion is as follows:" << endl;
293+
cout << std::setw(17) << left << "Process ID" << std::setw(17) << left
294+
<< "Arrival Time" << std::setw(17) << left << "Burst Time"
295+
<< std::setw(17) << left << "Completion Time" << std::setw(17)
296+
<< left << "Turnaround Time" << std::setw(17) << left << "Waiting Time" << endl;
297+
298+
// Loop through the final result and print all process details
299+
for (size_t i{}; i < finalResult.size(); i++) {
300+
cout << std::setprecision(2) << std::fixed << std::setw(17) << left
301+
<< get<0>(finalResult[i]) << std::setw(17) << left
302+
<< get<1>(finalResult[i]) << std::setw(17) << left
303+
<< get<2>(finalResult[i]) << std::setw(17) << left
304+
<< get<3>(finalResult[i]) << std::setw(17) << left
305+
<< get<4>(finalResult[i]) << std::setw(17) << left
306+
<< get<5>(finalResult[i]) << endl;
307+
}
308+
284309
cout << "All the tests have successfully passed!" << endl;
285310
}
286311

0 commit comments

Comments
 (0)