-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoundrobin.cpp
More file actions
182 lines (165 loc) · 5.33 KB
/
Copy pathRoundrobin.cpp
File metadata and controls
182 lines (165 loc) · 5.33 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// Round Robin
// This is the implementation of the Round Robin scheduling algorithm.
//
// Created by altanai on 08/02/21.
#include <string>
#include <iostream>
#include "Processes.h"
#include <fstream>
#include <list>
#include <iterator>
#include <typeinfo>
#include <queue>
#include "Roundrobin.h"
//Constructor
Roundrobin::Roundrobin() {
lastArrival = -1;
CPUtimer = -1; //CPU time hasn't started
sumTurnaroundTime = 0;
sumWaitingTime = 0;
countOfProcesses = 0; //FIXME
countTotalTime = 0;
newArrival = -1;
}
//gets the data from the file and puts them into a list of Process objects
void Roundrobin::getFileData(string fileName) {
//print out all values from file
ifstream afile;
string line;
afile.open(fileName); //fixme
list <Processes> alist;
//make each line a process
while (afile >> line) {
countOfProcesses++;
alist.push_back(makeProcess(line));
}
newArrival = countOfProcesses;
//do an array of size lastArrival and fill it with the processes
putProcessInArray(alist);
list<Processes>::iterator itr;
//make a min heap
//set up logic of putting in min heap if the time is equal or less
}
//puts all the processes in an array and then
//uses a minqueue to process them.
void Roundrobin::putProcessInArray(list <Processes> alist) {
//make array of lists
list <Processes> allProcesses[lastArrival + 1];
//fill array with empty list
for (int i = 0; i < lastArrival + 1; i++) {
list <Processes> temp;
allProcesses[i] = temp;
}
//put these processes in an array based on arrival time
list<Processes>::iterator itr;
for (itr = alist.begin(); itr != alist.end(); ++itr) {
allProcesses[itr->getArrivalTime()].push_back(*itr);
}
//initialize a min heap and put all the processes that start at 0
int beg = 0;
CPUtimer++;
priority_queue <Processes> q;
while (CPUtimer <= lastArrival) {
for (int i = beg; i <= CPUtimer; i++) {
if (allProcesses[i].empty()) {
//do nothing
} else {
//add it to the queue
list<Processes>::iterator itr2;
for (itr2 = allProcesses[i].begin(); itr2 != allProcesses[i].end(); ++itr2) {
q.push(*itr2);
}
}
}
Processes tempP = q.top();
tempP.printValues();
beg = CPUtimer + 1;
CPUtimer = CPUtimer + 1;
//subtract one unit of time from top
tempP.CPUburst = tempP.CPUburst - 1;
//pop, if greater than zero add back in, if 0 don't add
if (tempP.CPUburst == 0) {
sumTurnaroundTime = sumTurnaroundTime + (CPUtimer - tempP.arrivalTime);
countTotalTime = CPUtimer;
int turnaroundtime = CPUtimer - tempP.arrivalTime;
int waitingtime = turnaroundtime - tempP.tempCPUburst;
sumWaitingTime = sumWaitingTime + waitingtime;
q.pop();
} else {
q.pop();
newArrival = CPUtimer;
tempP.priority = -1; //appear first
tempP.tempArrivalTime = newArrival;
q.push(tempP);
}
}
//pop remaining
while (!q.empty()) {
Processes tempP = q.top();
tempP.printValues();
CPUtimer = CPUtimer + 1;
//subtract one unit of time from top
tempP.CPUburst = tempP.CPUburst - 1;
//pop, if greater than zero add back in, if 0 don't add
if (tempP.CPUburst == 0) {
sumTurnaroundTime = sumTurnaroundTime + (CPUtimer - tempP.arrivalTime);
countTotalTime = CPUtimer;
int turnaroundtime = CPUtimer - tempP.arrivalTime;
int waitingtime = turnaroundtime - tempP.tempCPUburst;
sumWaitingTime = sumWaitingTime + waitingtime;
q.pop();
} else {
q.pop();
newArrival = CPUtimer;
tempP.priority = -1;
tempP.tempArrivalTime = newArrival;
q.push(tempP);
}
}
return;
}
//goes through text and gets the data from the file
//to make a Process object
Processes Roundrobin::makeProcess(string line) {
int commas = 0;
string v1 = "";
string v2 = "";
string v3 = "";
for (int i = 0; i < line.size(); i++) {
if (line[i] == ',') {
commas++;
} else {
if (commas == 0) {
v1 = v1 + line.at(i);
} else if (commas == 1) {
v2 = v2 + line.at(i);
} else {
v3 = v3 + line.at(i);
}
}
}
Processes p(stoi(v1), stoi(v2), stoi(v3));
//find out last arrival time;
if (stoi(v3) > lastArrival) {
lastArrival = stoi(v3);
}
// p.printValues();
return p;
}
//calculates and returns the turnaroundtime
double Roundrobin::getAverageTurnaroundtime() {
double ans = (double(sumTurnaroundTime) / double(countOfProcesses));
//FIXME set precision higher
return ans;
}
//calculates and returns the averagewait time
double Roundrobin::getAverageWaitingTime() {
double ans = (double(sumWaitingTime) / double(countOfProcesses));
//FIXME set precision higher
return ans;
}
//calculates and returns the throughput
double Roundrobin::getThroughput() {
double ans = (double(countOfProcesses) / double(countTotalTime));
return ans;
}