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