1
+ /* *
2
+ * @file
3
+ * @brief Implementation of Round Robin CPU scheduling algorithm
4
+ * @details
5
+ * Round-robin is a preemptive CPU scheduling algorithm where each
6
+ * ready task runs turn by turn only in a cyclic queue for a limited
7
+ * time slice. This algorithm also offers starvation free execution
8
+ * of processes.
9
+ * @author [Daemon19](https://github.com/Daemon19)
10
+ */
11
+
1
12
#include < iomanip>
2
13
#include < cassert>
3
14
#include < string>
7
18
#include < utility>
8
19
#include < vector>
9
20
21
+ /* *
22
+ * @brief Represent a process to be executed.
23
+ */
10
24
struct Process {
11
25
uint32_t id;
12
26
uint32_t arrival_time;
13
27
uint32_t burst_time;
14
28
};
15
29
30
+ /* *
31
+ * @brief Represent the result of a process execution.
32
+ */
16
33
struct ProcessResult : public Process {
17
34
uint32_t completion_time;
18
35
uint32_t turn_around_time;
19
36
uint32_t waiting_time;
20
37
38
+ /* *
39
+ * @brief Constructor that calculate member variables based on a
40
+ * process and completion time.
41
+ *
42
+ * \param process A process that have been executed
43
+ * \param completion_time The process execution finish time
44
+ */
21
45
ProcessResult (const Process& process, uint32_t completion_time)
22
46
: Process(process), completion_time(completion_time) {
23
47
turn_around_time = completion_time - arrival_time;
24
48
waiting_time = turn_around_time - burst_time;
25
49
}
26
50
51
+ /* *
52
+ * @brief Compare each member variable.
53
+ *
54
+ * \param p ProcessResult to be compared with
55
+ * \return true if the processes IS equal
56
+ * \return false if the processes IS NOT equal
57
+ */
27
58
bool operator ==(const ProcessResult& p) const {
28
59
return id == p.id && arrival_time == p.arrival_time &&
29
60
burst_time == p.burst_time &&
@@ -33,16 +64,65 @@ struct ProcessResult : public Process {
33
64
}
34
65
};
35
66
67
+ /* *
68
+ * @brief Execute processes based on Round-robin algorithm.
69
+ *
70
+ * \param processes Processes to be executed
71
+ * \param time_slice Time slice for processes execution
72
+ * \return Results of each process execution
73
+ */
36
74
std::vector<ProcessResult> RRExecute (const std::vector<Process>& processes,
37
75
uint32_t time_slice);
76
+
77
+ /* *
78
+ * @brief Print a table containing process results data.
79
+ *
80
+ * \return ostream inputted ostream
81
+ */
38
82
std::ostream& operator <<(std::ostream& ostream,
39
83
const std::vector<ProcessResult>& results);
84
+
85
+ /* *
86
+ * @brief Self-test implementations.
87
+ *
88
+ * \returns void
89
+ */
40
90
void Test ();
41
91
92
+
93
+ /* *
94
+ * @brief Comparator function for sorting processes.
95
+ *
96
+ * \param p1 Process to be compared
97
+ * \param p2 Process to be compared
98
+ * \return
99
+ */
42
100
bool CompareAT (const Process& p1, const Process& p2) {
43
101
return p1.arrival_time < p2.arrival_time ;
44
102
}
45
103
104
+ /* *
105
+ * @brief Check processes that arrive after the given time_elapsed.
106
+ *
107
+ * If a process arrive after the give time_elapsed, then the process
108
+ * will be pushed into the schedule queue and inserted into the
109
+ * arrived_process set.
110
+ *
111
+ * \param processes Processes that will be checked for arrival
112
+ * \param arrived_process A set containing processes that has arrived
113
+ * \param schedule Queue containing pair of process and its remaining burst time
114
+ * \param time_elapsed Time that has elapsed after processes execution
115
+ */
116
+ void CheckArriveProcess (const std::vector<Process>& processes,
117
+ std::set<uint32_t >& arrived_process,
118
+ std::queue<std::pair<Process, uint32_t >>& schedule,
119
+ uint32_t time_elapsed);
120
+
121
+ /* *
122
+ * @brief Entry point of the program.
123
+ *
124
+ * \return 0 on exit
125
+ */
46
126
int main () {
47
127
Test ();
48
128
return 0 ;
@@ -68,11 +148,6 @@ void Test() {
68
148
std::cout << " All test passed" ;
69
149
}
70
150
71
- void CheckArriveProcess (const std::vector<Process>& processes,
72
- std::set<uint32_t >& arrived_process,
73
- std::queue<std::pair<Process, uint32_t >>& schedule,
74
- uint32_t time_elapsed);
75
-
76
151
std::vector<ProcessResult> RRExecute (const std::vector<Process>& processes,
77
152
uint32_t time_slice) {
78
153
std::vector<Process> sorted_processes = processes;
0 commit comments