Skip to content

Refactor ProcessDetails and PreemptivePriorityScheduling #5448

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -33,6 +41,10 @@ public int getTurnAroundTimeTime() {
return turnAroundTime;
}

public int getPriority() {
return priority;
}

public void setProcessId(final String processId) {
this.processId = processId;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,50 +1,42 @@
package com.thealgorithms.scheduling;

import com.thealgorithms.devutils.entities.ProcessDetails;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.PriorityQueue;

/**
* Preemptive Priority Scheduling Algorithm
*
* @author [Bama Charan Chhandogi](https://www.github.com/BamaCharanChhandogi)
*/
public class PreemptivePriorityScheduling {
protected final List<ProcessDetails> processes;
protected final List<String> 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<ProcessDetails> processes) {
this.processes = new ArrayList<>(processes);
this.ganttChart = new ArrayList<>();
}
}

public final class PreemptivePriorityScheduling {
private PreemptivePriorityScheduling() {
}
public static List<String> preemptivePriorityScheduling(List<Process> processes) {
List<String> ganttChart = new ArrayList<>();
PriorityQueue<Process> readyQueue = new PriorityQueue<>(Comparator.comparingInt(p -> - p.priority));
public void scheduleProcesses() {
PriorityQueue<ProcessDetails> readyQueue = new PriorityQueue<>(Comparator.comparingInt(ProcessDetails::getPriority).reversed().thenComparingInt(ProcessDetails::getArrivalTime));

int currentTime = 0;
List<ProcessDetails> arrivedProcesses = new ArrayList<>();

while (!processes.isEmpty() || !readyQueue.isEmpty()) {
while (!processes.isEmpty() && processes.get(0).arrivalTime <= currentTime) {
readyQueue.add(processes.remove(0));
}
updateArrivedProcesses(currentTime, arrivedProcesses);
readyQueue.addAll(arrivedProcesses);
arrivedProcesses.clear();

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 {
Expand All @@ -53,7 +45,15 @@ public static List<String> preemptivePriorityScheduling(List<Process> processes)

currentTime++;
}
}

return ganttChart;
private void updateArrivedProcesses(int currentTime, List<ProcessDetails> arrivedProcesses) {
processes.removeIf(process -> {
if (process.getArrivalTime() <= currentTime) {
arrivedProcesses.add(process);
return true;
}
return false;
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,29 @@

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.ArrayList;
import java.util.Arrays;
import com.thealgorithms.devutils.entities.ProcessDetails;
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
*
* @author [Bama Charan Chhandogi](https://www.github.com/BamaCharanChhandogi)
*/
class PreemptivePrioritySchedulingTest {
@ParameterizedTest
@MethodSource("provideProcessesAndExpectedSchedules")
void testPreemptivePriorityScheduling(List<ProcessDetails> processes, List<String> expectedSchedule) {
PreemptivePriorityScheduling scheduler = new PreemptivePriorityScheduling(processes);
scheduler.scheduleProcesses();
assertEquals(expectedSchedule, scheduler.ganttChart);
}

@Test
void testPreemptivePriorityScheduling() {
// Arrange
List<Process> 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<String> expectedGanttChart = Arrays.asList("P1", "P2", "P3", "P3", "P4", "P2", "P2", "P2", "P1", "P1", "P1", "P1");

// Act
List<String> actualGanttChart = PreemptivePriorityScheduling.preemptivePriorityScheduling(processes);

// Assert
assertEquals(expectedGanttChart, actualGanttChart);
static Stream<Arguments> 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")));
}
}