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>
|
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. More...
|
|
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. More...
|
|
static void | test () |
| Self-test implementations. More...
|
|
int | main () |
| Entry point of the program. More...
|
|
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
◆ 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
-
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 {
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 }
250}
uint64_t result(uint64_t n)
Definition: fibonacci_sum.cpp:76
◆ main()
Entry point of the program.
- Returns
- 0 on exit
287 {
289 return 0;
290}
static void test()
Self-test implementations.
Definition: fcfs_scheduling.cpp:256
◆ 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
-
S | Data type of Process ID |
T | Data type of Arrival time |
E | Data type of Burst time |
- Parameters
-
t1 | First tuple |
t2 | Second 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()
Self-test implementations.
- Returns
- void
256 {
257 for (int i{}; i < 1000; i++) {
259 uint32_t n = 1 +
rand() % 1000;
262
263 for (uint32_t i{}; i < n; i++) {
264 get<0>(input[i]) = i;
266 get<1>(input[i]) = 1 +
rand() % 10000;
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);
278
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