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 3 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,46 @@

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

import com.thealgorithms.devutils.entities.ProcessDetails;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.junit.jupiter.api.Test;

/**
* Test Cases of Preemptive Priority Scheduling Algorithm
*
* @author [Bama Charan Chhandogi](https://www.github.com/BamaCharanChhandogi)
*/
class PreemptivePrioritySchedulingTest {

@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<ProcessDetails> 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));

List<String> expectedGanttChart = Arrays.asList("P1", "P2", "P3", "P3", "P4", "P2", "P2", "P2", "P1", "P1", "P1", "P1");
PreemptivePriorityScheduling scheduler = new PreemptivePriorityScheduling(processes);
scheduler.scheduleProcesses();

// Act
List<String> actualGanttChart = PreemptivePriorityScheduling.preemptivePriorityScheduling(processes);
List<String> expectedSchedule = List.of("P1", "P2", "P3", "P3", "P4", "P2", "P2", "P2", "P1", "P1", "P1", "P1");

// Assert
assertEquals(expectedGanttChart, actualGanttChart);
assertEquals(expectedSchedule, scheduler.ganttChart);
}

@Test
void testPreemptivePrioritySchedulingWithIdleTime() {
List<ProcessDetails> 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<String> expectedSchedule = List.of("Idle", "Idle", "P1", "P1", "P1", "P2", "P2", "P3", "P2", "P1", "P1");

assertEquals(expectedSchedule, scheduler.ganttChart);
}
}