mirror of
https://hub.njuu.cf/TheAlgorithms/C-Plus-Plus.git
synced 2023-10-11 13:05:55 +08:00
clang-format and clang-tidy fixes for 40d663d3
This commit is contained in:
parent
40d663d358
commit
3c639313a4
@ -2,34 +2,35 @@
|
|||||||
* @file fcfs_scheduling.cpp
|
* @file fcfs_scheduling.cpp
|
||||||
* @brief Implementation of FCFS CPU scheduling algorithm
|
* @brief Implementation of FCFS CPU scheduling algorithm
|
||||||
* @details
|
* @details
|
||||||
* FCFS is a non-preemptive CPU scheduling algorithm in which whichever process arrives first, gets executed first.
|
* FCFS is a non-preemptive CPU scheduling algorithm in which whichever process
|
||||||
* If two or more processes arrive simultaneously, the process with smaller process ID gets executed first.
|
* arrives first, gets executed first. If two or more processes arrive
|
||||||
|
* simultaneously, the process with smaller process ID gets executed first.
|
||||||
* @link https://bit.ly/3ABNXOC
|
* @link https://bit.ly/3ABNXOC
|
||||||
* @author Pratyush Vatsa(https://github.com/Pratyush219)
|
* @author Pratyush Vatsa(https://github.com/Pratyush219)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <iostream> // for IO operations
|
|
||||||
#include <vector> // for using vector
|
|
||||||
#include <unordered_set> // for using unordered_set
|
|
||||||
#include <queue> // for priority_queue
|
|
||||||
#include <iomanip> // for formatting the output
|
|
||||||
#include <cstdlib> // random number generation
|
|
||||||
#include <algorithm> // for sorting
|
#include <algorithm> // for sorting
|
||||||
#include <cassert> //for assert
|
#include <cassert> //for assert
|
||||||
|
#include <cstdlib> // random number generation
|
||||||
#include <ctime> // for time
|
#include <ctime> // for time
|
||||||
|
#include <iomanip> // for formatting the output
|
||||||
|
#include <iostream> // for IO operations
|
||||||
|
#include <queue> // for priority_queue
|
||||||
|
#include <unordered_set> // for using unordered_set
|
||||||
|
#include <vector> // for using vector
|
||||||
|
|
||||||
using std::cin;
|
using std::cin;
|
||||||
using std::cout;
|
using std::cout;
|
||||||
using std::get;
|
|
||||||
using std::priority_queue;
|
|
||||||
using std::unordered_set;
|
|
||||||
using std::make_tuple;
|
|
||||||
using std::vector;
|
|
||||||
using std::tuple;
|
|
||||||
using std::endl;
|
using std::endl;
|
||||||
|
using std::get;
|
||||||
using std::left;
|
using std::left;
|
||||||
|
using std::make_tuple;
|
||||||
|
using std::priority_queue;
|
||||||
using std::rand;
|
using std::rand;
|
||||||
using std::srand;
|
using std::srand;
|
||||||
|
using std::tuple;
|
||||||
|
using std::unordered_set;
|
||||||
|
using std::vector;
|
||||||
/**
|
/**
|
||||||
* @brief Comparator function for sorting of vector
|
* @brief Comparator function for sorting of vector
|
||||||
* @tparam S Data type of Process ID
|
* @tparam S Data type of Process ID
|
||||||
@ -44,8 +45,7 @@ template<typename S, typename T, typename E>
|
|||||||
bool sortcol(tuple<S, T, E>& t1, tuple<S, T, E>& t2) {
|
bool sortcol(tuple<S, T, E>& t1, tuple<S, T, E>& t2) {
|
||||||
if (get<1>(t1) < get<1>(t2)) {
|
if (get<1>(t1) < get<1>(t2)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
} else if (get<1>(t1) == get<1>(t2) && get<0>(t1) < get<0>(t2)) {
|
||||||
else if(get<1>(t1) == get<1>(t2) && get<0>(t1) < get<0>(t2)){
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
@ -64,12 +64,16 @@ class Compare{
|
|||||||
/**
|
/**
|
||||||
* @param t1 first tuple
|
* @param t1 first tuple
|
||||||
* @param t2 second tuple
|
* @param t2 second tuple
|
||||||
* @brief A comparator function that checks whether to swap the two tuples or not.
|
* @brief A comparator function that checks whether to swap the two tuples
|
||||||
* @link Refer to https://www.geeksforgeeks.org/comparator-class-in-c-with-examples/ for detailed description of comparator
|
* or not.
|
||||||
|
* @link Refer to
|
||||||
|
* https://www.geeksforgeeks.org/comparator-class-in-c-with-examples/ for
|
||||||
|
* detailed description of comparator
|
||||||
* @returns true if the tuples SHOULD be swapped
|
* @returns true if the tuples SHOULD be swapped
|
||||||
* @returns false if the tuples SHOULDN'T be swapped
|
* @returns false if the tuples SHOULDN'T be swapped
|
||||||
*/
|
*/
|
||||||
bool operator () (tuple<S, T, E, double, double, double>& t1, tuple<S, T, E, double, double, double>& t2){
|
bool operator()(tuple<S, T, E, double, double, double>& t1,
|
||||||
|
tuple<S, T, E, double, double, double>& t2) {
|
||||||
// Compare arrival times
|
// Compare arrival times
|
||||||
if (get<1>(t2) < get<1>(t1)) {
|
if (get<1>(t2) < get<1>(t1)) {
|
||||||
return true;
|
return true;
|
||||||
@ -101,13 +105,18 @@ class FCFS{
|
|||||||
* 5th element: Turnaround time
|
* 5th element: Turnaround time
|
||||||
* 6th element: Waiting time
|
* 6th element: Waiting time
|
||||||
*/
|
*/
|
||||||
priority_queue<tuple<S, T, E, double, double, double>, vector<tuple<S, T, E, double, double, double>>, Compare<S, T, E>> schedule;
|
priority_queue<tuple<S, T, E, double, double, double>,
|
||||||
|
vector<tuple<S, T, E, double, double, double>>,
|
||||||
|
Compare<S, T, E>>
|
||||||
|
schedule;
|
||||||
|
|
||||||
// Stores final status of all the processes after completing the execution.
|
// Stores final status of all the processes after completing the execution.
|
||||||
vector<tuple<S, T, E, double, double, double>> result;
|
vector<tuple<S, T, E, double, double, double>> result;
|
||||||
|
|
||||||
// Stores process IDs. Used for confirming absence of a process while adding it.
|
// Stores process IDs. Used for confirming absence of a process while adding
|
||||||
|
// it.
|
||||||
unordered_set<S> idList;
|
unordered_set<S> idList;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
/**
|
/**
|
||||||
* @brief adds the process to the ready queue if it isn't already there
|
* @brief adds the process to the ready queue if it isn't already there
|
||||||
@ -120,21 +129,25 @@ class FCFS{
|
|||||||
void addProcess(S id, T arrival, E burst) {
|
void addProcess(S id, T arrival, E burst) {
|
||||||
// Add if a process with process ID as id is not found in idList.
|
// Add if a process with process ID as id is not found in idList.
|
||||||
if (idList.find(id) == idList.end()) {
|
if (idList.find(id) == idList.end()) {
|
||||||
tuple<S, T, E, double, double, double> t = make_tuple(id, arrival, burst, 0, 0, 0);
|
tuple<S, T, E, double, double, double> t =
|
||||||
|
make_tuple(id, arrival, burst, 0, 0, 0);
|
||||||
schedule.push(t);
|
schedule.push(t);
|
||||||
idList.insert(id);
|
idList.insert(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Algorithm for scheduling CPU processes according to the First Come First Serve(FCFS) scheduling algorithm.
|
* @brief Algorithm for scheduling CPU processes according to the First Come
|
||||||
|
* First Serve(FCFS) scheduling algorithm.
|
||||||
*
|
*
|
||||||
* @details FCFS is a non-preemptive algorithm in which the process which arrives first gets executed first. If two or
|
* @details FCFS is a non-preemptive algorithm in which the process which
|
||||||
* more processes arrive together then the process with smaller process ID runs first (each process has a unique proces ID).
|
* arrives first gets executed first. If two or more processes arrive
|
||||||
|
* together then the process with smaller process ID runs first (each
|
||||||
|
* process has a unique proces ID).
|
||||||
*
|
*
|
||||||
* I used a min priority queue of tuples to accomplish this task. The processes are ordered by their arrival times. If arrival
|
* I used a min priority queue of tuples to accomplish this task. The
|
||||||
* times of some processes are equal, then they are ordered by their process ID.
|
* processes are ordered by their arrival times. If arrival times of some
|
||||||
|
* processes are equal, then they are ordered by their process ID.
|
||||||
*
|
*
|
||||||
* @returns void
|
* @returns void
|
||||||
*/
|
*/
|
||||||
@ -145,7 +158,8 @@ class FCFS{
|
|||||||
while (!schedule.empty()) {
|
while (!schedule.empty()) {
|
||||||
tuple<S, T, E, double, double, double> cur = schedule.top();
|
tuple<S, T, E, double, double, double> cur = schedule.top();
|
||||||
|
|
||||||
// If the current process arrived at time t2, the last process completed its execution at time t1, and t2 > t1.
|
// If the current process arrived at time t2, the last process
|
||||||
|
// completed its execution at time t1, and t2 > t1.
|
||||||
if (get<1>(cur) > timeElapsed) {
|
if (get<1>(cur) > timeElapsed) {
|
||||||
timeElapsed += get<1>(cur) - timeElapsed;
|
timeElapsed += get<1>(cur) - timeElapsed;
|
||||||
}
|
}
|
||||||
@ -153,7 +167,8 @@ class FCFS{
|
|||||||
// Add Burst time to time elapsed
|
// Add Burst time to time elapsed
|
||||||
timeElapsed += get<2>(cur);
|
timeElapsed += get<2>(cur);
|
||||||
|
|
||||||
// Completion time of the current process will be same as time elapsed so far
|
// Completion time of the current process will be same as time
|
||||||
|
// elapsed so far
|
||||||
get<3>(cur) = timeElapsed;
|
get<3>(cur) = timeElapsed;
|
||||||
|
|
||||||
// Turnaround time = Completion time - Arrival time
|
// Turnaround time = Completion time - Arrival time
|
||||||
@ -169,42 +184,46 @@ class FCFS{
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Utility function for printing the status of each process after execution
|
* @brief Utility function for printing the status of each process after
|
||||||
|
* execution
|
||||||
* @returns void
|
* @returns void
|
||||||
*/
|
*/
|
||||||
void printResult() {
|
void printResult() {
|
||||||
cout << "Status of all the proceses post completion is as follows:" << endl;
|
cout << "Status of all the proceses post completion is as follows:"
|
||||||
|
<< endl;
|
||||||
|
|
||||||
cout << std::setw(17) << left << "Process ID"
|
cout << std::setw(17) << left << "Process ID" << std::setw(17) << left
|
||||||
<< std::setw(17) << left << "Arrival Time"
|
<< "Arrival Time" << std::setw(17) << left << "Burst Time"
|
||||||
<< std::setw(17) << left << "Burst Time"
|
<< std::setw(17) << left << "Completion Time" << std::setw(17)
|
||||||
<< std::setw(17) << left << "Completion Time"
|
<< left << "Turnaround Time" << std::setw(17) << left
|
||||||
<< std::setw(17) << left << "Turnaround Time"
|
<< "Waiting Time" << endl;
|
||||||
<< std::setw(17) << left << "Waiting Time" << endl;
|
|
||||||
|
|
||||||
for (size_t i{}; i < result.size(); i++) {
|
for (size_t i{}; i < result.size(); i++) {
|
||||||
cout << std::setprecision(2) << std::fixed
|
cout << std::setprecision(2) << std::fixed << std::setw(17) << left
|
||||||
<< std::setw(17) << left << get<0>(result[i])
|
<< get<0>(result[i]) << std::setw(17) << left
|
||||||
<< std::setw(17) << left << get<1>(result[i])
|
<< get<1>(result[i]) << std::setw(17) << left
|
||||||
<< std::setw(17) << left << get<2>(result[i])
|
<< get<2>(result[i]) << std::setw(17) << left
|
||||||
<< std::setw(17) << left << get<3>(result[i])
|
<< get<3>(result[i]) << std::setw(17) << left
|
||||||
<< std::setw(17) << left << get<4>(result[i])
|
<< get<4>(result[i]) << std::setw(17) << left
|
||||||
<< std::setw(17) << left << get<5>(result[i]) << endl;
|
<< get<5>(result[i]) << endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief function to be used for testing purposes. This function guarantees the correct solution for FCFS scheduling algorithm.
|
* @brief function to be used for testing purposes. This function guarantees the
|
||||||
|
* correct solution for FCFS scheduling algorithm.
|
||||||
* @param input the input data
|
* @param input the input data
|
||||||
* @details Sorts the input vector according to arrival time. Processes whose arrival times are same get sorted according to process ID
|
* @details Sorts the input vector according to arrival time. Processes whose
|
||||||
* For each process, completion time, turnaround time and completion time are calculated, inserted in a tuple, which is added to the vector result.
|
* arrival times are same get sorted according to process ID For each process,
|
||||||
* @returns a vector of tuples consisting of process ID, arrival time, burst time, completion time, turnaround time and waiting time 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.
|
||||||
*/
|
*/
|
||||||
template <typename S, typename T, typename E>
|
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){
|
vector<tuple<S, T, E, double, double, double>> get_final_status(
|
||||||
|
vector<tuple<uint32_t, uint32_t, uint32_t>> input) {
|
||||||
sort(input.begin(), input.end(), sortcol<uint32_t, uint32_t, uint32_t>);
|
sort(input.begin(), input.end(), sortcol<uint32_t, uint32_t, uint32_t>);
|
||||||
vector<tuple<S, T, E, double, double, double>> result(input.size());
|
vector<tuple<S, T, E, double, double, double>> result(input.size());
|
||||||
double timeElapsed = 0;
|
double timeElapsed = 0;
|
||||||
@ -226,7 +245,6 @@ vector<tuple<S, T, E, double, double, double>> get_final_status(vector<tuple<uin
|
|||||||
get<3>(result[i]) = completion;
|
get<3>(result[i]) = completion;
|
||||||
get<4>(result[i]) = turnaround;
|
get<4>(result[i]) = turnaround;
|
||||||
get<5>(result[i]) = waiting;
|
get<5>(result[i]) = waiting;
|
||||||
|
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@ -247,14 +265,15 @@ void test(){
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (uint32_t i{}; i < n; i++) {
|
for (uint32_t i{}; i < n; i++) {
|
||||||
readyQueue.addProcess(get<0>(input[i]), get<1>(input[i]), get<2>(input[i]));
|
readyQueue.addProcess(get<0>(input[i]), get<1>(input[i]),
|
||||||
|
get<2>(input[i]));
|
||||||
}
|
}
|
||||||
vector<tuple<uint32_t ,uint32_t, uint32_t, double, double, double>> res = get_final_status<uint32_t ,uint32_t, uint32_t>(input);
|
vector<tuple<uint32_t, uint32_t, uint32_t, double, double, double>>
|
||||||
|
res = get_final_status<uint32_t, uint32_t, uint32_t>(input);
|
||||||
assert(res == readyQueue.scheduleForFcfs());
|
assert(res == readyQueue.scheduleForFcfs());
|
||||||
// readyQueue.printResult();
|
// readyQueue.printResult();
|
||||||
}
|
}
|
||||||
cout << "All tests passed" << endl;
|
cout << "All tests passed" << endl;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user