From 803d7076029c7e946cefa4d39d1b23de0b4b1b99 Mon Sep 17 00:00:00 2001 From: Daemon19 Date: Sun, 9 Oct 2022 09:33:40 +0800 Subject: [PATCH 01/21] feat: create round-robin CPU scheduling algorithm --- .../round_robin_scheduling.cpp | 126 ++++++++++++++++++ 1 file changed, 126 insertions(+) create mode 100644 cpu_scheduling_algorithms/round_robin_scheduling.cpp diff --git a/cpu_scheduling_algorithms/round_robin_scheduling.cpp b/cpu_scheduling_algorithms/round_robin_scheduling.cpp new file mode 100644 index 00000000000..86343ff5aee --- /dev/null +++ b/cpu_scheduling_algorithms/round_robin_scheduling.cpp @@ -0,0 +1,126 @@ +#include +#include +#include +#include +#include +#include +#include + +struct Process { + uint32_t id; + uint32_t arrival_time; + uint32_t burst_time; +}; + +struct ProcessResult : public Process { + uint32_t completion_time; + uint32_t turn_around_time; + uint32_t waiting_time; + + ProcessResult(const Process& process, uint32_t time_elapsed) : Process(process) { + completion_time = time_elapsed; + turn_around_time = completion_time - arrival_time; + waiting_time = turn_around_time - burst_time; + } +}; + +std::vector RRExecute(const std::vector& processes, + uint32_t time_slice); + +std::ostream& operator<<(std::ostream& ostream, + const std::vector& results); + +int main() { + std::vector processes{{0, 3, 3}, {1, 8, 5}, {2, 5, 4}}; + const uint32_t kTimeSlice{2}; + std::vector results = RRExecute(processes, kTimeSlice); + std::cout << results; + std::getchar(); + return 0; +} + +bool CompareAT(const Process& p1, const Process& p2) { + return p1.arrival_time < p2.arrival_time; +} + +void CheckArriveProcess(const std::vector& processes, + std::set& arrived_process, + std::queue>& schedule, + uint32_t time_elapsed); + +std::vector RRExecute(const std::vector& processes, + uint32_t time_slice) { + std::vector sorted_processes = processes; + std::sort(sorted_processes.begin(), sorted_processes.end(), CompareAT); + + std::queue> schedule; + std::set arrived_processes; + std::vector results; + results.reserve(processes.size()); + uint32_t time_elapsed = sorted_processes[0].arrival_time; + + CheckArriveProcess(sorted_processes, arrived_processes, schedule, + time_elapsed); + + while (!schedule.empty()) { + std::pair current = schedule.front(); + schedule.pop(); + + uint32_t elapsed = + (current.second > time_slice) ? time_slice : current.second; + current.second -= elapsed; + time_elapsed += elapsed; + + CheckArriveProcess(sorted_processes, arrived_processes, schedule, + time_elapsed); + + if (current.second > 0) { + schedule.push(current); + continue; + } + results.emplace_back(current.first, time_elapsed); + } + + return results; +} + +std::ostream& operator<<(std::ostream& ostream, + const std::vector& results) { + auto PrintCell = [&](const std::string &str) { + ostream << std::setw(17) << std::left << str; + }; + + PrintCell("Process ID"); + PrintCell("Arrival Time"); + PrintCell("Burst Time"); + PrintCell("Completion Time"); + PrintCell("Turnaround Time"); + PrintCell("Waiting Time"); + ostream << std::endl; + + for (auto& p : results) { + PrintCell(std::to_string(p.id)); + PrintCell(std::to_string(p.arrival_time)); + PrintCell(std::to_string(p.burst_time)); + PrintCell(std::to_string(p.completion_time)); + PrintCell(std::to_string(p.turn_around_time)); + PrintCell(std::to_string(p.waiting_time)); + ostream << "\n"; + } + + return ostream; +} + +void CheckArriveProcess(const std::vector &processes, + std::set& arrived_process, + std::queue>& schedule, + uint32_t time_elapsed) { + for (auto& p : processes) { + if (p.arrival_time > time_elapsed || + arrived_process.find(p.id) != arrived_process.end()) { + continue; + } + schedule.emplace(p, p.burst_time); + arrived_process.insert(p.id); + } +} From 4885fac58a44104e03f66a34c5260a98e761c08e Mon Sep 17 00:00:00 2001 From: Daemon19 Date: Sun, 9 Oct 2022 12:48:26 +0800 Subject: [PATCH 02/21] test: create self-test implementation for round-robin cpu scheduling --- .../round_robin_scheduling.cpp | 50 +++++++++++++++---- 1 file changed, 39 insertions(+), 11 deletions(-) diff --git a/cpu_scheduling_algorithms/round_robin_scheduling.cpp b/cpu_scheduling_algorithms/round_robin_scheduling.cpp index 86343ff5aee..8aee91d1e9f 100644 --- a/cpu_scheduling_algorithms/round_robin_scheduling.cpp +++ b/cpu_scheduling_algorithms/round_robin_scheduling.cpp @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -17,30 +18,54 @@ struct ProcessResult : public Process { uint32_t turn_around_time; uint32_t waiting_time; - ProcessResult(const Process& process, uint32_t time_elapsed) : Process(process) { - completion_time = time_elapsed; + ProcessResult(const Process& process, uint32_t completion_time) + : Process(process), completion_time(completion_time) { turn_around_time = completion_time - arrival_time; waiting_time = turn_around_time - burst_time; } + + bool operator==(const ProcessResult& p) const { + return id == p.id && arrival_time == p.arrival_time && + burst_time == p.burst_time && + completion_time == p.completion_time && + turn_around_time == p.turn_around_time && + waiting_time == p.waiting_time; + } }; std::vector RRExecute(const std::vector& processes, uint32_t time_slice); - std::ostream& operator<<(std::ostream& ostream, const std::vector& results); +void Test(); + +bool CompareAT(const Process& p1, const Process& p2) { + return p1.arrival_time < p2.arrival_time; +} int main() { - std::vector processes{{0, 3, 3}, {1, 8, 5}, {2, 5, 4}}; - const uint32_t kTimeSlice{2}; - std::vector results = RRExecute(processes, kTimeSlice); - std::cout << results; - std::getchar(); + Test(); return 0; } -bool CompareAT(const Process& p1, const Process& p2) { - return p1.arrival_time < p2.arrival_time; +void Test() { + std::vector processes{ + {0, 70, 3}, {1, 9, 2}, {2, 3, 39}, {3, 5, 29}, {4, 30, 90}}; + const uint32_t kTimeSlice{3}; + std::vector results = RRExecute(processes, kTimeSlice); + + std::vector completion_times({80, 14, 100, 82, 166}); + std::vector correct_results; + for (size_t i = 0; i < processes.size(); i++) { + correct_results.emplace_back(processes[i], completion_times[i]); + } + + std::sort(results.begin(), results.end(), CompareAT); + std::sort(correct_results.begin(), correct_results.end(), CompareAT); + + std::cout << results; + assert(results == correct_results); + std::cout << "All test passed"; } void CheckArriveProcess(const std::vector& processes, @@ -90,6 +115,9 @@ std::ostream& operator<<(std::ostream& ostream, ostream << std::setw(17) << std::left << str; }; + std::vector sorted = results; + std::sort(sorted.begin(), sorted.end(), CompareAT); + PrintCell("Process ID"); PrintCell("Arrival Time"); PrintCell("Burst Time"); @@ -98,7 +126,7 @@ std::ostream& operator<<(std::ostream& ostream, PrintCell("Waiting Time"); ostream << std::endl; - for (auto& p : results) { + for (auto& p : sorted) { PrintCell(std::to_string(p.id)); PrintCell(std::to_string(p.arrival_time)); PrintCell(std::to_string(p.burst_time)); From b4d8109c186ebb9ce076746e1ec56946b5e052f1 Mon Sep 17 00:00:00 2001 From: Daemon19 Date: Sun, 9 Oct 2022 14:20:30 +0800 Subject: [PATCH 03/21] doc: add doxygen documentation to round_robin_scheduling.cpp --- .../round_robin_scheduling.cpp | 85 +++++++++++++++++-- 1 file changed, 80 insertions(+), 5 deletions(-) diff --git a/cpu_scheduling_algorithms/round_robin_scheduling.cpp b/cpu_scheduling_algorithms/round_robin_scheduling.cpp index 8aee91d1e9f..7d0b6f559f0 100644 --- a/cpu_scheduling_algorithms/round_robin_scheduling.cpp +++ b/cpu_scheduling_algorithms/round_robin_scheduling.cpp @@ -1,3 +1,14 @@ +/** + * @file + * @brief Implementation of Round Robin CPU scheduling algorithm + * @details + * Round-robin is a preemptive CPU scheduling algorithm where each + * ready task runs turn by turn only in a cyclic queue for a limited + * time slice. This algorithm also offers starvation free execution + * of processes. + * @author [Daemon19](https://github.com/Daemon19) + */ + #include #include #include @@ -7,23 +18,43 @@ #include #include +/** + * @brief Represent a process to be executed. + */ struct Process { uint32_t id; uint32_t arrival_time; uint32_t burst_time; }; +/** + * @brief Represent the result of a process execution. + */ struct ProcessResult : public Process { uint32_t completion_time; uint32_t turn_around_time; uint32_t waiting_time; + /** + * @brief Constructor that calculate member variables based on a + * process and completion time. + * + * \param process A process that have been executed + * \param completion_time The process execution finish time + */ ProcessResult(const Process& process, uint32_t completion_time) : Process(process), completion_time(completion_time) { turn_around_time = completion_time - arrival_time; waiting_time = turn_around_time - burst_time; } + /** + * @brief Compare each member variable. + * + * \param p ProcessResult to be compared with + * \return true if the processes IS equal + * \return false if the processes IS NOT equal + */ bool operator==(const ProcessResult& p) const { return id == p.id && arrival_time == p.arrival_time && burst_time == p.burst_time && @@ -33,16 +64,65 @@ struct ProcessResult : public Process { } }; +/** + * @brief Execute processes based on Round-robin algorithm. + * + * \param processes Processes to be executed + * \param time_slice Time slice for processes execution + * \return Results of each process execution + */ std::vector RRExecute(const std::vector& processes, uint32_t time_slice); + +/** + * @brief Print a table containing process results data. + * + * \return ostream inputted ostream + */ std::ostream& operator<<(std::ostream& ostream, const std::vector& results); + +/** + * @brief Self-test implementations. + * + * \returns void + */ void Test(); + +/** + * @brief Comparator function for sorting processes. + * + * \param p1 Process to be compared + * \param p2 Process to be compared + * \return + */ bool CompareAT(const Process& p1, const Process& p2) { return p1.arrival_time < p2.arrival_time; } +/** + * @brief Check processes that arrive after the given time_elapsed. + * + * If a process arrive after the give time_elapsed, then the process + * will be pushed into the schedule queue and inserted into the + * arrived_process set. + * + * \param processes Processes that will be checked for arrival + * \param arrived_process A set containing processes that has arrived + * \param schedule Queue containing pair of process and its remaining burst time + * \param time_elapsed Time that has elapsed after processes execution + */ +void CheckArriveProcess(const std::vector& processes, + std::set& arrived_process, + std::queue>& schedule, + uint32_t time_elapsed); + +/** + * @brief Entry point of the program. + * + * \return 0 on exit + */ int main() { Test(); return 0; @@ -68,11 +148,6 @@ void Test() { std::cout << "All test passed"; } -void CheckArriveProcess(const std::vector& processes, - std::set& arrived_process, - std::queue>& schedule, - uint32_t time_elapsed); - std::vector RRExecute(const std::vector& processes, uint32_t time_slice) { std::vector sorted_processes = processes; From e48f29a03472813c12ecebe9051dcd7b827a5a35 Mon Sep 17 00:00:00 2001 From: Daemon19 Date: Sun, 9 Oct 2022 14:27:36 +0800 Subject: [PATCH 04/21] chore: processes don't need to be sorted --- cpu_scheduling_algorithms/round_robin_scheduling.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/cpu_scheduling_algorithms/round_robin_scheduling.cpp b/cpu_scheduling_algorithms/round_robin_scheduling.cpp index 7d0b6f559f0..d3730612eed 100644 --- a/cpu_scheduling_algorithms/round_robin_scheduling.cpp +++ b/cpu_scheduling_algorithms/round_robin_scheduling.cpp @@ -150,16 +150,16 @@ void Test() { std::vector RRExecute(const std::vector& processes, uint32_t time_slice) { - std::vector sorted_processes = processes; - std::sort(sorted_processes.begin(), sorted_processes.end(), CompareAT); - std::queue> schedule; std::set arrived_processes; + std::vector results; results.reserve(processes.size()); - uint32_t time_elapsed = sorted_processes[0].arrival_time; - CheckArriveProcess(sorted_processes, arrived_processes, schedule, + uint32_t time_elapsed = + std::min_element(processes.begin(), processes.end(), CompareAT)->arrival_time; + + CheckArriveProcess(processes, arrived_processes, schedule, time_elapsed); while (!schedule.empty()) { @@ -171,7 +171,7 @@ std::vector RRExecute(const std::vector& processes, current.second -= elapsed; time_elapsed += elapsed; - CheckArriveProcess(sorted_processes, arrived_processes, schedule, + CheckArriveProcess(processes, arrived_processes, schedule, time_elapsed); if (current.second > 0) { From 26210baef341d204532180f3e684d8212e0adeb0 Mon Sep 17 00:00:00 2001 From: Daemon19 Date: Sun, 9 Oct 2022 14:42:02 +0800 Subject: [PATCH 05/21] doc: add documentation so the code more understandable --- .../round_robin_scheduling.cpp | 25 ++++++++++++++----- 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/cpu_scheduling_algorithms/round_robin_scheduling.cpp b/cpu_scheduling_algorithms/round_robin_scheduling.cpp index d3730612eed..dd2cfd2b059 100644 --- a/cpu_scheduling_algorithms/round_robin_scheduling.cpp +++ b/cpu_scheduling_algorithms/round_robin_scheduling.cpp @@ -64,6 +64,11 @@ struct ProcessResult : public Process { } }; +/** + * Represent remaining burst time of a process. + */ +using BTLeft = uint32_t; + /** * @brief Execute processes based on Round-robin algorithm. * @@ -115,7 +120,7 @@ bool CompareAT(const Process& p1, const Process& p2) { */ void CheckArriveProcess(const std::vector& processes, std::set& arrived_process, - std::queue>& schedule, + std::queue>& schedule, uint32_t time_elapsed); /** @@ -134,12 +139,14 @@ void Test() { const uint32_t kTimeSlice{3}; std::vector results = RRExecute(processes, kTimeSlice); - std::vector completion_times({80, 14, 100, 82, 166}); + std::vector correct_completion_times({80, 14, 100, 82, 166}); std::vector correct_results; + // Generate correct process results based on correct completion times for (size_t i = 0; i < processes.size(); i++) { - correct_results.emplace_back(processes[i], completion_times[i]); + correct_results.emplace_back(processes[i], correct_completion_times[i]); } + // Sort the results and correct results so they're exactly the same std::sort(results.begin(), results.end(), CompareAT); std::sort(correct_results.begin(), correct_results.end(), CompareAT); @@ -150,12 +157,13 @@ void Test() { std::vector RRExecute(const std::vector& processes, uint32_t time_slice) { - std::queue> schedule; + std::queue> schedule; std::set arrived_processes; std::vector results; results.reserve(processes.size()); + // The time of the first process execution will be the lowest process AT uint32_t time_elapsed = std::min_element(processes.begin(), processes.end(), CompareAT)->arrival_time; @@ -163,9 +171,12 @@ std::vector RRExecute(const std::vector& processes, time_elapsed); while (!schedule.empty()) { - std::pair current = schedule.front(); + std::pair current = schedule.front(); schedule.pop(); + // If process burst time < time slice, then the process will be + // executed for the burst time amount of time, not the time + // quantum/slice uint32_t elapsed = (current.second > time_slice) ? time_slice : current.second; current.second -= elapsed; @@ -178,6 +189,8 @@ std::vector RRExecute(const std::vector& processes, schedule.push(current); continue; } + // Generate process result based on the completion time (time + // that has elapsed) results.emplace_back(current.first, time_elapsed); } @@ -216,7 +229,7 @@ std::ostream& operator<<(std::ostream& ostream, void CheckArriveProcess(const std::vector &processes, std::set& arrived_process, - std::queue>& schedule, + std::queue>& schedule, uint32_t time_elapsed) { for (auto& p : processes) { if (p.arrival_time > time_elapsed || From be91e2f910e3cfa1348f8964a3444b67468bcf0c Mon Sep 17 00:00:00 2001 From: Daemon19 Date: Mon, 10 Oct 2022 20:06:19 +0800 Subject: [PATCH 06/21] doc: add include documentation --- .../round_robin_scheduling.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/cpu_scheduling_algorithms/round_robin_scheduling.cpp b/cpu_scheduling_algorithms/round_robin_scheduling.cpp index dd2cfd2b059..6898878cd25 100644 --- a/cpu_scheduling_algorithms/round_robin_scheduling.cpp +++ b/cpu_scheduling_algorithms/round_robin_scheduling.cpp @@ -9,14 +9,14 @@ * @author [Daemon19](https://github.com/Daemon19) */ -#include -#include -#include -#include -#include -#include -#include -#include +#include // For formatting process results output +#include // For testing the round-robin algorithm +#include // For converting int type to string +#include // For outputting process execution results +#include // Container for process execution turn +#include // Container for processes that have arrived +#include // So I can use std::pair +#include // Container for processes that will be executed /** * @brief Represent a process to be executed. From 6c0d6d8a2ea4c1650e3f07d1826d76331709cf6e Mon Sep 17 00:00:00 2001 From: Daemon <90456722+Daemon19@users.noreply.github.com> Date: Tue, 18 Oct 2022 15:14:40 +0800 Subject: [PATCH 07/21] Update cpu_scheduling_algorithms/round_robin_scheduling.cpp Co-authored-by: David Leal --- .../round_robin_scheduling.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/cpu_scheduling_algorithms/round_robin_scheduling.cpp b/cpu_scheduling_algorithms/round_robin_scheduling.cpp index 6898878cd25..4d4f960b8c8 100644 --- a/cpu_scheduling_algorithms/round_robin_scheduling.cpp +++ b/cpu_scheduling_algorithms/round_robin_scheduling.cpp @@ -9,14 +9,14 @@ * @author [Daemon19](https://github.com/Daemon19) */ -#include // For formatting process results output -#include // For testing the round-robin algorithm -#include // For converting int type to string -#include // For outputting process execution results -#include // Container for process execution turn -#include // Container for processes that have arrived -#include // So I can use std::pair -#include // Container for processes that will be executed +#include /// For formatting process results output +#include /// For testing the round-robin algorithm +#include /// For converting int type to string +#include /// For outputting process execution results +#include /// Container for process execution turn +#include /// Container for processes that have arrived +#include /// So I can use std::pair +#include /// Container for processes that will be executed /** * @brief Represent a process to be executed. From 3586fb02563041d73d3cdbc6a9d996aeeb73b395 Mon Sep 17 00:00:00 2001 From: Daemon19 Date: Tue, 18 Oct 2022 15:28:08 +0800 Subject: [PATCH 08/21] doc: add doc for structs member variable --- .../round_robin_scheduling.cpp | 32 +++++++++---------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/cpu_scheduling_algorithms/round_robin_scheduling.cpp b/cpu_scheduling_algorithms/round_robin_scheduling.cpp index 4d4f960b8c8..73ac9655fe0 100644 --- a/cpu_scheduling_algorithms/round_robin_scheduling.cpp +++ b/cpu_scheduling_algorithms/round_robin_scheduling.cpp @@ -22,23 +22,23 @@ * @brief Represent a process to be executed. */ struct Process { - uint32_t id; - uint32_t arrival_time; - uint32_t burst_time; + uint32_t id; ///< Used to distinguish processes + uint32_t arrival_time; ///< The time at which the process arrives + uint32_t burst_time; ///< Time required to complete process execution }; /** * @brief Represent the result of a process execution. */ struct ProcessResult : public Process { - uint32_t completion_time; - uint32_t turn_around_time; - uint32_t waiting_time; + uint32_t completion_time; ///< The time at which the process execution is finished + uint32_t turn_around_time; ///< The turn around time required for the process to complete + uint32_t waiting_time; ///< Process waiting time before execution /** * @brief Constructor that calculate member variables based on a * process and completion time. - * + * * \param process A process that have been executed * \param completion_time The process execution finish time */ @@ -47,10 +47,10 @@ struct ProcessResult : public Process { turn_around_time = completion_time - arrival_time; waiting_time = turn_around_time - burst_time; } - + /** * @brief Compare each member variable. - * + * * \param p ProcessResult to be compared with * \return true if the processes IS equal * \return false if the processes IS NOT equal @@ -71,7 +71,7 @@ using BTLeft = uint32_t; /** * @brief Execute processes based on Round-robin algorithm. - * + * * \param processes Processes to be executed * \param time_slice Time slice for processes execution * \return Results of each process execution @@ -81,7 +81,7 @@ std::vector RRExecute(const std::vector& processes, /** * @brief Print a table containing process results data. - * + * * \return ostream inputted ostream */ std::ostream& operator<<(std::ostream& ostream, @@ -89,7 +89,7 @@ std::ostream& operator<<(std::ostream& ostream, /** * @brief Self-test implementations. - * + * * \returns void */ void Test(); @@ -97,10 +97,10 @@ void Test(); /** * @brief Comparator function for sorting processes. - * + * * \param p1 Process to be compared * \param p2 Process to be compared - * \return + * \return */ bool CompareAT(const Process& p1, const Process& p2) { return p1.arrival_time < p2.arrival_time; @@ -112,7 +112,7 @@ bool CompareAT(const Process& p1, const Process& p2) { * If a process arrive after the give time_elapsed, then the process * will be pushed into the schedule queue and inserted into the * arrived_process set. - * + * * \param processes Processes that will be checked for arrival * \param arrived_process A set containing processes that has arrived * \param schedule Queue containing pair of process and its remaining burst time @@ -125,7 +125,7 @@ void CheckArriveProcess(const std::vector& processes, /** * @brief Entry point of the program. - * + * * \return 0 on exit */ int main() { From 8f81761f59ea89809c326b8e4da2e6f5b70f8731 Mon Sep 17 00:00:00 2001 From: Daemon <90456722+Daemon19@users.noreply.github.com> Date: Tue, 18 Oct 2022 15:31:57 +0800 Subject: [PATCH 09/21] Update cpu_scheduling_algorithms/round_robin_scheduling.cpp Update Test function to be a static function. Co-authored-by: David Leal --- cpu_scheduling_algorithms/round_robin_scheduling.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cpu_scheduling_algorithms/round_robin_scheduling.cpp b/cpu_scheduling_algorithms/round_robin_scheduling.cpp index 73ac9655fe0..2202e1fe37b 100644 --- a/cpu_scheduling_algorithms/round_robin_scheduling.cpp +++ b/cpu_scheduling_algorithms/round_robin_scheduling.cpp @@ -92,7 +92,7 @@ std::ostream& operator<<(std::ostream& ostream, * * \returns void */ -void Test(); +static void Test(); /** From 6a63dcdd50ae71d57948093104fd7fbce30272b5 Mon Sep 17 00:00:00 2001 From: Daemon19 Date: Tue, 18 Oct 2022 15:53:51 +0800 Subject: [PATCH 10/21] fix: missing algorithm header file --- .../round_robin_scheduling.cpp | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/cpu_scheduling_algorithms/round_robin_scheduling.cpp b/cpu_scheduling_algorithms/round_robin_scheduling.cpp index 2202e1fe37b..ae66260a6d2 100644 --- a/cpu_scheduling_algorithms/round_robin_scheduling.cpp +++ b/cpu_scheduling_algorithms/round_robin_scheduling.cpp @@ -9,14 +9,15 @@ * @author [Daemon19](https://github.com/Daemon19) */ -#include /// For formatting process results output -#include /// For testing the round-robin algorithm -#include /// For converting int type to string -#include /// For outputting process execution results -#include /// Container for process execution turn -#include /// Container for processes that have arrived -#include /// So I can use std::pair -#include /// Container for processes that will be executed +#include /// For formatting process results output +#include /// For testing the round-robin algorithm +#include /// For converting int type to string +#include /// For outputting process execution results +#include /// Container for process execution turn +#include /// Container for processes that have arrived +#include /// So I can use std::pair +#include /// Container for processes that will be executed +#include /// So I can use std::sort /** * @brief Represent a process to be executed. From cb43edc5d52124cb28de987d6bc2feafd0379754 Mon Sep 17 00:00:00 2001 From: Daemon <90456722+Daemon19@users.noreply.github.com> Date: Tue, 18 Oct 2022 15:54:54 +0800 Subject: [PATCH 11/21] Update cpu_scheduling_algorithms/round_robin_scheduling.cpp Co-authored-by: David Leal --- cpu_scheduling_algorithms/round_robin_scheduling.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/cpu_scheduling_algorithms/round_robin_scheduling.cpp b/cpu_scheduling_algorithms/round_robin_scheduling.cpp index ae66260a6d2..1a5a91d20ab 100644 --- a/cpu_scheduling_algorithms/round_robin_scheduling.cpp +++ b/cpu_scheduling_algorithms/round_robin_scheduling.cpp @@ -134,7 +134,11 @@ int main() { return 0; } -void Test() { +/** + * @brief Self-test implementations + * @returns void + */ +static void Test() { std::vector processes{ {0, 70, 3}, {1, 9, 2}, {2, 3, 39}, {3, 5, 29}, {4, 30, 90}}; const uint32_t kTimeSlice{3}; From b6ff48cf96146ddd5b1f92dfbb8be93d6fd6c25d Mon Sep 17 00:00:00 2001 From: Daemon Date: Tue, 18 Oct 2022 08:27:14 +0000 Subject: [PATCH 12/21] fix: formatting errors --- .../round_robin_scheduling.cpp | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/cpu_scheduling_algorithms/round_robin_scheduling.cpp b/cpu_scheduling_algorithms/round_robin_scheduling.cpp index 1a5a91d20ab..dcbb626b5da 100644 --- a/cpu_scheduling_algorithms/round_robin_scheduling.cpp +++ b/cpu_scheduling_algorithms/round_robin_scheduling.cpp @@ -120,8 +120,8 @@ bool CompareAT(const Process& p1, const Process& p2) { * \param time_elapsed Time that has elapsed after processes execution */ void CheckArriveProcess(const std::vector& processes, - std::set& arrived_process, - std::queue>& schedule, + std::set* arrived_process, + std::queue>* schedule, uint32_t time_elapsed); /** @@ -172,7 +172,7 @@ std::vector RRExecute(const std::vector& processes, uint32_t time_elapsed = std::min_element(processes.begin(), processes.end(), CompareAT)->arrival_time; - CheckArriveProcess(processes, arrived_processes, schedule, + CheckArriveProcess(processes, &arrived_processes, &schedule, time_elapsed); while (!schedule.empty()) { @@ -187,7 +187,7 @@ std::vector RRExecute(const std::vector& processes, current.second -= elapsed; time_elapsed += elapsed; - CheckArriveProcess(processes, arrived_processes, schedule, + CheckArriveProcess(processes, &arrived_processes, &schedule, time_elapsed); if (current.second > 0) { @@ -233,15 +233,15 @@ std::ostream& operator<<(std::ostream& ostream, } void CheckArriveProcess(const std::vector &processes, - std::set& arrived_process, - std::queue>& schedule, + std::set* arrived_process, + std::queue>* schedule, uint32_t time_elapsed) { for (auto& p : processes) { if (p.arrival_time > time_elapsed || - arrived_process.find(p.id) != arrived_process.end()) { + arrived_process->find(p.id) != arrived_process->end()) { continue; } - schedule.emplace(p, p.burst_time); - arrived_process.insert(p.id); + schedule->emplace(p, p.burst_time); + arrived_process->insert(p.id); } } From 6d00a6b0396ae4fb0836a4cf2a4fae4fb3c3ec3f Mon Sep 17 00:00:00 2001 From: David Date: Tue, 18 Oct 2022 08:27:53 +0000 Subject: [PATCH 13/21] updating DIRECTORY.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index fe099e8914e..1c80f3f96aa 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -35,6 +35,7 @@ ## Cpu Scheduling Algorithms * [Fcfs Scheduling](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/cpu_scheduling_algorithms/fcfs_scheduling.cpp) + * [Round Robin Scheduling](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/cpu_scheduling_algorithms/round_robin_scheduling.cpp) ## Data Structures * [Avltree](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/data_structures/avltree.cpp) From ee2b2e911d807daf81103f20558fcdc738c1afbb Mon Sep 17 00:00:00 2001 From: David Date: Tue, 18 Oct 2022 08:28:05 +0000 Subject: [PATCH 14/21] clang-format and clang-tidy fixes for b6ff48cf --- .../round_robin_scheduling.cpp | 45 ++++++++++--------- 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/cpu_scheduling_algorithms/round_robin_scheduling.cpp b/cpu_scheduling_algorithms/round_robin_scheduling.cpp index dcbb626b5da..8fb6fcd2887 100644 --- a/cpu_scheduling_algorithms/round_robin_scheduling.cpp +++ b/cpu_scheduling_algorithms/round_robin_scheduling.cpp @@ -9,15 +9,15 @@ * @author [Daemon19](https://github.com/Daemon19) */ -#include /// For formatting process results output -#include /// For testing the round-robin algorithm -#include /// For converting int type to string -#include /// For outputting process execution results -#include /// Container for process execution turn -#include /// Container for processes that have arrived -#include /// So I can use std::pair -#include /// Container for processes that will be executed -#include /// So I can use std::sort +#include /// So I can use std::sort +#include /// For testing the round-robin algorithm +#include /// For formatting process results output +#include /// For outputting process execution results +#include /// Container for process execution turn +#include /// Container for processes that have arrived +#include /// For converting int type to string +#include /// So I can use std::pair +#include /// Container for processes that will be executed /** * @brief Represent a process to be executed. @@ -32,8 +32,10 @@ struct Process { * @brief Represent the result of a process execution. */ struct ProcessResult : public Process { - uint32_t completion_time; ///< The time at which the process execution is finished - uint32_t turn_around_time; ///< The turn around time required for the process to complete + uint32_t completion_time; ///< The time at which the process execution is + ///< finished + uint32_t turn_around_time; ///< The turn around time required for the + ///< process to complete uint32_t waiting_time; ///< Process waiting time before execution /** @@ -95,7 +97,6 @@ std::ostream& operator<<(std::ostream& ostream, */ static void Test(); - /** * @brief Comparator function for sorting processes. * @@ -170,10 +171,10 @@ std::vector RRExecute(const std::vector& processes, // The time of the first process execution will be the lowest process AT uint32_t time_elapsed = - std::min_element(processes.begin(), processes.end(), CompareAT)->arrival_time; + std::min_element(processes.begin(), processes.end(), CompareAT) + ->arrival_time; - CheckArriveProcess(processes, &arrived_processes, &schedule, - time_elapsed); + CheckArriveProcess(processes, &arrived_processes, &schedule, time_elapsed); while (!schedule.empty()) { std::pair current = schedule.front(); @@ -184,14 +185,14 @@ std::vector RRExecute(const std::vector& processes, // quantum/slice uint32_t elapsed = (current.second > time_slice) ? time_slice : current.second; - current.second -= elapsed; - time_elapsed += elapsed; + current.second -= elapsed; + time_elapsed += elapsed; - CheckArriveProcess(processes, &arrived_processes, &schedule, - time_elapsed); + CheckArriveProcess(processes, &arrived_processes, &schedule, + time_elapsed); if (current.second > 0) { - schedule.push(current); + schedule.push(current); continue; } // Generate process result based on the completion time (time @@ -204,7 +205,7 @@ std::vector RRExecute(const std::vector& processes, std::ostream& operator<<(std::ostream& ostream, const std::vector& results) { - auto PrintCell = [&](const std::string &str) { + auto PrintCell = [&](const std::string& str) { ostream << std::setw(17) << std::left << str; }; @@ -232,7 +233,7 @@ std::ostream& operator<<(std::ostream& ostream, return ostream; } -void CheckArriveProcess(const std::vector &processes, +void CheckArriveProcess(const std::vector& processes, std::set* arrived_process, std::queue>* schedule, uint32_t time_elapsed) { From ba74e24725aabf44a707e10237bc696af34de686 Mon Sep 17 00:00:00 2001 From: David Date: Wed, 19 Oct 2022 01:10:40 +0000 Subject: [PATCH 15/21] clang-format and clang-tidy fixes for 825bcd54 --- dynamic_programming/0_1_knapsack.cpp | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/dynamic_programming/0_1_knapsack.cpp b/dynamic_programming/0_1_knapsack.cpp index 8d08402e8e3..3634019ed32 100644 --- a/dynamic_programming/0_1_knapsack.cpp +++ b/dynamic_programming/0_1_knapsack.cpp @@ -39,8 +39,8 @@ namespace dynamic_programming { namespace knapsack { /** * @brief Picking up all those items whose combined weight is below - * the given capacity and calculating the value of those picked items. Trying all - * possible combinations will yield the maximum knapsack value. + * the given capacity and calculating the value of those picked items. Trying + * all possible combinations will yield the maximum knapsack value. * @tparam n size of the weight and value array * @param capacity capacity of the carrying bag * @param weight array representing the weight of items @@ -62,9 +62,9 @@ int maxKnapsackValue(const int capacity, const std::array &weight, // will be zero maxValue[i][j] = 0; } else if (weight[i - 1] <= j) { - // if the ith item's weight(in the actual array it will be at i-1) - // is less than or equal to the allowed weight i.e. j then we - // can pick that item for our knapsack. maxValue will be the + // if the ith item's weight(in the actual array it will be at + // i-1) is less than or equal to the allowed weight i.e. j then + // we can pick that item for our knapsack. maxValue will be the // obtained either by picking the current item or by not picking // current item @@ -76,8 +76,9 @@ int maxKnapsackValue(const int capacity, const std::array &weight, maxValue[i][j] = std::max(profit1, profit2); } else { - // as the weight of the current item is greater than the allowed weight, so - // maxProfit will be profit obtained by excluding the current item. + // as the weight of the current item is greater than the allowed + // weight, so maxProfit will be profit obtained by excluding the + // current item. maxValue[i][j] = maxValue[i - 1][j]; } } From d08022c335a4294cd2dbd64ad282afaf3a306ba4 Mon Sep 17 00:00:00 2001 From: Daemon <90456722+Daemon19@users.noreply.github.com> Date: Fri, 21 Oct 2022 06:26:25 +0800 Subject: [PATCH 16/21] doc: update include doc in cpu_scheduling_algorithms/round_robin_scheduling.cpp Co-authored-by: David Leal --- cpu_scheduling_algorithms/round_robin_scheduling.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cpu_scheduling_algorithms/round_robin_scheduling.cpp b/cpu_scheduling_algorithms/round_robin_scheduling.cpp index 8fb6fcd2887..5f568e1b7c7 100644 --- a/cpu_scheduling_algorithms/round_robin_scheduling.cpp +++ b/cpu_scheduling_algorithms/round_robin_scheduling.cpp @@ -16,7 +16,7 @@ #include /// Container for process execution turn #include /// Container for processes that have arrived #include /// For converting int type to string -#include /// So I can use std::pair +#include /// For std::pair #include /// Container for processes that will be executed /** From c5a915c02bd639ea5a031bac9a2792d3bf97f1b8 Mon Sep 17 00:00:00 2001 From: Daemon <90456722+Daemon19@users.noreply.github.com> Date: Fri, 21 Oct 2022 16:17:17 +0800 Subject: [PATCH 17/21] doc: update include doc in cpu_scheduling_algorithms/round_robin_scheduling.cpp Co-authored-by: David Leal --- cpu_scheduling_algorithms/round_robin_scheduling.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cpu_scheduling_algorithms/round_robin_scheduling.cpp b/cpu_scheduling_algorithms/round_robin_scheduling.cpp index 5f568e1b7c7..d9e017c8f44 100644 --- a/cpu_scheduling_algorithms/round_robin_scheduling.cpp +++ b/cpu_scheduling_algorithms/round_robin_scheduling.cpp @@ -9,7 +9,7 @@ * @author [Daemon19](https://github.com/Daemon19) */ -#include /// So I can use std::sort +#include /// For std::sort #include /// For testing the round-robin algorithm #include /// For formatting process results output #include /// For outputting process execution results From e1071fea35aee8a90ad484b11c8994a05b31412c Mon Sep 17 00:00:00 2001 From: Daemon19 Date: Fri, 21 Oct 2022 16:42:50 +0800 Subject: [PATCH 18/21] doc: added Wikipedia link --- cpu_scheduling_algorithms/round_robin_scheduling.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cpu_scheduling_algorithms/round_robin_scheduling.cpp b/cpu_scheduling_algorithms/round_robin_scheduling.cpp index d9e017c8f44..878fbe74e82 100644 --- a/cpu_scheduling_algorithms/round_robin_scheduling.cpp +++ b/cpu_scheduling_algorithms/round_robin_scheduling.cpp @@ -1,6 +1,7 @@ /** * @file - * @brief Implementation of Round Robin CPU scheduling algorithm + * @brief Implementation of [Round Robin CPU + * scheduling](https://en.wikipedia.org/wiki/Round-robin_scheduling) algorithm * @details * Round-robin is a preemptive CPU scheduling algorithm where each * ready task runs turn by turn only in a cyclic queue for a limited From 75745a00f11a1eec6ca06e7e166a81fa11aea9c6 Mon Sep 17 00:00:00 2001 From: Daemon19 Date: Fri, 21 Oct 2022 17:15:57 +0800 Subject: [PATCH 19/21] doc: remove Test function prototype --- .../round_robin_scheduling.cpp | 27 +++++++------------ 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/cpu_scheduling_algorithms/round_robin_scheduling.cpp b/cpu_scheduling_algorithms/round_robin_scheduling.cpp index 878fbe74e82..3fad6555311 100644 --- a/cpu_scheduling_algorithms/round_robin_scheduling.cpp +++ b/cpu_scheduling_algorithms/round_robin_scheduling.cpp @@ -91,13 +91,6 @@ std::vector RRExecute(const std::vector& processes, std::ostream& operator<<(std::ostream& ostream, const std::vector& results); -/** - * @brief Self-test implementations. - * - * \returns void - */ -static void Test(); - /** * @brief Comparator function for sorting processes. * @@ -126,16 +119,6 @@ void CheckArriveProcess(const std::vector& processes, std::queue>* schedule, uint32_t time_elapsed); -/** - * @brief Entry point of the program. - * - * \return 0 on exit - */ -int main() { - Test(); - return 0; -} - /** * @brief Self-test implementations * @returns void @@ -162,6 +145,16 @@ static void Test() { std::cout << "All test passed"; } +/** + * @brief Entry point of the program. + * + * \return 0 on exit + */ +int main() { + Test(); + return 0; +} + std::vector RRExecute(const std::vector& processes, uint32_t time_slice) { std::queue> schedule; From dfef85433310b2b30ae8d44c894fe5042cdbe6de Mon Sep 17 00:00:00 2001 From: Daemon19 Date: Fri, 21 Oct 2022 17:20:02 +0800 Subject: [PATCH 20/21] fix: missing newline character --- cpu_scheduling_algorithms/round_robin_scheduling.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cpu_scheduling_algorithms/round_robin_scheduling.cpp b/cpu_scheduling_algorithms/round_robin_scheduling.cpp index 3fad6555311..82e53a09727 100644 --- a/cpu_scheduling_algorithms/round_robin_scheduling.cpp +++ b/cpu_scheduling_algorithms/round_robin_scheduling.cpp @@ -142,7 +142,7 @@ static void Test() { std::cout << results; assert(results == correct_results); - std::cout << "All test passed"; + std::cout << "All test passed.\n"; } /** From 72e4f5920ff7067d189611a7d34c4367a99feae9 Mon Sep 17 00:00:00 2001 From: David Date: Sun, 30 Oct 2022 02:12:19 +0000 Subject: [PATCH 21/21] clang-format and clang-tidy fixes for 5f752df9 --- .../ground_to_ground_projectile_motion.cpp | 32 +++++++++++++------ sorting/merge_sort.cpp | 4 +-- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/physics/ground_to_ground_projectile_motion.cpp b/physics/ground_to_ground_projectile_motion.cpp index f2551935e74..e8c3c298f7c 100644 --- a/physics/ground_to_ground_projectile_motion.cpp +++ b/physics/ground_to_ground_projectile_motion.cpp @@ -43,7 +43,9 @@ double degrees_to_radians(double radian, double PI = 3.14) { */ template T time_of_flight(T initial_velocity, T angle, double gravity = 9.81) { - double Viy = initial_velocity * (std::sin(degrees_to_radians(angle))); // calculate y component of the initial velocity + double Viy = initial_velocity * + (std::sin(degrees_to_radians( + angle))); // calculate y component of the initial velocity return 2.0 * Viy / gravity; } @@ -55,7 +57,9 @@ T time_of_flight(T initial_velocity, T angle, double gravity = 9.81) { */ template T horizontal_range(T initial_velocity, T angle, T time) { - double Vix = initial_velocity * (std::cos(degrees_to_radians(angle))); // calculate x component of the initial velocity + double Vix = initial_velocity * + (std::cos(degrees_to_radians( + angle))); // calculate x component of the initial velocity return Vix * time; } @@ -68,7 +72,9 @@ T horizontal_range(T initial_velocity, T angle, T time) { */ template T max_height(T initial_velocity, T angle, double gravity = 9.81) { - double Viy = initial_velocity * (std::sin(degrees_to_radians(angle))); // calculate y component of the initial velocity + double Viy = initial_velocity * + (std::sin(degrees_to_radians( + angle))); // calculate y component of the initial velocity return (std::pow(Viy, 2) / (2.0 * gravity)); } } // namespace ground_to_ground_projectile_motion @@ -86,7 +92,9 @@ static void test() { // 1st test double expected_time_of_flight = 0.655; // expected time output double flight_time_output = - std::round(physics::ground_to_ground_projectile_motion::time_of_flight(initial_velocity, angle) * 1000.0) / + std::round(physics::ground_to_ground_projectile_motion::time_of_flight( + initial_velocity, angle) * + 1000.0) / 1000.0; // round output to 3 decimal places std::cout << "Projectile Flight Time (double)" << std::endl; @@ -98,11 +106,13 @@ static void test() { std::cout << "TEST PASSED" << std::endl << std::endl; // 2nd test - double expected_horizontal_range = 2.51; // expected range output + double expected_horizontal_range = 2.51; // expected range output double horizontal_range_output = - std::round(physics::ground_to_ground_projectile_motion::horizontal_range(initial_velocity, angle, - flight_time_output) * - 100.0) / + std::round( + physics::ground_to_ground_projectile_motion::horizontal_range( + initial_velocity, angle, + flight_time_output) * + 100.0) / 100.0; // round output to 2 decimal places std::cout << "Projectile Horizontal Range (double)" << std::endl; @@ -115,9 +125,11 @@ static void test() { std::cout << "TEST PASSED" << std::endl << std::endl; // 3rd test - double expected_max_height = 0.526; // expected height output + double expected_max_height = 0.526; // expected height output double max_height_output = - std::round(physics::ground_to_ground_projectile_motion::max_height(initial_velocity, angle) * 1000.0) / + std::round(physics::ground_to_ground_projectile_motion::max_height( + initial_velocity, angle) * + 1000.0) / 1000.0; // round output to 3 decimal places std::cout << "Projectile Max Height (double)" << std::endl; diff --git a/sorting/merge_sort.cpp b/sorting/merge_sort.cpp index 74087491bef..8dbb72a8278 100644 --- a/sorting/merge_sort.cpp +++ b/sorting/merge_sort.cpp @@ -31,7 +31,7 @@ * @param r - end index or right index of second half array */ void merge(int *arr, int l, int m, int r) { - int i, j, k; + int i = 0, j = 0, k = 0; int n1 = m - l + 1; int n2 = r - m; @@ -88,7 +88,7 @@ void show(int *arr, int size) { /** Main function */ int main() { - int size; + int size = 0; std::cout << "Enter the number of elements : "; std::cin >> size; int *arr = new int[size];