|
| 1 | +package utils; |
| 2 | + |
| 3 | +import java.util.*; |
| 4 | +import java.util.concurrent.TimeUnit; |
| 5 | + |
| 6 | +public abstract class Scheduler { |
| 7 | + protected List<Process> processes; |
| 8 | + protected Queue<Process> readyQueue = new LinkedList<>(); |
| 9 | + protected Process currentProcess = null; |
| 10 | + protected int currentTime = -1; |
| 11 | + |
| 12 | + // Main scheduling method to be called by all subclasses |
| 13 | + public void schedule(List<Process> processes) { |
| 14 | + this.processes = processes; |
| 15 | + this.readyQueue = initializeQueue(); // Let subclasses decide the queue type |
| 16 | + |
| 17 | + while (!processes.isEmpty() || !readyQueue.isEmpty() || currentProcess != null) { |
| 18 | + currentTime++; |
| 19 | + |
| 20 | + // Add newly arrived processes to the ready queue |
| 21 | + Iterator<Process> processIterator = processes.iterator(); |
| 22 | + while (processIterator.hasNext()) { |
| 23 | + Process p = processIterator.next(); |
| 24 | + if (p.arrivalTime == currentTime) { |
| 25 | + readyQueue.add(p); |
| 26 | + System.out.println(currentTime + ": New process in queue: " + p); |
| 27 | + processIterator.remove(); // Remove from original list once added to the queue |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + // Select the next process to run based on the specific scheduler logic |
| 32 | + if (shouldSwitchProcess()) { |
| 33 | + if (currentProcess != null && currentProcess.cpuBurst > 0) { |
| 34 | + readyQueue.add(currentProcess); // Add the current process back if not finished |
| 35 | + } |
| 36 | + currentProcess = selectNextProcess(); // Let subclasses select the next process |
| 37 | + resetQuantumOrCounters(); |
| 38 | + } |
| 39 | + |
| 40 | + // Execute the selected process |
| 41 | + if (currentProcess != null) { |
| 42 | + executeBurst(currentProcess); |
| 43 | + System.out.printf("(%d) Executing %s\n", currentTime, currentProcess); |
| 44 | + } else { |
| 45 | + System.out.printf("(%d) No process is running\n", currentTime); |
| 46 | + } |
| 47 | + |
| 48 | + waitOneSecond(); // Simulate the passage of time |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + protected abstract Process selectNextProcess(); |
| 53 | + |
| 54 | + protected abstract boolean shouldSwitchProcess(); |
| 55 | + |
| 56 | + protected void resetQuantumOrCounters() {} |
| 57 | + |
| 58 | + protected void executeBurst(Process process) { |
| 59 | + if (process != null) { |
| 60 | + process.cpuBurst--; |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + protected Queue<Process> initializeQueue() { |
| 65 | + return new LinkedList<>(); |
| 66 | + } |
| 67 | + |
| 68 | + private void waitOneSecond() { |
| 69 | + try { |
| 70 | + TimeUnit.SECONDS.sleep(1); |
| 71 | + } catch (InterruptedException e) { |
| 72 | + e.printStackTrace(); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + public void test1() { |
| 77 | + List<Process> p = new ArrayList<>(); |
| 78 | + p.add(new Process("P1", 0, 20)); |
| 79 | + p.add(new Process("P2", 4, 15)); |
| 80 | + p.add(new Process("P3", 8, 9)); |
| 81 | + schedule(p); |
| 82 | + } |
| 83 | + |
| 84 | + public void test2() { |
| 85 | + List<Process> p = new ArrayList<>(); |
| 86 | + p.add(new Process("P1", 0, 17)); |
| 87 | + p.add(new Process("P2", 3, 14)); |
| 88 | + p.add(new Process("P3", 4, 10)); |
| 89 | + p.add(new Process("P4", 9, 4)); |
| 90 | + p.add(new Process("P5", 11, 12)); |
| 91 | + schedule(p); |
| 92 | + } |
| 93 | +} |
0 commit comments