-
Notifications
You must be signed in to change notification settings - Fork 2
/
MLFQ.cpp
191 lines (174 loc) · 7.1 KB
/
MLFQ.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
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
#include "scheduler.h"
void changeThePriorty(int n, process processes[])
{
for (int i = 0; i < n; i++)
{
if (processes[i].state == 2)
continue;
processes[i].priority = 0;
processes[i].qunta = 0;
}
}
void MLFQ(int n, process processes[])
{
int time_to_moveup = 10, finised_processes = 0, MAX_Q = 4, current_queue = 0, WillGoToTheQ[4], processor[4], p, i = 0, time_slice = 10;
queue<int> my_queue[MAX_Q];
memset(processor, -1, sizeof(processor));
memset(WillGoToTheQ, -1, sizeof(WillGoToTheQ));
for (int t = 0;; ++t)
{
// push in queue & CPU
int idx_p = 0;
for (idx_p = 0; idx_p < 4 && i < n && processes[i].arrive_time <= t; ++idx_p)
{
if (processor[idx_p] == -1)
{
processor[idx_p] = i;
processes[i].current_brust_time = processes[i].phases[processes[i].phase_idx].first;
processes[i].state = processes[i].phases[processes[i].phase_idx].second;
processes[i].last_processor = idx_p;
processes[i].priority = 0;
i++;
}
}
while (i < n && processes[i].arrive_time <= t)
{
processes[i].priority = 0;
processes[i].current_brust_time = processes[i].phases[processes[i].phase_idx].first;
processes[i].state = processes[i].phases[processes[i].phase_idx].second;
bool flag = false;
for (idx_p = 0; idx_p < 4; idx_p++)
{
if (processor[idx_p] == -1)
continue;
if (processes[processor[idx_p]].priority > 0)
{
my_queue[processes[processor[idx_p]].priority].push(processor[idx_p]);
processor[idx_p] = i;
flag = true;
break;
}
}
if (flag == false)
my_queue[0].push(i);
i++;
}
// names in the processor in each unit of time
/*
cout << t << el;
for (idx_p = 0; idx_p < 4; idx_p++)
{
if (processor[idx_p] == -1)
continue;
cout << processes[processor[idx_p]].priority << " " << processor[idx_p] << " " << processes[processor[idx_p]].process_name << " " << processes[processor[idx_p]].qunta << " " << processes[processor[idx_p]].time_consumed << " " << (1 << (processes[p].priority + 1)) << el;
}
*/
// dowork in processor
for (idx_p = 0; idx_p < 4; ++idx_p)
{
// idle
if (processor[idx_p] == -1)
{
output[t][idx_p] = "i";
continue;
}
// do the work for one unit of time
p = processor[idx_p];
output[t][idx_p] = processes[p].process_name;
processes[p].qunta++;
processes[p].time_consumed++;
// check to remove the process from the processor
if (processes[p].qunta % (1 << (processes[p].priority + 1)) == 0 || processes[p].time_consumed == processes[p].current_brust_time) // remove from processor
{
if (processes[p].qunta % (1 << (processes[p].priority + 1)) == 0)
{
// move to the next queue level
processes[p].priority++;
processes[p].qunta = 0;
}
if (processes[p].time_consumed == processes[p].current_brust_time) // removed after finishing EXE
{
// move to the next phase
processes[p].phase_idx++;
processes[p].time_consumed = 0;
if (processes[p].phase_idx < processes[p].n_phases)
{
processes[p].state = processes[p].phases[processes[p].phase_idx].second;
processes[p].current_brust_time = processes[p].phases[processes[p].phase_idx].first;
}
else // the process finished
{
finised_processes++;
processes[p].state = 2;
processes[p].complete_time = t;
processes[p].turn_around_time = t - processes[p].arrive_time;
}
}
else // removed after time slice
{
my_queue[processes[p].priority].push(p);
}
processor[idx_p] = -1;
}
}
// add process for the processor
current_queue = 0;
for (idx_p = 0; idx_p < 4; ++idx_p) // add for the idle processors
{
if (processor[idx_p] != -1)
continue;
while (current_queue < MAX_Q && my_queue[current_queue].empty())
{
current_queue++;
}
if (current_queue < MAX_Q && !my_queue[current_queue].empty())
{
WillGoToTheQ[idx_p] = my_queue[current_queue].front();
my_queue[current_queue].pop();
}
}
while (1) // add for the processors with lower priorites
{
bool flag = false;
while (current_queue < MAX_Q && my_queue[current_queue].empty())
{
current_queue++;
}
if (current_queue < MAX_Q && !my_queue[current_queue].empty())
{
// cout << processes[my_queue[current_queue].front()].process_name << el;
for (idx_p = 0; idx_p < 4; idx_p++)
{
if (processor[idx_p] == -1)
continue;
// cout << processes[processor[idx_p]].priority << " " << processor[idx_p] << " " << processes[processor[idx_p]].process_name << " - " << current_queue << " " << my_queue[current_queue].front() << el;
if (processes[processor[idx_p]].priority > current_queue)
{
my_queue[processes[processor[idx_p]].priority].push(processor[idx_p]);
processor[idx_p] = -1;
WillGoToTheQ[idx_p] = my_queue[current_queue].front();
my_queue[current_queue].pop();
flag = true;
break;
}
}
}
if (!flag)
break;
}
// Match each process with its preferable queue
match_prefrences(processor, WillGoToTheQ, processes);
// Do IO
IO_handler_MLFQ(i, processes, my_queue, finised_processes, t);
// THE FUNCTION FINISHED SUCCESFULLY
if (finised_processes == n)
{
print(t, processes, n);
return;
}
if (t != 0 && t % time_to_moveup == 0)
{
changeThePriorty(i, processes);
}
}
}