A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://TheAlgorithms.github.io/C-Plus-Plus/df/d47/fcfs__scheduling_8cpp.html below:

TheAlgorithms/C++: cpu_scheduling_algorithms/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>

Go to the source code of this file.

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.

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.

◆ 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.

Definition at line 226 of file fcfs_scheduling.cpp.

227 {

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 }

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()

Entry point of the program.

Returns
0 on exit

Definition at line 288 of file fcfs_scheduling.cpp.

288 {

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
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

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()

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>>

279

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...


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4