-
Notifications
You must be signed in to change notification settings - Fork 0
/
Round Robin_without Arrival.cpp
153 lines (115 loc) · 3.04 KB
/
Round Robin_without Arrival.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
#include<bits/stdc++.h>
using namespace std;
typedef vector<int>vii;
const int high = 1e3+3;
struct info
{
int process;
int start , end;
};
info arr[high];
struct db
{
int start , end;
db(int s , int e)
{
start = s;
end = e;
}
};
vector<int>adj[high];
int burst[high] , len=0, calculate[high] , flag[high] , brst[high];
void Round_Robin_without_Arrival(int time_quantum , int indx , int num)
{
int split =0, waiting = 0 , i=0;
while(burst[indx] > 0)
{
if(burst[i] > 0)
{
arr[len].start = waiting;
split = burst[i] - time_quantum;
if(burst[i] > time_quantum) waiting += time_quantum;
else waiting += burst[i];
burst[i] = split;
//cout << waiting << "; ";
arr[len].end = waiting;
arr[len].process = i+1;
len++;
}
if(i == num) i=0;
else i++;
}
}
int main()
{
int num , i;
cout << "Enter Number of Process: ";
cin >> num;
int maxi = -1 , indx = 0;
cout << "Enter Burst times: ";
for(i=0; i<num; i++)
{
cin >> burst[i];
brst[i] = burst[i];
if(maxi < burst[i])
{
maxi = burst[i];
indx = i;
}
}
int time_quantum , split;
cout << "Enter Time Quantum: ";
cin >> time_quantum;
Round_Robin_without_Arrival(time_quantum , indx , num);
cout << "\n\t------Result-----\n";
//cout << "\nProcess\tWaiting time\t" << "Process\n";
for(i=0; i<len; i++)
{
cout << " " << arr[i].process << " " << arr[i].start << " " << arr[i].end << "\n";
}
int j , calculate[high];
for(i=1; i<=num; i++)
{
int curr = i;
for(j=0; j<len; j++)
{
if(curr == arr[j].process)
{
int s = arr[j].start;
int e = arr[j].end;
adj[curr].push_back(s);
adj[curr].push_back(e);
calculate[curr] = adj[curr][0];
}
}
}
for(i=1; i<=num; i++)
{
for(j=1; j<adj[i].size()-1; j+=2)
{
if(j > 0)
{
int x = adj[i][j];
int y = adj[i][j+1];
//cout << "x = " << x << " y = " << y << "\n";
calculate[i] = calculate[i] + (y - x);
}
}
}
double avgWaiting = 0.0, turn = 0.0;
for(i=1; i<=num; i++)
{
//cout << calculate[i] << "; ";
avgWaiting += (calculate[i] * 1.0);
}
avgWaiting /= (num * 1.0);
cout << "Average Waiting Time: " << avgWaiting << "\n";
for(i=1; i<=num; i++)
{
turn += ((brst[i-1] + calculate[i]) * 1.0);
}
cout << "Turn Around Time: " << turn << "\n";
double avgTurn = turn / (num * 1.0);
cout << "Average Turn Around Time: " << avgTurn << "\n";
return 0;
}