TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
non_preemptive_sjf_scheduling.cpp File Reference

Implementation of SJF CPU scheduling algorithm. More...

#include <algorithm>
#include <cassert>
#include <iomanip>
#include <iostream>
#include <queue>
#include <random>
#include <unordered_set>
#include <vector>
Include dependency graph for non_preemptive_sjf_scheduling.cpp:

Go to the source code of this file.

Classes

class  Compare< S, T, E >
 Comparator class for priority queue. More...
 
class  SJF< S, T, E >
 Class which implements the SJF scheduling algorithm. More...
 

Functions

template<typename S , typename T , typename E >
bool sortcol (tuple< S, T, E > &t1, tuple< S, T, E > &t2)
 Comparator function for sorting a vector.
 
template<typename S , typename T , typename E >
vector< tuple< S, T, E, double, double, double > > get_final_status (vector< tuple< S, T, E > > input)
 Computes the final status of processes after applying non-preemptive SJF scheduling.
 
static void test ()
 Self-test implementations.
 
int main ()
 Main function.
 

Detailed Description

Implementation of SJF CPU scheduling algorithm.

shortest job first (SJF), also known as shortest job next (SJN), is a 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. detailed description on SJF scheduling Author : Lakshmi Srikumar

Definition in file non_preemptive_sjf_scheduling.cpp.

Function Documentation

◆ get_final_status()

template<typename S , typename T , typename E >
vector< tuple< S, T, E, double, double, double > > get_final_status ( vector< tuple< S, T, E > > input)

Computes the final status of processes after applying non-preemptive SJF scheduling.

Template Parameters
SData type of Process ID
TData type of Arrival time
EData type of Burst time
Parameters
inputA vector of tuples containing Process ID, Arrival time, and Burst time
Returns
A vector of tuples containing Process ID, Arrival time, Burst time, Completion time, Turnaround time, and Waiting time

Definition at line 228 of file non_preemptive_sjf_scheduling.cpp.

229 {
230 // Sort the processes based on Arrival time and then Burst time
231 sort(input.begin(), input.end(), sortcol<S, T, E>);
232
233 // Result vector to hold the final status of each process
234 vector<tuple<S, T, E, double, double, double>> result(input.size());
235 double timeElapsed = 0;
236
237 for (size_t i = 0; i < input.size(); i++) {
238 // Extract Arrival time and Burst time
239 T arrival = get<1>(input[i]);
240 E burst = get<2>(input[i]);
241
242 // If the CPU is idle, move time to the arrival of the next process
243 if (arrival > timeElapsed) {
244 timeElapsed = arrival;
245 }
246
247 // Update timeElapsed by adding the burst time
248 timeElapsed += burst;
249
250 // Calculate Completion time, Turnaround time, and Waiting time
251 double completion = timeElapsed;
252 double turnaround = completion - arrival;
253 double waiting = turnaround - burst;
254
255 // Store the results in the result vector
256 result[i] = make_tuple(get<0>(input[i]), arrival, burst, completion,
257 turnaround, waiting);
258 }
259
260 return result;
261}
uint64_t result(uint64_t n)
bool sortcol(tuple< S, T, E > &t1, tuple< S, T, E > &t2)
Comparator function for sorting a vector.

◆ main()

int main ( void )

Main function.

Returns
0 on successful exit

Definition at line 313 of file non_preemptive_sjf_scheduling.cpp.

313 {
314 test();
315 return 0;
316}
static void test()
Self-test implementations.

◆ sortcol()

template<typename S , typename T , typename E >
bool sortcol ( tuple< S, T, E > & t1,
tuple< S, T, E > & t2 )

Comparator function for sorting a vector.

Template Parameters
SData type of Process ID
TData type of Arrival time
EData type of Burst time
Parameters
t1First tuple<S,T,E>t1
t2Second tuple<S,T,E>t2
Returns
true if t1 and t2 are in the CORRECT order
false if t1 and t2 are in the INCORRECT order

Definition at line 45 of file non_preemptive_sjf_scheduling.cpp.

45 {
46 if (get<1>(t1) < get<1>(t2) ||
47 (get<1>(t1) == get<1>(t2) && get<0>(t1) < get<0>(t2))) {
48 return true;
49 }
50 return false;
51}

◆ test()

static void test ( )
static

Self-test implementations.

Returns
void

Definition at line 267 of file non_preemptive_sjf_scheduling.cpp.

267 {
268 // A vector to store the results of all processes across all test cases.
269 vector<tuple<uint32_t, uint32_t, uint32_t, double, double, double>>
270 finalResult;
271
272 for (int i{}; i < 10; i++) {
273 std::random_device rd; // Seeding
274 std::mt19937 eng(rd());
275 std::uniform_int_distribution<> distr(1, 10);
276
277 uint32_t n = distr(eng);
279 vector<tuple<uint32_t, uint32_t, uint32_t, double, double, double>>
280 input(n);
281
282 // Generate random arrival and burst times
283 for (uint32_t i{}; i < n; i++) {
284 get<0>(input[i]) = i;
285 get<1>(input[i]) = distr(eng); // Random arrival time
286 get<2>(input[i]) = distr(eng); // Random burst time
287 }
288
289 // Print processes before scheduling
290 cout << "Processes before SJF scheduling:" << endl;
291 readyQueue.printResult(input);
292
293 // Add processes to the queue
294 for (uint32_t i{}; i < n; i++) {
295 readyQueue.addProcess(get<0>(input[i]), get<1>(input[i]),
296 get<2>(input[i]));
297 }
298
299 // Perform SJF schedulings
300 auto finalResult = readyQueue.scheduleForSJF();
301
302 // Print processes after scheduling
303 cout << "\nProcesses after SJF scheduling:" << endl;
304 readyQueue.printResult(finalResult);
305 }
306 cout << "All the tests have successfully passed!" << endl;
307}
Class which implements the SJF scheduling algorithm.
void printResult(const vector< tuple< S, T, E, double, double, double > > &processes)
Utility function for printing the status of each process after execution.
vector< tuple< S, T, E, double, double, double > > scheduleForSJF()
Algorithm for scheduling CPU processes according to the Shortest Job First (SJF) scheduling algorithm...
void addProcess(S id, T arrival, E burst)
Adds the process to the ready queue if it isn't already there.
#define endl