11
11
12
12
#include < algorithm> // / for sorting
13
13
#include < cassert> // / for assert
14
- #include < cstdlib> // / random number generation
15
- #include < ctime> // / for time
14
+ #include < random> // / random number generation
16
15
#include < iomanip> // / for formatting the output
17
16
#include < iostream> // / for IO operations
18
17
#include < queue> // / for std::priority_queue
@@ -26,8 +25,6 @@ using std::get;
26
25
using std::left;
27
26
using std::make_tuple;
28
27
using std::priority_queue;
29
- using std::rand;
30
- using std::srand;
31
28
using std::tuple;
32
29
using std::unordered_set;
33
30
using std::vector;
@@ -37,8 +34,8 @@ using std::vector;
37
34
* @tparam S Data type of Process ID
38
35
* @tparam T Data type of Arrival time
39
36
* @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
42
39
* @returns true if t1 and t2 are in the CORRECT order
43
40
* @returns false if t1 and t2 are in the INCORRECT order
44
41
*/
@@ -260,27 +257,55 @@ class SJF {
260
257
* @returns void
261
258
*/
262
259
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);
266
269
SJF<uint32_t , uint32_t , uint32_t > readyQueue;
267
270
vector<tuple<uint32_t , uint32_t , uint32_t >> input (n);
268
271
272
+ // Generate random arrival and burst times
269
273
for (uint32_t i{}; i < n; i++) {
270
274
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
275
277
}
276
278
279
+ // Add processes to the queue
277
280
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]));
280
282
}
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 ());
283
289
}
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
+
284
309
cout << " All the tests have successfully passed!" << endl;
285
310
}
286
311
0 commit comments