From 9ff928ff589b92221bd0126cb71cc1987b99098e Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Thu, 7 Oct 2021 05:42:24 +0000 Subject: [PATCH] clang-format and clang-tidy fixes for c95495b4 --- cpu_scheduling_algorithms/fcfs_scheduling.cpp | 170 +++++++++--------- 1 file changed, 88 insertions(+), 82 deletions(-) diff --git a/cpu_scheduling_algorithms/fcfs_scheduling.cpp b/cpu_scheduling_algorithms/fcfs_scheduling.cpp index bba34c0dd..a3ee90c95 100644 --- a/cpu_scheduling_algorithms/fcfs_scheduling.cpp +++ b/cpu_scheduling_algorithms/fcfs_scheduling.cpp @@ -1,41 +1,41 @@ -#include -#include -#include -#include #include #include #include +#include +#include +#include +#include using std::cin; 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::get; using std::left; +using std::make_tuple; +using std::priority_queue; +using std::tuple; +using std::unordered_set; +using std::vector; -/** +/** * @brief Comparator class for Priority queue * S: Data type of Process id * T: Data type of Arrival time - * E: Data type of Burst time -*/ -template -class Compare{ - public: + * E: Data type of Burst time + */ +template +class Compare { + public: /** * @param t1 = first tuple - * @param t2 = second tuple + * @param t2 = second tuple * @brief checks whether to swap two tuples or not - */ - bool operator () (tuple& t1, tuple& t2){ - if(get<1>(t2) < get<1>(t1)){ + */ + bool operator()(tuple& t1, + tuple& t2) { + if (get<1>(t2) < get<1>(t1)) { return true; - } - else if(get<1>(t2) == get<1>(t1)){ + } else if (get<1>(t2) == get<1>(t1)) { return get<0>(t2) < get<0>(t1); } return false; @@ -46,10 +46,10 @@ class Compare{ * @brief Class which implements the FCFS scheduling algorithm * S: Data type of Process id * T: Data type of Arrival time - * E: Data type of Burst time -*/ -template -class FCFS{ + * E: Data type of Burst time + */ +template +class FCFS { /** * Priority queue of schedules(stored as tuples) of processes. * In each tuple @@ -59,62 +59,72 @@ class FCFS{ * 4th element: Completion time * 5th element: Turnaround time * 6th element: Waiting time - */ - priority_queue, vector>, Compare> schedule; + */ + priority_queue, + vector>, + Compare> + schedule; // Stores final status of all the processes after completing execution. - vector> result; - - // Stores process ids. Used for confirming absence of a process while adding it. + vector> result; + + // Stores process ids. Used for confirming absence of a process while adding + // it. unordered_set idList; - public: + + public: /** * @brief add the process to the ready queue if it isn't already there * @param id: Process id * @param arrival: Arrival time of the process * @param burst: Burst time of the process - * - */ - void addProcess(S id, T arrival, E burst){ + * + */ + void addProcess(S id, T arrival, E burst) { // Add if process with process id not found in idList. - if(idList.find(id) == idList.end()) { - tuple t = make_tuple(id, arrival, burst, 0, 0, 0); + if (idList.find(id) == idList.end()) { + tuple 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. - * - * @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. - */ - void scheduleForFcfs(){ + * @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). + * + * 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. + */ + void scheduleForFcfs() { // Variable to keep track of time elepsed so far double timeElapsed = 0; - while(!schedule.empty()){ + while (!schedule.empty()) { tuple cur = schedule.top(); - - // If the next process arrived at time t2, the last process completed its execution at time t1, and t2 > t1. - if(get<1>(cur) > timeElapsed){ + + // If the next process arrived at time t2, the last process + // completed its execution at time t1, and t2 > t1. + if (get<1>(cur) > timeElapsed) { timeElapsed += get<1>(cur) - timeElapsed; } // Add Burst time to time elapsed timeElapsed += get<2>(cur); - + get<3>(cur) = timeElapsed; - + // Turnaround time = Completion time - Arrival time get<4>(cur) = get<3>(cur) - get<1>(cur); - //Waiting time = Turnaround time - Burst time + // Waiting time = Turnaround time - Burst time get<5>(cur) = get<4>(cur) - get<2>(cur); result.push_back(cur); @@ -124,43 +134,39 @@ class FCFS{ } // Utility function for printing the status of each process after execution - void printResult(){ - cout << "Status of all the proceses post completion is as follows:" << endl; + void printResult() { + 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; - - 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::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; } } - - }; -int main(){ - FCFS readyQueue; +int main() { + FCFS readyQueue; - //Sample test case + // Sample test case int n = 3; vector> 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(int i{}; i < n; i++){ - readyQueue.addProcess(get<0>(input[i]), get<1>(input[i]), get<2>(input[i])); + for (int i{}; i < n; i++) { + readyQueue.addProcess(get<0>(input[i]), get<1>(input[i]), + get<2>(input[i])); } readyQueue.scheduleForFcfs();