Skip to content

Commit 5b747c7

Browse files
committed
Add MLFQScheduler.java new algorithm
1 parent 842ff52 commit 5b747c7

File tree

2 files changed

+192
-0
lines changed

2 files changed

+192
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
package com.thealgorithms.scheduling;
2+
3+
import java.util.LinkedList;
4+
import java.util.Queue;
5+
6+
/**
7+
* The Multi-Level Feedback Queue (MLFQ) Scheduler class.
8+
* This class simulates scheduling using multiple queues, where processes move
9+
* between queues depending on their CPU burst behavior.
10+
*/
11+
public class MLFQScheduler {
12+
private Queue<Process>[] queues; // Multi-level feedback queues
13+
private int timeQuantum[]; // Time quantum for each queue level
14+
private int currentTime; // Current time in the system
15+
16+
/**
17+
* Constructor to initialize the MLFQ scheduler with the specified number of
18+
* levels
19+
* and their corresponding time quantums.
20+
*
21+
* @param levels Number of queues (priority levels)
22+
* @param timeQuantums Time quantum for each queue level
23+
*/
24+
public MLFQScheduler(int levels, int[] timeQuantums) {
25+
queues = new LinkedList[levels];
26+
for (int i = 0; i < levels; i++) {
27+
queues[i] = new LinkedList<>();
28+
}
29+
timeQuantum = timeQuantums;
30+
currentTime = 0;
31+
}
32+
33+
/**
34+
* Adds a new process to the highest priority queue (queue 0).
35+
*
36+
* @param p The process to be added to the scheduler
37+
*/
38+
public void addProcess(Process p) {
39+
queues[0].add(p);
40+
}
41+
42+
/**
43+
* Executes the scheduling process by running the processes in all queues,
44+
* promoting or demoting them based on their completion status and behavior.
45+
* The process continues until all queues are empty.
46+
*/
47+
public void run() {
48+
while (!allQueuesEmpty()) {
49+
for (int i = 0; i < queues.length; i++) {
50+
Queue<Process> queue = queues[i];
51+
if (!queue.isEmpty()) {
52+
Process p = queue.poll();
53+
int quantum = timeQuantum[i];
54+
55+
// Execute the process for the minimum of the time quantum or the remaining time
56+
int timeSlice = Math.min(quantum, p.remainingTime);
57+
p.execute(timeSlice);
58+
currentTime += timeSlice; // Update the system's current time
59+
60+
if (p.isFinished()) {
61+
System.out.println("Process " + p.pid + " finished at time " + currentTime);
62+
} else {
63+
if (i < queues.length - 1) {
64+
p.priority++; // Demote the process to the next lower priority queue
65+
queues[i + 1].add(p); // Add to the next queue level
66+
} else {
67+
queues[i].add(p); // Stay in the same queue if it's the last level
68+
}
69+
}
70+
}
71+
}
72+
}
73+
}
74+
75+
/**
76+
* Helper function to check if all the queues are empty (i.e., no process is
77+
* left to execute).
78+
*
79+
* @return true if all queues are empty, otherwise false
80+
*/
81+
private boolean allQueuesEmpty() {
82+
for (Queue<Process> queue : queues) {
83+
if (!queue.isEmpty()) {
84+
return false;
85+
}
86+
}
87+
return true;
88+
}
89+
90+
/**
91+
* Retrieves the current time of the scheduler, which reflects the total time
92+
* elapsed during the execution of all processes.
93+
*
94+
* @return The current time in the system
95+
*/
96+
public int getCurrentTime() {
97+
return currentTime;
98+
}
99+
}
100+
101+
/**
102+
* Represents a process in the Multi-Level Feedback Queue (MLFQ) scheduling
103+
* algorithm.
104+
*/
105+
class Process {
106+
int pid;
107+
int burstTime;
108+
int remainingTime;
109+
int arrivalTime;
110+
int priority;
111+
112+
/**
113+
* Constructor to initialize a new process.
114+
*
115+
* @param pid Process ID
116+
* @param burstTime CPU Burst Time (time required for the process)
117+
* @param arrivalTime Arrival time of the process
118+
*/
119+
public Process(int pid, int burstTime, int arrivalTime) {
120+
this.pid = pid;
121+
this.burstTime = burstTime;
122+
this.remainingTime = burstTime;
123+
this.arrivalTime = arrivalTime;
124+
this.priority = 0;
125+
}
126+
127+
/**
128+
* Executes the process for a given time slice.
129+
*
130+
* @param timeSlice The amount of time the process is executed
131+
*/
132+
public void execute(int timeSlice) {
133+
remainingTime -= timeSlice;
134+
if (remainingTime < 0)
135+
remainingTime = 0;
136+
}
137+
138+
/**
139+
* Checks if the process has finished execution.
140+
*
141+
* @return true if the process is finished, otherwise false
142+
*/
143+
public boolean isFinished() {
144+
return remainingTime == 0;
145+
}
146+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.thealgorithms.scheduling;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
public class MLFQSchedulerTest {
8+
9+
@Test
10+
void testMLFQScheduling() {
11+
// Create MLFQ Scheduler with 3 levels and time quantum for each level
12+
int[] timeQuantums = {4, 8, 12}; // Example of different quantum for each queue
13+
MLFQScheduler scheduler = new MLFQScheduler(3, timeQuantums);
14+
15+
// Add processes to the scheduler
16+
scheduler.addProcess(new Process(1, 10, 0)); // pid=1, burstTime=10, arrivalTime=0
17+
scheduler.addProcess(new Process(2, 15, 0)); // pid=2, burstTime=15, arrivalTime=0
18+
scheduler.addProcess(new Process(3, 25, 0)); // pid=3, burstTime=25, arrivalTime=0
19+
20+
// Run the scheduler
21+
scheduler.run();
22+
23+
// Check current time after all processes are finished
24+
assertEquals(50, scheduler.getCurrentTime());
25+
}
26+
27+
@Test
28+
void testProcessCompletionOrder() {
29+
int[] timeQuantums = { 3, 6, 9 };
30+
MLFQScheduler scheduler = new MLFQScheduler(3, timeQuantums);
31+
32+
Process p1 = new Process(1, 10, 0);
33+
Process p2 = new Process(2, 5, 0);
34+
Process p3 = new Process(3, 20, 0);
35+
36+
scheduler.addProcess(p1);
37+
scheduler.addProcess(p2);
38+
scheduler.addProcess(p3);
39+
40+
scheduler.run();
41+
42+
// After running, current time should match the total burst time for all
43+
// processes
44+
assertEquals(35, scheduler.getCurrentTime());
45+
}
46+
}

0 commit comments

Comments
 (0)