-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathround-robbin.cpp
212 lines (181 loc) · 6.82 KB
/
round-robbin.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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <algorithm>
#include <map>
#include <math.h>
using namespace std;
// Define a structure to represent the data
struct DataPoint {
string name;
double arrTime;
double execTime;
double burstTime;
double remainingBurstTime;
double ioBlockTime;
int priority;
};
class compare{
public:
bool operator()(DataPoint a, DataPoint b) {
if(a.arrTime == b.arrTime){
return a.priority > b.priority;
}
else{
return a.arrTime > b.arrTime;
}
}
};
// Function to compare two DataPoint objects for sorting
bool compareByarrTime(const DataPoint& a, const DataPoint& b) {
return a.arrTime < b.arrTime;
}
void print(priority_queue<DataPoint,vector<DataPoint>, compare>& q){
while(!q.empty()){
DataPoint point = q.top();
q.pop();
cout << point.name << " " << point.arrTime << " " << point.execTime <<" "<<point.burstTime << " " << point.remainingBurstTime << " " << point.ioBlockTime << " " << point.priority << endl;
}
cout<<endl;
}
int main() {
// Open the input file
string fileName;
cout<<"Enter Filename:- "<<endl;
cin>>fileName;
ifstream inputFile(fileName);
// Check if the file is open
if (!inputFile.is_open()) {
std::cerr << "Failed to open the file." << std::endl;
return 1;
}
cout<<"The name of the file to be read :- "<<fileName<<endl;
vector<DataPoint> data; // Vector to store the data
priority_queue<DataPoint,vector<DataPoint>, compare> readyQueue;
// Read data from the file into the vector
while (true) {
DataPoint point;
if (inputFile >> point.name >> point.arrTime >> point.execTime >> point.burstTime >> point.ioBlockTime >> point.priority) {
point.remainingBurstTime = point.burstTime;
data.push_back(point);
readyQueue.push(point);
} else {
break;
}
}
// Close the file
inputFile.close();
// Sort the data based on the first arrTime
sort(data.begin(), data.end(), compareByarrTime);
// Print the sorted data
for (const auto& point : data) {
cout << point.name << " " << point.arrTime << " " << point.execTime << " "<<point.burstTime<<" " << point.remainingBurstTime << " " << point.ioBlockTime << " " << point.priority << endl;
cout<<endl;
}
map<string,double> initialArrTime;
for(const auto& point : data){
initialArrTime[point.name] = point.arrTime;
}
map<string,double> tat;
map<string,double> wt;
double timeSlice = 3.0;
double timer = data[0].arrTime;
priority_queue<DataPoint,vector<DataPoint>, compare> tmp = readyQueue;
print(tmp);
while(true){
//in every time slice a process has to be picked with min arrival time
//every iteration is a single new time slice
DataPoint curr;
if(!readyQueue.empty()){
curr = readyQueue.top(); //pointer to current process
readyQueue.pop();
cout<<"Process execution start:- "<<curr.name<<endl;
}
double currNextArrival = -1;
double startTime = timer; //starting timer
cout<<"Starting time:- "<<startTime<<endl;
if(startTime < curr.arrTime){
timer = curr.arrTime; //taking timer to arrival time of current
startTime = curr.arrTime;
//CPU IDLE
}
else{
wt[curr.name] += timer - curr.arrTime;
}
cout<<"Before Updating "<<curr.name << " " << curr.arrTime << " " << curr.execTime <<" "<<curr.burstTime<< " " << curr.remainingBurstTime << " " << curr.ioBlockTime << " " << curr.priority << endl;
//have to give CPU to curr for timeSlice, if it gets over in b/w then CPU idle
if(curr.remainingBurstTime > timeSlice){
//would have run for entire timeSlice
if(curr.execTime > curr.remainingBurstTime){
curr.remainingBurstTime -= timeSlice;
curr.execTime -= timeSlice;
timer+=timeSlice; //complete time slice
//calculate next arrival
currNextArrival = max(startTime,curr.arrTime) + timeSlice ; //=timer,as exec stopped im b/w
}
else if(curr.execTime <= curr.remainingBurstTime){
//exec finishes in b/w
//calculate tat
//out of timeSlice it executed for curr->execTime
//startTime -> when this process picked
timer+=curr.execTime;
tat[curr.name] = startTime + curr.execTime - initialArrTime[curr.name];
currNextArrival = -1; //will not arrive after this
}
}
else if(curr.remainingBurstTime <= timeSlice) {
cout<<"HERE"<<endl;
//burst ends in b/w
if(curr.execTime > curr.remainingBurstTime){
curr.execTime -= curr.remainingBurstTime;
double temp = curr.remainingBurstTime;
curr.remainingBurstTime = 0;
curr.remainingBurstTime = curr.burstTime; //Refresh
//will go for IO
//calculate next arrival
timer+=temp;
currNextArrival = max(startTime,curr.arrTime) + temp + curr.ioBlockTime;
}
else{
//exec finishes in b/w
//calculate tat
timer+=curr.execTime;
tat[curr.name] = startTime + curr.execTime - initialArrTime[curr.name];
currNextArrival = -1;
}
}
if(currNextArrival == -1){
//no new entry in queue
if(readyQueue.empty()) break;
else continue; //nothing to push
}
DataPoint updated;
updated.name = curr.name;
updated.arrTime = currNextArrival;
updated.remainingBurstTime = curr.remainingBurstTime;
updated.burstTime = curr.burstTime;
updated.execTime = curr.execTime;
updated.ioBlockTime = curr.ioBlockTime;
updated.priority = curr.priority;
readyQueue.push(updated);
tmp = readyQueue;
cout<<endl;
cout<<"After Updating "<<updated.name << " " << updated.arrTime << " " << updated.execTime << " " <<updated.burstTime<<" "<< updated.remainingBurstTime << " " << updated.ioBlockTime << " " << updated.priority << endl;
cout<<endl;
print(tmp);
}
cout<<"DONE"<<endl;
for(auto &it:tat){
cout<<"Turnaround time of "<<it.first << " " << it.second <<endl;
}
for(auto &it:data){
if(wt.find(it.name) == wt.end()){
wt[it.name] = 0;
}
}
for(auto &it:wt){
cout<<"Waiting time of "<<it.first << " " << it.second <<endl;
}
return 0;
}