From 7e437749e4d4a64ea43845b109f5c10ebbacfe62 Mon Sep 17 00:00:00 2001 From: doxxx93 Date: Wed, 11 Sep 2024 07:50:57 +0900 Subject: [PATCH 1/4] Refactor ProcessDetails and PreemptivePriorityScheduling for consistency --- .../devutils/entities/ProcessDetails.java | 12 +++++ .../PreemptivePriorityScheduling.java | 51 +++++++++---------- .../PreemptivePrioritySchedulingTest.java | 47 ++++++++++++----- 3 files changed, 68 insertions(+), 42 deletions(-) diff --git a/src/main/java/com/thealgorithms/devutils/entities/ProcessDetails.java b/src/main/java/com/thealgorithms/devutils/entities/ProcessDetails.java index 388fb3fd9056..b8b863eda3fc 100644 --- a/src/main/java/com/thealgorithms/devutils/entities/ProcessDetails.java +++ b/src/main/java/com/thealgorithms/devutils/entities/ProcessDetails.java @@ -6,6 +6,14 @@ public class ProcessDetails { private int burstTime; private int waitingTime; private int turnAroundTime; + private int priority; + + public ProcessDetails(final String processId, final int arrivalTime, final int burstTime, int priority) { + this.processId = processId; + this.arrivalTime = arrivalTime; + this.burstTime = burstTime; + this.priority = priority; + } public ProcessDetails(final String processId, final int arrivalTime, final int burstTime) { this.processId = processId; @@ -33,6 +41,10 @@ public int getTurnAroundTimeTime() { return turnAroundTime; } + public int getPriority() { + return priority; + } + public void setProcessId(final String processId) { this.processId = processId; } diff --git a/src/main/java/com/thealgorithms/scheduling/PreemptivePriorityScheduling.java b/src/main/java/com/thealgorithms/scheduling/PreemptivePriorityScheduling.java index 9bcd5df81056..7380d2e9b5db 100644 --- a/src/main/java/com/thealgorithms/scheduling/PreemptivePriorityScheduling.java +++ b/src/main/java/com/thealgorithms/scheduling/PreemptivePriorityScheduling.java @@ -1,5 +1,7 @@ package com.thealgorithms.scheduling; +import com.thealgorithms.devutils.entities.ProcessDetails; + import java.util.ArrayList; import java.util.Comparator; import java.util.List; @@ -7,44 +9,39 @@ /** * Preemptive Priority Scheduling Algorithm + * * @author [Bama Charan Chhandogi](https://www.github.com/BamaCharanChhandogi) */ +public class PreemptivePriorityScheduling { + protected final List processes; + protected final List ganttChart; -class Process { - String name; - int arrivalTime; - int burstTime; - int priority; - - Process(String name, int arrivalTime, int burstTime, int priority) { - this.name = name; - this.arrivalTime = arrivalTime; - this.burstTime = burstTime; - this.priority = priority; + public PreemptivePriorityScheduling(List processes) { + this.processes = new ArrayList<>(processes); + this.ganttChart = new ArrayList<>(); } -} -public final class PreemptivePriorityScheduling { - private PreemptivePriorityScheduling() { - } - public static List preemptivePriorityScheduling(List processes) { - List ganttChart = new ArrayList<>(); - PriorityQueue readyQueue = new PriorityQueue<>(Comparator.comparingInt(p -> - p.priority)); + public void scheduleProcesses() { + PriorityQueue readyQueue = new PriorityQueue<>( + Comparator.comparingInt(ProcessDetails::getPriority).reversed() + .thenComparingInt(ProcessDetails::getArrivalTime) + ); int currentTime = 0; + int processIndex = 0; - while (!processes.isEmpty() || !readyQueue.isEmpty()) { - while (!processes.isEmpty() && processes.get(0).arrivalTime <= currentTime) { - readyQueue.add(processes.remove(0)); + while (processIndex < processes.size() || !readyQueue.isEmpty()) { + while (processIndex < processes.size() && processes.get(processIndex).getArrivalTime() <= currentTime) { + readyQueue.add(processes.get(processIndex)); + processIndex++; } if (!readyQueue.isEmpty()) { - Process currentProcess = readyQueue.poll(); + ProcessDetails currentProcess = readyQueue.poll(); + ganttChart.add(currentProcess.getProcessId()); + currentProcess.setBurstTime(currentProcess.getBurstTime() - 1); - ganttChart.add(currentProcess.name); - currentProcess.burstTime--; - - if (currentProcess.burstTime > 0) { + if (currentProcess.getBurstTime() > 0) { readyQueue.add(currentProcess); } } else { @@ -53,7 +50,5 @@ public static List preemptivePriorityScheduling(List processes) currentTime++; } - - return ganttChart; } } diff --git a/src/test/java/com/thealgorithms/scheduling/PreemptivePrioritySchedulingTest.java b/src/test/java/com/thealgorithms/scheduling/PreemptivePrioritySchedulingTest.java index 1c8a25dfda49..5e5dd7bf889b 100644 --- a/src/test/java/com/thealgorithms/scheduling/PreemptivePrioritySchedulingTest.java +++ b/src/test/java/com/thealgorithms/scheduling/PreemptivePrioritySchedulingTest.java @@ -1,33 +1,52 @@ package com.thealgorithms.scheduling; -import static org.junit.jupiter.api.Assertions.assertEquals; +import com.thealgorithms.devutils.entities.ProcessDetails; +import org.junit.jupiter.api.Test; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; -import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; /** * Test Cases of Preemptive Priority Scheduling Algorithm + * * @author [Bama Charan Chhandogi](https://www.github.com/BamaCharanChhandogi) */ class PreemptivePrioritySchedulingTest { @Test void testPreemptivePriorityScheduling() { - // Arrange - List processes = new ArrayList<>(); - processes.add(new Process("P1", 0, 5, 10)); - processes.add(new Process("P2", 1, 4, 20)); - processes.add(new Process("P3", 2, 2, 30)); - processes.add(new Process("P4", 4, 1, 40)); + List processes = new ArrayList<>(); + processes.add(new ProcessDetails("P1", 0, 5, 2)); + processes.add(new ProcessDetails("P2", 1, 4, 4)); + processes.add(new ProcessDetails("P3", 2, 2, 6)); + processes.add(new ProcessDetails("P4", 4, 1, 8)); + + PreemptivePriorityScheduling scheduler = new PreemptivePriorityScheduling(processes); + scheduler.scheduleProcesses(); + + List expectedSchedule = List.of( + "P1", "P2", "P3", "P3", "P4", "P2", "P2", "P2", "P1", "P1", "P1", "P1" + ); + + assertEquals(expectedSchedule, scheduler.ganttChart); + } + + @Test + void testPreemptivePrioritySchedulingWithIdleTime() { + List processes = new ArrayList<>(); + processes.add(new ProcessDetails("P1", 2, 5, 3)); + processes.add(new ProcessDetails("P2", 5, 3, 5)); + processes.add(new ProcessDetails("P3", 7, 1, 9)); - List expectedGanttChart = Arrays.asList("P1", "P2", "P3", "P3", "P4", "P2", "P2", "P2", "P1", "P1", "P1", "P1"); + PreemptivePriorityScheduling scheduler = new PreemptivePriorityScheduling(processes); + scheduler.scheduleProcesses(); - // Act - List actualGanttChart = PreemptivePriorityScheduling.preemptivePriorityScheduling(processes); + List expectedSchedule = List.of( + "Idle", "Idle", "P1", "P1", "P1", "P2", "P2", "P3", "P2", "P1", "P1" + ); - // Assert - assertEquals(expectedGanttChart, actualGanttChart); + assertEquals(expectedSchedule, scheduler.ganttChart); } } From 663135007b828a42d74f11b7bcf58c18fb3c1fb9 Mon Sep 17 00:00:00 2001 From: doxxx93 Date: Wed, 11 Sep 2024 08:02:08 +0900 Subject: [PATCH 2/4] fix formatting --- .../scheduling/PreemptivePriorityScheduling.java | 6 +----- .../PreemptivePrioritySchedulingTest.java | 15 +++++---------- 2 files changed, 6 insertions(+), 15 deletions(-) diff --git a/src/main/java/com/thealgorithms/scheduling/PreemptivePriorityScheduling.java b/src/main/java/com/thealgorithms/scheduling/PreemptivePriorityScheduling.java index 7380d2e9b5db..43abbb03226c 100644 --- a/src/main/java/com/thealgorithms/scheduling/PreemptivePriorityScheduling.java +++ b/src/main/java/com/thealgorithms/scheduling/PreemptivePriorityScheduling.java @@ -1,7 +1,6 @@ package com.thealgorithms.scheduling; import com.thealgorithms.devutils.entities.ProcessDetails; - import java.util.ArrayList; import java.util.Comparator; import java.util.List; @@ -22,10 +21,7 @@ public PreemptivePriorityScheduling(List processes) { } public void scheduleProcesses() { - PriorityQueue readyQueue = new PriorityQueue<>( - Comparator.comparingInt(ProcessDetails::getPriority).reversed() - .thenComparingInt(ProcessDetails::getArrivalTime) - ); + PriorityQueue readyQueue = new PriorityQueue<>(Comparator.comparingInt(ProcessDetails::getPriority).reversed().thenComparingInt(ProcessDetails::getArrivalTime)); int currentTime = 0; int processIndex = 0; diff --git a/src/test/java/com/thealgorithms/scheduling/PreemptivePrioritySchedulingTest.java b/src/test/java/com/thealgorithms/scheduling/PreemptivePrioritySchedulingTest.java index 5e5dd7bf889b..69b0d136fed8 100644 --- a/src/test/java/com/thealgorithms/scheduling/PreemptivePrioritySchedulingTest.java +++ b/src/test/java/com/thealgorithms/scheduling/PreemptivePrioritySchedulingTest.java @@ -1,12 +1,11 @@ package com.thealgorithms.scheduling; -import com.thealgorithms.devutils.entities.ProcessDetails; -import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; +import com.thealgorithms.devutils.entities.ProcessDetails; import java.util.ArrayList; import java.util.List; - -import static org.junit.jupiter.api.Assertions.assertEquals; +import org.junit.jupiter.api.Test; /** * Test Cases of Preemptive Priority Scheduling Algorithm @@ -26,9 +25,7 @@ void testPreemptivePriorityScheduling() { PreemptivePriorityScheduling scheduler = new PreemptivePriorityScheduling(processes); scheduler.scheduleProcesses(); - List expectedSchedule = List.of( - "P1", "P2", "P3", "P3", "P4", "P2", "P2", "P2", "P1", "P1", "P1", "P1" - ); + List expectedSchedule = List.of("P1", "P2", "P3", "P3", "P4", "P2", "P2", "P2", "P1", "P1", "P1", "P1"); assertEquals(expectedSchedule, scheduler.ganttChart); } @@ -43,9 +40,7 @@ void testPreemptivePrioritySchedulingWithIdleTime() { PreemptivePriorityScheduling scheduler = new PreemptivePriorityScheduling(processes); scheduler.scheduleProcesses(); - List expectedSchedule = List.of( - "Idle", "Idle", "P1", "P1", "P1", "P2", "P2", "P3", "P2", "P1", "P1" - ); + List expectedSchedule = List.of("Idle", "Idle", "P1", "P1", "P1", "P2", "P2", "P3", "P2", "P1", "P1"); assertEquals(expectedSchedule, scheduler.ganttChart); } From d21b09bb8d536ed9ee634f23c4950a7ae715d837 Mon Sep 17 00:00:00 2001 From: doxxx93 Date: Wed, 11 Sep 2024 08:25:05 +0900 Subject: [PATCH 3/4] fix formatting --- .../PreemptivePriorityScheduling.java | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/thealgorithms/scheduling/PreemptivePriorityScheduling.java b/src/main/java/com/thealgorithms/scheduling/PreemptivePriorityScheduling.java index 43abbb03226c..27d85a94d6f5 100644 --- a/src/main/java/com/thealgorithms/scheduling/PreemptivePriorityScheduling.java +++ b/src/main/java/com/thealgorithms/scheduling/PreemptivePriorityScheduling.java @@ -24,13 +24,12 @@ public void scheduleProcesses() { PriorityQueue readyQueue = new PriorityQueue<>(Comparator.comparingInt(ProcessDetails::getPriority).reversed().thenComparingInt(ProcessDetails::getArrivalTime)); int currentTime = 0; - int processIndex = 0; + List arrivedProcesses = new ArrayList<>(); - while (processIndex < processes.size() || !readyQueue.isEmpty()) { - while (processIndex < processes.size() && processes.get(processIndex).getArrivalTime() <= currentTime) { - readyQueue.add(processes.get(processIndex)); - processIndex++; - } + while (!processes.isEmpty() || !readyQueue.isEmpty()) { + updateArrivedProcesses(currentTime, arrivedProcesses); + readyQueue.addAll(arrivedProcesses); + arrivedProcesses.clear(); if (!readyQueue.isEmpty()) { ProcessDetails currentProcess = readyQueue.poll(); @@ -47,4 +46,14 @@ public void scheduleProcesses() { currentTime++; } } + + private void updateArrivedProcesses(int currentTime, List arrivedProcesses) { + processes.removeIf(process -> { + if (process.getArrivalTime() <= currentTime) { + arrivedProcesses.add(process); + return true; + } + return false; + }); + } } From d3d3bc8e09366c61027fb3887c307673cdd08035 Mon Sep 17 00:00:00 2001 From: doxxx93 Date: Wed, 11 Sep 2024 21:29:23 +0900 Subject: [PATCH 4/4] Improve test readability and maintainability --- .../PreemptivePrioritySchedulingTest.java | 37 +++++-------------- 1 file changed, 10 insertions(+), 27 deletions(-) diff --git a/src/test/java/com/thealgorithms/scheduling/PreemptivePrioritySchedulingTest.java b/src/test/java/com/thealgorithms/scheduling/PreemptivePrioritySchedulingTest.java index 69b0d136fed8..d0005dda9097 100644 --- a/src/test/java/com/thealgorithms/scheduling/PreemptivePrioritySchedulingTest.java +++ b/src/test/java/com/thealgorithms/scheduling/PreemptivePrioritySchedulingTest.java @@ -3,9 +3,11 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import com.thealgorithms.devutils.entities.ProcessDetails; -import java.util.ArrayList; import java.util.List; -import org.junit.jupiter.api.Test; +import java.util.stream.Stream; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; /** * Test Cases of Preemptive Priority Scheduling Algorithm @@ -13,35 +15,16 @@ * @author [Bama Charan Chhandogi](https://www.github.com/BamaCharanChhandogi) */ class PreemptivePrioritySchedulingTest { - - @Test - void testPreemptivePriorityScheduling() { - List processes = new ArrayList<>(); - processes.add(new ProcessDetails("P1", 0, 5, 2)); - processes.add(new ProcessDetails("P2", 1, 4, 4)); - processes.add(new ProcessDetails("P3", 2, 2, 6)); - processes.add(new ProcessDetails("P4", 4, 1, 8)); - + @ParameterizedTest + @MethodSource("provideProcessesAndExpectedSchedules") + void testPreemptivePriorityScheduling(List processes, List expectedSchedule) { PreemptivePriorityScheduling scheduler = new PreemptivePriorityScheduling(processes); scheduler.scheduleProcesses(); - - List expectedSchedule = List.of("P1", "P2", "P3", "P3", "P4", "P2", "P2", "P2", "P1", "P1", "P1", "P1"); - assertEquals(expectedSchedule, scheduler.ganttChart); } - @Test - void testPreemptivePrioritySchedulingWithIdleTime() { - List processes = new ArrayList<>(); - processes.add(new ProcessDetails("P1", 2, 5, 3)); - processes.add(new ProcessDetails("P2", 5, 3, 5)); - processes.add(new ProcessDetails("P3", 7, 1, 9)); - - PreemptivePriorityScheduling scheduler = new PreemptivePriorityScheduling(processes); - scheduler.scheduleProcesses(); - - List expectedSchedule = List.of("Idle", "Idle", "P1", "P1", "P1", "P2", "P2", "P3", "P2", "P1", "P1"); - - assertEquals(expectedSchedule, scheduler.ganttChart); + static Stream provideProcessesAndExpectedSchedules() { + return Stream.of(Arguments.of(List.of(new ProcessDetails("P1", 0, 5, 2), new ProcessDetails("P2", 1, 4, 4), new ProcessDetails("P3", 2, 2, 6), new ProcessDetails("P4", 4, 1, 8)), List.of("P1", "P2", "P3", "P3", "P4", "P2", "P2", "P2", "P1", "P1", "P1", "P1")), + Arguments.of(List.of(new ProcessDetails("P1", 2, 5, 3), new ProcessDetails("P2", 5, 3, 5), new ProcessDetails("P3", 7, 1, 9)), List.of("Idle", "Idle", "P1", "P1", "P1", "P2", "P2", "P3", "P2", "P1", "P1"))); } }