Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
Loading...
Searching...
No Matches
fcfs_scheduling.cpp File Reference

Implementation of FCFS CPU scheduling algorithm. More...

#include <algorithm>
#include <cassert>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <queue>
#include <unordered_set>
#include <vector>
Include dependency graph for fcfs_scheduling.cpp:

Classes

class  Compare< S, T, E >
 Comparator class for priority queue. More...
 
class  FCFS< S, T, E >
 Class which implements the FCFS 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< uint32_t, uint32_t, uint32_t > > input)
 Function to be used for testing purposes. This function guarantees the correct solution for FCFS scheduling algorithm.
 
static void test ()
 Self-test implementations.
 
int main ()
 Entry point of the program.
 

Detailed Description

Implementation of FCFS CPU scheduling algorithm.

FCFS is a non-preemptive CPU scheduling algorithm in which whichever process arrives first, gets executed first. If two or more processes arrive simultaneously, the process with smaller process ID gets executed first. https://bit.ly/3ABNXOCPratyush Vatsa

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< uint32_t, uint32_t, uint32_t > > input)

Function to be used for testing purposes. This function guarantees the correct solution for FCFS scheduling algorithm.

Parameters
inputthe input data

Sorts the input vector according to arrival time. Processes whose arrival times are same get sorted according to process ID For each process, completion time, turnaround time and completion time are calculated, inserted in a tuple, which is added to the vector result.

Returns
A vector of tuples consisting of process ID, arrival time, burst time, completion time, turnaround time and waiting time for each process.
226 {
227 sort(input.begin(), input.end(), sortcol<S, T, E>);
229 double timeElapsed = 0;
230 for (size_t i{}; i < input.size(); i++) {
231 T arrival = get<1>(input[i]);
232 E burst = get<2>(input[i]);
233
234 if (arrival > timeElapsed) {
235 timeElapsed += arrival - timeElapsed;
236 }
237 timeElapsed += burst;
238 double completion = timeElapsed;
239 double turnaround = completion - arrival;
240 double waiting = turnaround - burst;
241
242 get<0>(result[i]) = get<0>(input[i]);
243 get<1>(result[i]) = arrival;
244 get<2>(result[i]) = burst;
245 get<3>(result[i]) = completion;
246 get<4>(result[i]) = turnaround;
247 get<5>(result[i]) = waiting;
248 }
249 return result;
250}
T begin(T... args)
T end(T... args)
uint64_t result(uint64_t n)
Definition fibonacci_sum.cpp:76
T size(T... args)
T sort(T... args)

◆ main()

int main ( void )

Entry point of the program.

Returns
0 on exit
287 {
288 test(); // run self-test implementations
289 return 0;
290}
static void test()
Self-test implementations.
Definition fcfs_scheduling.cpp:256
Here is the call graph for this function:

◆ 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
t2Second tuple
Returns
true if t1 and t2 are in the CORRECT order
false if t1 and t2 are in the INCORRECT order
45 {
46 if (get<1>(t1) < get<1>(t2)) {
47 return true;
48 } else if (get<1>(t1) == get<1>(t2) && get<0>(t1) < get<0>(t2)) {
49 return true;
50 }
51 return false;
52}

◆ test()

static void test ( )
static

Self-test implementations.

Returns
void
256 {
257 for (int i{}; i < 1000; i++) {
258 srand(time(nullptr));
259 uint32_t n = 1 + rand() % 1000;
262
263 for (uint32_t i{}; i < n; i++) {
264 get<0>(input[i]) = i;
265 srand(time(nullptr));
266 get<1>(input[i]) = 1 + rand() % 10000;
267 srand(time(nullptr));
268 get<2>(input[i]) = 1 + rand() % 10000;
269 }
270
271 for (uint32_t i{}; i < n; i++) {
272 readyQueue.addProcess(get<0>(input[i]), get<1>(input[i]),
273 get<2>(input[i]));
274 }
276 res = get_final_status<uint32_t, uint32_t, uint32_t>(input);
277 assert(res == readyQueue.scheduleForFcfs());
278 // readyQueue.printResult();
279 }
280 cout << "All the tests have successfully passed!" << endl;
281}
Class which implements the FCFS scheduling algorithm.
Definition fcfs_scheduling.cpp:97
void addProcess(S id, T arrival, E burst)
Adds the process to the ready queue if it isn't already there.
Definition fcfs_scheduling.cpp:129
vector< tuple< S, T, E, double, double, double > > scheduleForFcfs()
Algorithm for scheduling CPU processes according to the First Come First Serve(FCFS) scheduling algor...
Definition fcfs_scheduling.cpp:154
#define endl
Definition matrix_exponentiation.cpp:36
T time(T... args)
Here is the call graph for this function: