forked from mayank9804/OS-scheduling-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultilevel Queue Scheduling Algorithms.cpp
More file actions
330 lines (288 loc) · 9.69 KB
/
Multilevel Queue Scheduling Algorithms.cpp
File metadata and controls
330 lines (288 loc) · 9.69 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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*Ques. 13. Write a program for multilevel queue scheduling algorithm. There must be three
/queues generated. There must be specific range of priority associated with every queue. Now
prompt the user to enter number of processes along with their priority and burst time. Each
process must occupy the respective queue with specific priority range according to its priority.
Apply Round robin algorithm with quantum time 4 on queue with highest priority range. Apply
priority scheduling algorithm on the queue with medium range of priority and First come first
serve algorithm on the queue with lowest range of priority. Each and every queue should get a
quantum time of 10 seconds. Cpu will keep on shifting between queues after every 10
seconds i.e. to apply round robin algorithm of 10 seconds on over all structure.
Calculate Waiting time and turnaround time for every process. The input for number of
processes should be given by the user.
*/
//ALGORITHM
//Initiate 3 queues and associate specific range of priority with every queue
//Enter number of processes along with their priority and burst time.
//Each process should occupy respective queue
//Apply Round Robin Algorithm (q=4) with highest priority range
//Apply Priority Scheduling Algorithm on medium priority range
//First Come First Serve on lowest priority range
//Each queue will only get 10 seconds
//Round Robin on overall structure
//q1 : p1,p2,p3 |RR(4) |
//q2 : p4,p5,p6 |PS ---| Round Robin (10)
//q3 : p7,p8,p9 |FCFS |
#include <iostream>
using namespace std;
struct process{
int priority;
int burst_time;
int tt_time;
int total_time=0;
};
struct queues{
int priority_start;
int priority_end;
int total_time=0;
int length = 0;
process *p;
bool executed = false;
};
bool notComplete(queues q[]){
bool a=false;
int countInc=0;
for(int i=0;i<3;i++){
countInc=0;
for(int j=0;j<q[i].length;j++){
if(q[i].p[j].burst_time != 0){
a=true;
}
else{
countInc+=1;
}
}
if(countInc==q[i].length){
q[i].executed = true;
}
}
return a;
}
void sort_ps(queues q){
//Queue q has to be sorted according to priority of processes
for(int i=1;i<q.length;i++){
for(int j=0;j<q.length-1;j++){
if(q.p[j].priority<q.p[j+1].priority){
process temp = q.p[j+1];
q.p[j+1] = q.p[j];
q.p[j] = temp;
}
}
}
}
void checkCompleteTimer(queues q[]){
bool a = notComplete(q);
for(int i=0;i<3;i++){
if(q[i].executed==false){
for(int j=0;j<q[i].length;j++){
if(q[i].p[j].burst_time!=0){
q[i].p[j].total_time+=1;
}
}
q[i].total_time+=1;
}
}
}
main(){
//Initializing 3 queues
queues q[3];
q[0].priority_start = 7;
q[0].priority_end = 9;
q[1].priority_start = 4;
q[1].priority_end = 6;
q[2].priority_start = 1;
q[2].priority_end = 3;
int no_of_processes,priority_of_process,burst_time_of_process;
//Prompt User for entering Processes and assigning it to respective queues.
cout<<"Enter the number of processes\n";
cin>>no_of_processes;
process p1[no_of_processes];
for(int i=0;i<no_of_processes;i++){
cout<<"Enter the priority of the process\n";
cin>>priority_of_process;
cout<<"Enter the burst time of the process\n";
cin>>burst_time_of_process;
p1[i].priority = priority_of_process;
p1[i].burst_time = burst_time_of_process;
p1[i].tt_time = burst_time_of_process;
for(int j=0;j<3;j++){
if(q[j].priority_start<=priority_of_process && priority_of_process<=q[j].priority_end){
q[j].length++;
}
}
}
for(int i =0;i<3;i++){
int len = q[i].length;
q[i].p = new process[len];
}
int a=0;
int b=0;
int c=0;
for(int i =0;i<3;i++){
for(int j=0;j<no_of_processes;j++){
if((q[i].priority_start<=p1[j].priority) && (p1[j].priority<=q[i].priority_end)){
if(i==0){
q[i].p[a++] = p1[j];
}
else if(i==1){
q[i].p[b++] = p1[j];
}
else{
q[i].p[c++] = p1[j];
}
}
}
}
a--;b--;c--;
for(int i=0;i<3;i++){
cout<<"Queue "<<i+1<<" : \t";
for(int j=0;j<q[i].length;j++){
cout<<q[i].p[j].priority<<"->";
}
cout<<"NULL\n";
}
//While RR on multiple queues is not complete, keep on repeating
int timer = 0;
int l =-1;
int rr_timer = 4;
int counter=0;
int counterps=0;
int counterfcfs=0;
while(notComplete(q)){
if(timer == 10){
timer = 0;
}
l+=1;
if(l>=3){
l=l%3;
}
//Process lth queue if its already not executed
//If its executed change the value of l
if(q[l].executed == true){
cout<<"Queue "<<l+1<<" completed\n";
l+=1;
if(l>=3){
l=l%3;
}
continue;
}
//Finally you now have a queue which is not completely executed
//Process the incomplete processes over it
if(l==0){
cout<<"Queue "<<l+1<<" in hand\n";
//Round Robin Algorithm for q=4
if(rr_timer == 0){
rr_timer = 4;
}
for(int i=0;i<q[l].length;i++){
if(q[l].p[i].burst_time==0){
counter++;
continue;
}
if(counter == q[l].length){
break;
}
while(rr_timer>0 && q[l].p[i].burst_time!=0 && timer!=10){
cout<<"Executing queue 1 and "<<i+1<<" process for a unit time. Process has priority of "<<q[l].p[i].priority<<"\n";
q[l].p[i].burst_time--;
checkCompleteTimer(q);
rr_timer--;
timer++;
}
if(timer == 10){
break;
}
if(q[l].p[i].burst_time==0 && rr_timer ==0){
rr_timer = 4;
if(i == (q[i].length-1)){
i=-1;
}
continue;
}
if(q[l].p[i].burst_time==0 && rr_timer > 0){
if(i == (q[i].length-1)){
i=-1;
}
continue;
}
if(rr_timer <= 0){
rr_timer = 4;
if(i == (q[i].length-1)){
i=-1;
}
continue;
}
}
}
else if(l==1){
cout<<"Queue "<<l+1<<" in hand\n";
sort_ps(q[l]);
//Priority Scheduling
for(int i=0;i<q[l].length;i++){
if(q[l].p[i].burst_time==0){
counterps++;
continue;
}
if(counterps == q[l].length){
break;
}
while(q[l].p[i].burst_time!=0 && timer!=10){
cout<<"Executing queue 2 and "<<i+1<<" process for a unit time. Process has priority of "<<q[l].p[i].priority<<"\n";
q[l].p[i].burst_time--;
checkCompleteTimer(q);
timer++;
}
if(timer == 10){
break;
}
if(q[l].p[i].burst_time==0){
continue;
}
}
}
else{
cout<<"Queue "<<l+1<<" in hand\n";
//FCFS
for(int i=0;i<q[l].length;i++){
if(q[l].p[i].burst_time==0){
counterfcfs++;
continue;
}
if(counterfcfs == q[l].length){
break;
}
while(q[l].p[i].burst_time!=0 && timer!=10){
cout<<"Executing queue 3 and "<<i+1<<" process for a unit time. Process has priority of "<<q[l].p[i].priority<<"\n";
q[l].p[i].burst_time--;
checkCompleteTimer(q);
timer++;
}
if(timer == 10){
break;
}
if(q[l].p[i].burst_time==0){
continue;
}
}
}
cout<<"Broke from queue "<<l+1<<"\n";
}
for(int i=0;i<3;i++){
cout<<"\nTime taken for queue "<<i+1<<" to execute: "<<q[i].total_time<<"\n";
for(int j=0;j<q[i].length;j++){
cout<<"Process "<<j+1<<" of queue "<<i+1<<" took "<<q[i].p[j].total_time<<"\n";
}
}
int sum_tt=0;
int sum_wt=0;
cout<<"\n\nProcess | Turn Around Time | Waiting Time\n";
for(int i=0;i<3;i++){
cout<<"Queue "<<i+1<<"\n";
for(int j=0;j<q[i].length;j++){
cout<<"Process P"<<j+1<<"\t"<<q[i].p[j].total_time<<"\t\t "<<q[i].p[j].total_time-q[i].p[j].tt_time<<"\n";
sum_tt+=q[i].p[j].total_time;
sum_wt+=q[i].p[j].total_time-q[i].p[j].tt_time;
}
}
cout<<"\n The average turnaround time is : "<<sum_tt/no_of_processes<<endl;
cout<<"\n The average waiting time is : "<<sum_wt/no_of_processes<<endl;
}