-
Notifications
You must be signed in to change notification settings - Fork 0
/
RoundRobin.cpp
executable file
·124 lines (104 loc) · 3.81 KB
/
RoundRobin.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include "Task.h"
#include <vector>
#include <queue> // std::queue
#include <string>
#include <stdio.h>
#include <deque>
#include <stdlib.h>
#include <algorithm>
using namespace std;
//To sort by arrival time
bool my_sort (const Task& lhs, const Task& rhs) {
return lhs.arrival < rhs.arrival;
}
/*
* Implement this function using the Round Robin algorithm in Part 2.
*/
void schedule(std::vector<Task> tasks, int quantum) {
int size = tasks.size(); //calculate size
sort(tasks.begin(), tasks.end(), my_sort); //sort tasks, using my_sort above
queue<Task> ready_queue; //new queue, ready_queue
int current_time = 0; //keep track of current time
Task current_task; //declare new task, current task
int i = 0; //iterator i
int quant = 0; //set quant counter to keep trakc of quantum
bool busy = false; //set boolean flag busy to false
int completed_tasks = 0; //counter to keep track of completed_tasks
double waiting_time = 0; //counter to keep track of waiting_time for ALL tasks
/*
While completed_tasks counter is less than size, we continue
*/
while(completed_tasks < size) {
/*
For int j, we loop through each task
If arrival time of task is same as current time
Then we add the task (and push the task into queue)
*/
for (int j = 0; j < size; j++) {
if(tasks[j].arrival == current_time) {
cout << tasks[j].arrival << ": Adding task " << tasks[j].name <<endl;
ready_queue.push(tasks[j]);
}
}
/*
If quant counter equals quantum and the current task has reamining time left
Then we print the Time Slice for this task (at current time)
And then we push current stack onto queue
And set busy flag to false
*/
if(quant == quantum && current_task.remaining > 0) {
cout << current_time << ": Time Slice Done for Task " << current_task.name <<endl;
ready_queue.push(current_task);
busy = false;
// current_task = ready_queue.front();
}
/*
If current_task remaining time is 0 AND busy flag is true
Then the current task is finished
We also calculate waiting time of current task and add it to the "waiting_time" counter
We are no longer busy, so busy flag is false, and completed tasks increments by 1
*/
if(current_task.remaining == 0 && busy == true) {
cout << current_time << ": Finished task " << current_task.name << endl;
waiting_time = waiting_time + (current_time - current_task.arrival - current_task.time);
busy = false;
completed_tasks++;
}
/*
If the ready_queue is NOT empty and busy is false, well then
The current task is going to be set to the ready queue (first object)
And we print that we are now running the current task
Note: we then need to pop the current task off the queue
We set our busy flag to true, and reset our quant to 0 (since we were not busy, we know it should be 0)
*/
if(!ready_queue.empty() && busy == false) {
current_task = ready_queue.front(); //return first object
cout << current_time << ": Running task " << current_task.name <<endl;
ready_queue.pop(); //pop it off
busy = true;
quant = 0;
}
/*
If current_task has time remaining and quant is less than quantum
We will decrement the time remaining for current task (we are running)
And increment the quant (we are busy)
*/
if(current_task.remaining > 0 && quant < quantum ) {
current_task.remaining --;
quant ++;
}
/*
If completed tasks equals to size, well then we have finished all tasks!
Print current time
*/
if(completed_tasks == size) {
cout<< (current_time) << ": All tasks finished" << endl;
}
current_time++; //increment current_time (does not impact finished tasks statement above)
}
/*
Calculate average waiting time which is just waiting_time over size
*/
double average = waiting_time / size;
cout<< "The average waiting time for tasks is: " << average << "." << endl;
}