TheAlgorithms/C++ 1.0.0
All the 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 <cstdint>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <queue>
#include <unordered_set>
#include <vector>
Include dependency graph for fcfs_scheduling.cpp:

Go to the source code of this file.

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

Definition in file fcfs_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< 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.

Definition at line 226 of file fcfs_scheduling.cpp.

227 {
228 sort(input.begin(), input.end(), sortcol<S, T, E>);
229 vector<tuple<S, T, E, double, double, double>> result(input.size());
230 double timeElapsed = 0;
231 for (size_t i{}; i < input.size(); i++) {
232 T arrival = get<1>(input[i]);
233 E burst = get<2>(input[i]);
234
235 if (arrival > timeElapsed) {
236 timeElapsed += arrival - timeElapsed;
237 }
238 timeElapsed += burst;
239 double completion = timeElapsed;
240 double turnaround = completion - arrival;
241 double waiting = turnaround - burst;
242
243 get<0>(result[i]) = get<0>(input[i]);
244 get<1>(result[i]) = arrival;
245 get<2>(result[i]) = burst;
246 get<3>(result[i]) = completion;
247 get<4>(result[i]) = turnaround;
248 get<5>(result[i]) = waiting;
249 }
250 return result;
251}
bool sortcol(tuple< S, T, E > &t1, tuple< S, T, E > &t2)
Comparator function for sorting a vector.
uint64_t result(uint64_t n)

◆ main()

int main ( void )

Entry point of the program.

Returns
0 on exit

Definition at line 288 of file fcfs_scheduling.cpp.

288 {
289 test(); // run self-test implementations
290 return 0;
291}
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
t2Second tuple
Returns
true if t1 and t2 are in the CORRECT order
false if t1 and t2 are in the INCORRECT order

Definition at line 46 of file fcfs_scheduling.cpp.

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

◆ test()

static void test ( )
static

Self-test implementations.

Returns
void

Definition at line 257 of file fcfs_scheduling.cpp.

257 {
258 for (int i{}; i < 1000; i++) {
259 srand(time(nullptr));
260 uint32_t n = 1 + rand() % 1000;
262 vector<tuple<uint32_t, uint32_t, uint32_t>> input(n);
263
264 for (uint32_t i{}; i < n; i++) {
265 get<0>(input[i]) = i;
266 srand(time(nullptr));
267 get<1>(input[i]) = 1 + rand() % 10000;
268 srand(time(nullptr));
269 get<2>(input[i]) = 1 + rand() % 10000;
270 }
271
272 for (uint32_t i{}; i < n; i++) {
273 readyQueue.addProcess(get<0>(input[i]), get<1>(input[i]),
274 get<2>(input[i]));
275 }
276 vector<tuple<uint32_t, uint32_t, uint32_t, double, double, double>>
278 assert(res == readyQueue.scheduleForFcfs());
279 // readyQueue.printResult();
280 }
281 cout << "All the tests have successfully passed!" << endl;
282}
Class which implements the FCFS scheduling algorithm.
void addProcess(S id, T arrival, E burst)
Adds the process to the ready queue if it isn't already there.
vector< tuple< S, T, E, double, double, double > > scheduleForFcfs()
Algorithm for scheduling CPU processes according to the First Come First Serve(FCFS) scheduling algor...
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 sche...
#define endl