-
Notifications
You must be signed in to change notification settings - Fork 1
/
Multilevel_Feedback_Queue_Scheduling.cpp
121 lines (102 loc) · 3.71 KB
/
Multilevel_Feedback_Queue_Scheduling.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
// C++ implementaion of 'Multilevel Feedback Queue' CPU Scheduling Algorithm.
#include<stdio.h>
#define N 10
typedef struct
{
int process_id, arrival_time, burst_time, priority;
int q, ready;
}process_structure;
int Queue(int t1)
{
if(t1 == 0 || t1 == 1 || t1 == 2 || t1 == 3)
{
return 1;
}
else
{
return 2;
}
}
int main()
{
int limit, i, temp_process, time, j, y;
process_structure temp;
printf("\n\nEnter Total Number of Processes:\t");
scanf("%d", &limit);
process_structure process[limit];
for(i = 0; i < limit; i++)
{
printf("\nProcess ID:\t");
scanf("%d", &process[i].process_id);
printf("Arrival Time:\t");
scanf("%d", &process[i].arrival_time);
printf("Burst Time:\t");
scanf("%d", &process[i].burst_time);
printf("Process Priority:\t");
scanf("%d", &process[i].priority);
temp_process = process[i].priority;
process[i].q = Queue(temp_process);
process[i].ready = 0;
}
time = process[0].burst_time;
for(y = 0; y < limit; y++)
{
// Do ready all the processes from "y" which has arrival time less than burst time of first process.
for(i = y; i < limit; i++)
{
if(process[i].arrival_time < time)
{
process[i].ready = 1;
}
}
for(i = y; i < limit - 1; i++)
{
for(j = i + 1; j < limit; j++)
{
if(process[i].ready == 1 && process[j].ready == 1)
{
if(process[i].q == 2 && process[j].q == 1)
{
// swapping...
temp = process[i];
process[i] = process[j];
process[j] = temp;
}
}
}
}
for(i = y; i < limit - 1; i++)
{
for(j = i + 1; j < limit; j++)
{
if(process[i].ready == 1 && process[j].ready == 1)
{
if(process[i].q == 1 && process[j].q == 1)
{
if(process[i].burst_time > process[j].burst_time)
{
// swapping...
temp = process[i];
process[i] = process[j];
process[j] = temp;
}
else
{
break;
}
}
}
}
}
printf("\nProcess[%d]:\tTime:\t%d To %d\n", process[y].process_id, time, time + process[y].burst_time);
time = time + process[y].burst_time;
for(i = y; i < limit; i++)
{
if(process[i].ready == 1)
{
process[i].ready = 0;
}
}
}
return 0;
}