-
Notifications
You must be signed in to change notification settings - Fork 1
/
mlfq.js
184 lines (151 loc) · 5.32 KB
/
mlfq.js
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
const cases = require("./RandomCases.js");
exports.schedule = async (cases) => {
const queues = [[], [], []];
const finalScheduled = [];
// Assign cases to different queues based on priority ranges
for (const currentCase of cases) {
if (currentCase.severity >= 7 && currentCase.severity <= 10) {
this.queues[0].push(currentCase); // Queue 1 for urgent cases (7-10)
} else if (currentCase.score >= 4 && currentCase.score <= 6) {
this.queues[1].push(currentCase); // Queue 2 for medium priority cases (4-6)
} else if (currentCase.score >= 1 && currentCase.score <= 3) {
this.queues[2].push(currentCase); // Queue 3 for low-priority cases (1-3)
}
}
// function to sort each queue
// loop on each queues
for (let i = 0; i < this.queues.length; i++) {
// const priorityLabel = this.getPriorityLabel(i);
// console.log(`${priorityLabel} cases:`);
// Sort cases within each queue based on the score (higher scores first)
const sortedCases = this.queues[i].sort((a, b) => b.score - a.score);
// for (const currentCase of sortedCases) {
// console.log(` Case ${currentCase.name} (Score ${currentCase.score})`);
// }
}
let counter = cases.length;
let urgentCount = 3;
let mediumCount = 2;
let lowCount = 1;
while (counter > 0) {
// Process urgent queue
while (urgentCount > 0 && queues[0].length > 0) {
finalScheduled.push(queues[0].shift());
counter--;
urgentCount--;
}
// Process medium queue
while (mediumCount > 0 && queues[1].length > 0) {
finalScheduled.push(queues[1].shift());
counter--;
mediumCount--;
}
// Process low priority queue
while (lowCount > 0 && queues[2].length > 0) {
finalScheduled.push(queues[2].shift());
counter--;
lowCount--;
}
// Reset counts for the next iteration
urgentCount = 3;
mediumCount = 2;
lowCount = 1;
}
console.log("finalScheduled", finalScheduled);
return finalScheduled;
};
class MLFQ {
constructor(queues) {
this.queues = queues;
}
schedule(cases) {
const finalScheduled = [];
// Assign cases to different queues based on priority ranges
for (const currentCase of cases) {
if (currentCase.score >= 7 && currentCase.score <= 10) {
this.queues[0].push(currentCase); // Queue 1 for urgent cases (7-10)
} else if (currentCase.score >= 4 && currentCase.score <= 6) {
this.queues[1].push(currentCase); // Queue 2 for medium priority cases (4-6)
} else if (currentCase.score >= 1 && currentCase.score <= 3) {
this.queues[2].push(currentCase); // Queue 3 for low-priority cases (1-3)
}
}
// function to sort each queue
// loop on each queues
for (let i = 0; i < this.queues.length; i++) {
const priorityLabel = this.getPriorityLabel(i);
// console.log(`${priorityLabel} cases:`);
// Sort cases within each queue based on the score (higher scores first)
const sortedCases = this.queues[i].sort((a, b) => b.score - a.score);
// for (const currentCase of sortedCases) {
// console.log(` Case ${currentCase.name} (Score ${currentCase.score})`);
// }
}
// traverse on queues
// if queue is not empty and it is urgent queue then take 3 and put it in scheduled
// else (empty) then ignore
// then take 2 from medium same
// then take 1 from low priority
let counter = cases.length;
let urgentCount = 3;
let mediumCount = 2;
let lowCount = 1;
while (counter > 0) {
// Process urgent queue
while (urgentCount > 0 && queues[0].length > 0) {
finalScheduled.push(queues[0].shift());
counter--;
urgentCount--;
}
// Process medium queue
while (mediumCount > 0 && queues[1].length > 0) {
finalScheduled.push(queues[1].shift());
counter--;
mediumCount--;
}
// Process low priority queue
while (lowCount > 0 && queues[2].length > 0) {
finalScheduled.push(queues[2].shift());
counter--;
lowCount--;
}
// Reset counts for the next iteration
urgentCount = 3;
mediumCount = 2;
lowCount = 1;
}
console.log("finalScheduled", finalScheduled);
// for (let i = 0; i < this.queues.length; i++) {
// const priorityLabel = this.getPriorityLabel(i);
// console.log(`${priorityLabel} cases:`);
// // Sort cases within each queue based on the score (higher scores first)
// const sortedCases = this.queues[i].sort((a, b) => b.score - a.score);
// for (const currentCase of sortedCases) {
// console.log(` Case ${currentCase.name} (Score ${currentCase.score})`);
// }
// }
return finalScheduled;
}
getPriorityLabel(queueIndex) {
// Get the priority label for a given queue index
switch (queueIndex) {
case 0:
return "Urgent";
case 1:
return "Medium Priority";
case 2:
return "Low Priority";
default:
return `Queue ${queueIndex + 1}`;
}
}
}
// Example usage with 10 cases
// queue "Urgent queue, Medium Queue and Low Queue"
const queues = [[], [], []]; // Three queues with different priorities
const mlfq = new MLFQ(queues);
const temp1 = async () => {
await mlfq.schedule(cases); // Add the 'await' keyword here
};
temp1();
module.exports = temp1;