-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathround_robin.cpp
More file actions
171 lines (144 loc) · 4.84 KB
/
Copy pathround_robin.cpp
File metadata and controls
171 lines (144 loc) · 4.84 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
//Round Robin Scheduling
//Input File name :- "input.dat" (see reference input file)
//Output File name :- "round_robin_out.dat"
//Compile:- g++ round_robin.cpp -o round_robin
//Run:- ./round_robin
//Assuming processes in file are in sorted arrival time manner
#include<bits/stdc++.h>
using namespace std;
int main()
{
string line;
int n,pid,arrival=0,burst=0,quantum,prev=-1,start=0,count=0,tat_sum=0,rt_sum=0,wt_sum=0;
ifstream infile("input.dat");
ofstream outfile("round_robin_out.dat");
if(!infile)
{
cerr << "Datafile does not exist" << endl;
exit(-1);
}
getline(infile,line);
stringstream str(line);
str >> n; //no. of processes
vector<tuple<int,int,int>> process(n); //tuple = <burst_time,arrival_time,pid>
map<int,pair<int,int>> proc_count; //(arrival,(index,count))
for(int i=0; i < n; i++)
{
getline(infile,line);
stringstream str(line);
str >> arrival;
str >> burst;
if(arrival == prev)
count++;
else
{
if(i!=0)
{
proc_count.insert({prev,{start,count}});
}
count = 1; prev = arrival;
start = i;
}
process[i] = make_tuple(burst,arrival,i+1);
}
proc_count.insert({prev,{start,count}});
getline(infile,line);
stringstream str1(line); //getting quantum value
str1 >> quantum;
infile.close(); //read complete
//Round Robin scheduling calculations
tuple<int,int,int> curr_process;
pair<int,int> st_ct;
vector<tuple<int,int,int>> proc_queue;
vector<int> turn_around_time(n),waiting_time(n),response_time(n),tsched(n,-1),completion(n,0);
int time = -1,flag = 0,lastpid = 0;
count = 0; //no. of completed processes
prev = 0;
while(count != n)
{
if(!proc_queue.empty())
{
curr_process = proc_queue[0];
burst = get<0>(curr_process);
arrival = get<1>(curr_process);
pid = get<2>(curr_process);
proc_queue.erase(proc_queue.begin()); //dequeing
completion[pid-1] += quantum; //time spent by process in CPU
if(tsched[pid-1] == -1)
{
tsched[pid-1] = time;
response_time[pid-1] = time - arrival;
waiting_time[pid-1] = response_time[pid-1];
}
else
{
waiting_time[pid-1] += time - tsched[pid-1] - quantum;
tsched[pid-1] = time;
}
if(completion[pid-1] < burst)
{
flag = 1;
time += quantum;
}
else if(completion[pid-1] == burst)
{
count++;
flag = 0;
time += quantum;
turn_around_time[pid-1] = time - arrival;
}
else
{
count++;
flag = 0;
time += completion[pid-1] - burst;
turn_around_time[pid-1] = time - arrival;
}
if(pid != lastpid)
{
lastpid = pid;
cout << "Process " << pid <<" at time " << tsched[pid-1] << endl;
}
prev = tsched[pid-1];
}
else
{
prev = time;
time++;
}
for(int i=prev+1; i <= time; i++)
{
auto itr = proc_count.find(i);
if(itr != proc_count.end())
{
st_ct = itr->second;
start = st_ct.first;
for(int j=start; j < start+st_ct.second; j++)
proc_queue.push_back(process[j]);
}
}
if(flag)
proc_queue.push_back(curr_process);
} //while loop ends
cout << endl;
outfile << "PID\tTurnaround\tResponse\tWaiting" << endl;
cout << "PID\tTurnaround\tResponse\tWaiting" << endl;
for(int i=0; i < n; i++)
{
outfile << i+1 << "\t\t" << turn_around_time[i] << "\t\t " << response_time[i] << "\t\t\t" << waiting_time[i] << endl;
cout << i+1 << "\t" << turn_around_time[i] << "\t\t" << response_time[i] << "\t\t" << waiting_time[i] << endl;
tat_sum += turn_around_time[i];
rt_sum += response_time[i];
wt_sum += waiting_time[i];
}
outfile << endl;
cout << endl;
outfile << "Average Turnaround Time = " << (float)tat_sum/n << endl;
outfile << "Average Response Time = " << (float)rt_sum/n << endl;
outfile << "Average Waiting Time = " << (float)wt_sum/n << endl;
cout << "Average Turnaround Time = " << (float)tat_sum/n << endl;
cout << "Average Response Time = " << (float)rt_sum/n << endl;
cout << "Average Waiting Time = " << (float)wt_sum/n << endl;
outfile.close();
return 0;
}