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 295f0cc6
This commit is contained in:
parent
295f0cc6d1
commit
07afee4b94
@ -2,17 +2,18 @@
|
||||
* @file fcfs_scheduling.cpp
|
||||
* @brief Implementation of FCFS CPU scheduling algorithm
|
||||
* @details
|
||||
* 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.
|
||||
* 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.
|
||||
* @link https://bit.ly/3ABNXOC
|
||||
* @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 <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::cout;
|
||||
@ -38,11 +39,15 @@ class Compare{
|
||||
/**
|
||||
* @param t1: first tuple
|
||||
* @param t2: second tuple
|
||||
* @brief A comparator function that checks whether to swap the two tuples or not.
|
||||
* @link Refer https://www.geeksforgeeks.org/comparator-class-in-c-with-examples/ for detailed description of comparator
|
||||
* @brief A comparator function that checks whether to swap the two tuples
|
||||
* or not.
|
||||
* @link Refer
|
||||
* https://www.geeksforgeeks.org/comparator-class-in-c-with-examples/ for
|
||||
* detailed description of comparator
|
||||
* @returns true if the tuples should be swapped, false othewise
|
||||
*/
|
||||
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
|
||||
if (get<1>(t2) < get<1>(t1)) {
|
||||
return true;
|
||||
@ -82,7 +87,8 @@ class FCFS{
|
||||
// Stores final status of all the processes after completing the execution.
|
||||
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;
|
||||
|
||||
public:
|
||||
@ -97,20 +103,25 @@ class FCFS{
|
||||
void addProcess(S id, T arrival, E burst) {
|
||||
// Add if a process with process ID as id is not found in idList.
|
||||
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);
|
||||
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.
|
||||
*
|
||||
* @description FCFS is a non-preemptive algorithm in which the process which 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).
|
||||
* @description FCFS is a non-preemptive algorithm in which the process
|
||||
* which 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
|
||||
* times of some processes are equal, then they are ordered by their process ID.
|
||||
* I used a min priority queue of tuples to accomplish this task. The
|
||||
* 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
|
||||
*/
|
||||
@ -130,7 +141,8 @@ class FCFS{
|
||||
// Add Burst time to time elapsed
|
||||
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;
|
||||
|
||||
// Turnaround time = Completion time - Arrival time
|
||||
@ -146,27 +158,28 @@ 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
|
||||
*/
|
||||
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"
|
||||
<< std::setw(17) << left << "Arrival Time"
|
||||
<< std::setw(17) << left << "Burst Time"
|
||||
<< std::setw(17) << left << "Completion Time"
|
||||
<< std::setw(17) << left << "Turnaround Time"
|
||||
<< std::setw(17) << left << "Waiting Time" << endl;
|
||||
cout << std::setw(17) << left << "Process ID" << std::setw(17) << left
|
||||
<< "Arrival Time" << std::setw(17) << left << "Burst Time"
|
||||
<< std::setw(17) << left << "Completion Time" << std::setw(17)
|
||||
<< left << "Turnaround Time" << std::setw(17) << left
|
||||
<< "Waiting Time" << endl;
|
||||
|
||||
for (size_t i{}; i < result.size(); i++) {
|
||||
cout << std::setprecision(2) << std::fixed
|
||||
<< std::setw(17) << left << get<0>(result[i])
|
||||
<< std::setw(17) << left << get<1>(result[i])
|
||||
<< std::setw(17) << left << get<2>(result[i])
|
||||
<< std::setw(17) << left << get<3>(result[i])
|
||||
<< std::setw(17) << left << get<4>(result[i])
|
||||
<< std::setw(17) << left << get<5>(result[i]) << endl;
|
||||
cout << std::setprecision(2) << std::fixed << std::setw(17) << left
|
||||
<< get<0>(result[i]) << std::setw(17) << left
|
||||
<< get<1>(result[i]) << std::setw(17) << left
|
||||
<< get<2>(result[i]) << std::setw(17) << left
|
||||
<< get<3>(result[i]) << std::setw(17) << left
|
||||
<< get<4>(result[i]) << std::setw(17) << left
|
||||
<< get<5>(result[i]) << endl;
|
||||
}
|
||||
}
|
||||
};
|
||||
@ -181,13 +194,11 @@ int main(){
|
||||
// Sample test case
|
||||
int n = 3;
|
||||
vector<tuple<uint32_t, uint32_t, uint32_t>> input = {
|
||||
make_tuple(1, 0, 30),
|
||||
make_tuple(2, 0, 5),
|
||||
make_tuple(3, 0, 5)
|
||||
};
|
||||
make_tuple(1, 0, 30), make_tuple(2, 0, 5), make_tuple(3, 0, 5)};
|
||||
|
||||
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]));
|
||||
}
|
||||
|
||||
readyQueue.scheduleForFcfs();
|
||||
|
Loading…
Reference in New Issue
Block a user