-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRTOS_RR.cpp
161 lines (159 loc) · 4.61 KB
/
RTOS_RR.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
#include<iostream>
#include<math.h>
#include<iomanip>
using namespace std;
int main()
{
int n;
cout << "\nEnter the number of processes:";
cin >> n;
int bt[n], p[n], s[n], wt[n], tat[n], ts, its[n], tq[n][n], rbt[n], ord[n];
for (int i = 0; i < n; i++)
{
wt[i] = tat[i] = 0;
s[i] = 1;
for (int j = 0; j < n; j++)
tq[i][j] = 0;
}
cout << "\nEnter the initial time slice: ";
cin >> ts;
for (int i = 0; i < n; i++)
{
ord[i] = i + 1;
cout << "\nEnter burst time for process " << i + 1 << ":";
cin >> bt[i];
cout << "\nEnter the priority of the process " << i + 1 << ":";
cin >> p[i];
}
int flag = 0, j = 0;
for (int i = 0; i < n - 1; i++)
for (int j = 0; j < n - 1; j++)
if (bt[j] > bt[j - 1])
{
int t = bt[j];
bt[j] = bt[j + 1];
bt[j + 1] = t;
t = p[j];
p[j] = p[j + 1];
p[j + 1] = t;
t = ord[j];
ord[j] = ord[j + 1];
ord[j + 1] = t;
}
for (int i = 0; i < n; i++)
p[i] += i;
for (int i = 0; i < n - 1; i++)
for (int j = 0; j < n - 1; j++)
if (p[j] > p[j + 1])
{
int t = bt[j];
bt[j] = bt[j + 1];
bt[j + 1] = t;
t = p[j];
p[j] = p[j + 1];
p[j + 1] = t;
t = ord[j];
ord[j] = ord[j + 1];
ord[j + 1] = t;
}
for (int i = 0; i < n; i++)
rbt[i] = bt[i];
while (!flag)
{
for (int i = 0; i < n; i++)
{
if (p[i] > 0.67 * n)
p[i] = 0;
else if (p[i] > 0.33 * n)
p[i] = 1;
else
p[i] = 2;
if (i != 0)
if ((bt[i] - bt[i - 1]) > 0)
s[i] = 0;
its[i] = ts + bt[i] + s[i] + p[i];
if (j == 0)
{
if (s[i] == 1)
tq[j][i] = its[i];
else
tq[j][i] = ceil(0.5 * (float)its[i]);
if (rbt[i] < tq[j][i])
tq[j][i] = rbt[i];
rbt[i] = rbt[i] - tq[j][i];
}
else
{
if (rbt[i] <= 0)
tq[j][i] = 0;
else if (s[i] == 1)
tq[j][i] = 2 * tq[j - 1][i];
else
tq[j][i] = 1.5 * tq[j - 1][i];
if (rbt[i] < tq[j][i])
tq[j][i] = rbt[i];
rbt[i] = rbt[i] - tq[j][i];
}
}
j++;
flag = -1;
for (int i = 0; i < n; i++)
if (rbt[i] > 0)
flag = 0;
}
cout << "\n\nProcess no.:\n";
for (int i = 0; i < n; i++)
cout << setw(5) << ord[i];
cout << "\n\nBurst Times for the processes:\n";
for (int i = 0; i < n; i++)
cout << setw(5) << bt[i];
cout << "\n\nIntelligent Time Slices for the processes:\n";
for (int i = 0; i < n; i++)
cout << setw(5) << its[i];
cout << "\n\nDynamic Time Quantums for the processes:\n";
for (int x = 0; x < j; x++)
{
cout << "Round " << x + 1 << ": " << endl;
for (int y = 0; y < n; y++)
cout << setw(5) << tq[x][y];
cout << endl;
}
for (int x = 0; x < n; x++)
{
flag = -1;
for (int y = 0; y < j; y++)
{
for (int z = 0; z < n; z++)
{
if (z != x)
wt[x] += tq[y][z];
else if (z == x && tq[y + 1][z] == 0)
{
flag = 0;
break;
}
}
tat[x] += tq[y][x];
if (flag == 0)
break;
}
tat[x] += wt[x];
}
cout << "\nWaiting time for the processes:\n";
for (int i = 0; i < n; i++)
cout << setw(5) << wt[i];
cout << "\n\nTurnaround time for the processes:\n";
for (int i = 0; i < n; i++)
cout << setw(5) << tat[i];
float avwt = 0, avtat = 0;
for (int i = 0; i < n; i++)
{
avwt += wt[i];
avtat += tat[i];
}
avwt /= n;
avtat /= n;
cout << "\n\nAverage waiting time: " << avwt << endl;
cout << "\nAverage turnaround time: " << avtat << endl;
return 0;
}