From c8252eae2e5be211eeef74b9466561ef3fcd750c Mon Sep 17 00:00:00 2001 From: Chaitanya Shah Date: Tue, 7 Oct 2025 21:55:33 +0530 Subject: [PATCH 1/4] Add Preemptive & Non-Preemptive Scheduling Algorithm --- .../Preemptive&NonPreemptiveScheduling.java | 217 ++++++++++++++++++ 1 file changed, 217 insertions(+) create mode 100644 src/main/java/com/thealgorithms/scheduling/Preemptive&NonPreemptiveScheduling.java diff --git a/src/main/java/com/thealgorithms/scheduling/Preemptive&NonPreemptiveScheduling.java b/src/main/java/com/thealgorithms/scheduling/Preemptive&NonPreemptiveScheduling.java new file mode 100644 index 000000000000..c4d6a418318f --- /dev/null +++ b/src/main/java/com/thealgorithms/scheduling/Preemptive&NonPreemptiveScheduling.java @@ -0,0 +1,217 @@ +import java.util.*; + +class SchedulingAlgorithms +{ + + static class Process + { + int pid; + int priority; + int arrivalTime; + int burstTime; + int remainingTime; + int completionTime; + int waitingTime; + int turnaroundTime; + int responseTime; + boolean completed; + + Process(int pid, int priority, int arrivalTime, int burstTime) + { + this.pid = pid; + this.priority = priority; + this.arrivalTime = arrivalTime; + this.burstTime = burstTime; + this.remainingTime = burstTime; + this.completed = false; + } + } + + public static void main(String[] args) + { + Scanner sc = new Scanner(System.in); + + System.out.print("Enter number of processes: "); + int n = sc.nextInt(); + + Process[] processes = new Process[n]; + for (int i = 0; i < n; i++) + { + System.out.println("Process " + (i + 1)); + System.out.print("Enter arrival time: "); + int at = sc.nextInt(); + System.out.print("Enter burst time: "); + int bt = sc.nextInt(); + System.out.print("Enter priority (integer): "); + int priority = sc.nextInt(); + processes[i] = new Process(i + 1, priority, at, bt); + } + + System.out.println("Choose the scheduling algorithm:"); + System.out.println("1. Non-Preemptive"); + System.out.println("2. Preemptive"); + System.out.print("Enter your choice (1 or 2): "); + int choice = sc.nextInt(); + + switch (choice) + { + case 1: + nonPreemptive(processes); + break; + case 2: + preemptive(processes); + break; + default: + System.out.println("Invalid choice."); + return; + } + } + + private static void nonPreemptive(Process[] processes) + { + int n = processes.length; + int completed = 0; + int currentTime = 0; + boolean[] isCompleted = new boolean[n]; + + while (completed != n) + { + int idx = -1; + int minBurst = Integer.MAX_VALUE; + for (int i = 0; i < n; i++) + { + if ((processes[i].arrivalTime <= currentTime) && !isCompleted[i]) + { + if (processes[i].burstTime < minBurst) + { + minBurst = processes[i].burstTime; + idx = i; + } + if (processes[i].burstTime == minBurst) + { + if (processes[i].priority < processes[idx].priority) + { + idx = i; + } + } + } + } + + if (idx != -1) + { + processes[idx].waitingTime = currentTime - processes[idx].arrivalTime; + currentTime += processes[idx].burstTime; + processes[idx].completionTime = currentTime; + processes[idx].turnaroundTime = processes[idx].completionTime - processes[idx].arrivalTime; + processes[idx].responseTime = processes[idx].waitingTime; + isCompleted[idx] = true; + completed++; + } + else + { + currentTime++; + } + } + + printResults(processes, "Non-Preemptive SJF (Shortest Job First) Scheduling"); + } + + private static void preemptive(Process[] processes) + { + int n = processes.length; + int completed = 0; + int currentTime = 0; + int minRemainingTime; + int shortest = -1; + boolean check = false; + + int[] waitingTime = new int[n]; + int[] turnaroundTime = new int[n]; + int[] completionTime = new int[n]; + int[] responseTime = new int[n]; + + int[] remainingTime = new int[n]; + for (int i = 0; i < n; i++) + { + remainingTime[i] = processes[i].burstTime; + } + + while (completed != n) + { + minRemainingTime = Integer.MAX_VALUE; + shortest = -1; + check = false; + + for (int i = 0; i < n; i++) + { + if ((processes[i].arrivalTime <= currentTime) && + (remainingTime[i] < minRemainingTime) && ( remainingTime[i] > 0)) + { + minRemainingTime = remainingTime[i]; + shortest = i; + check = true; + } + } + + if (!check) + { + currentTime++; + continue; + } + + if (responseTime[shortest] == 0) + { + responseTime[shortest] = currentTime - processes[shortest].arrivalTime; + } + + remainingTime[shortest]--; + currentTime++; + + if (remainingTime[shortest] == 0) + { + completed++; + completionTime[shortest] = currentTime; + turnaroundTime[shortest] = completionTime[shortest] - processes[shortest].arrivalTime; + waitingTime[shortest] = turnaroundTime[shortest] - processes[shortest].burstTime; + if (waitingTime[shortest] < 0) + { + waitingTime[shortest] = 0; + } + } + } + + for (int i = 0; i < n; i++) + { + processes[i].completionTime = completionTime[i]; + processes[i].turnaroundTime = turnaroundTime[i]; + processes[i].waitingTime = waitingTime[i]; + processes[i].responseTime = responseTime[i]; + } + + printResults(processes, "Preemptive SRTF (Shortest Remaining Time First) Scheduling"); + } + + private static void printResults(Process[] processes, String title) + { + System.out.println("\n" + title); + System.out.printf("%-10s%-10s%-15s%-15s%-17s%-15s%-17s%-15s\n", + "Process", "Priority", "Arrival Time", "Burst Time", + "Completion Time", "Waiting Time", "Turnaround Time", "Response Time\n"); + + double totalWT = 0, totalTAT = 0; + + Arrays.sort(processes, (a, b) -> a.pid - b.pid); + + for (Process p : processes) + { + totalWT += p.waitingTime; + totalTAT += p.turnaroundTime; + System.out.printf("%-10d%-10d%-15d%-15d%-17d%-15d%-17d%-15d\n", + p.pid, p.priority, p.arrivalTime, p.burstTime, + p.completionTime, p.waitingTime, p.turnaroundTime, p.responseTime); + } + + System.out.printf("Average Waiting Time: %.2f\n", totalWT / processes.length); + System.out.printf("Average Turnaround Time: %.2f\n", totalTAT / processes.length); + } +} \ No newline at end of file From 7373d289f07fec8b203e974a1d59fbf3e3a3b70f Mon Sep 17 00:00:00 2001 From: Chaitanya Shah <149926880+ChaitanyaShah26@users.noreply.github.com> Date: Thu, 23 Oct 2025 15:10:10 +0530 Subject: [PATCH 2/4] Update Preemptive&NonPreemptiveScheduling.java --- .../Preemptive&NonPreemptiveScheduling.java | 300 ++++++++---------- 1 file changed, 140 insertions(+), 160 deletions(-) diff --git a/src/main/java/com/thealgorithms/scheduling/Preemptive&NonPreemptiveScheduling.java b/src/main/java/com/thealgorithms/scheduling/Preemptive&NonPreemptiveScheduling.java index c4d6a418318f..c1a6fe48551f 100644 --- a/src/main/java/com/thealgorithms/scheduling/Preemptive&NonPreemptiveScheduling.java +++ b/src/main/java/com/thealgorithms/scheduling/Preemptive&NonPreemptiveScheduling.java @@ -1,217 +1,197 @@ -import java.util.*; - -class SchedulingAlgorithms -{ - - static class Process - { - int pid; - int priority; - int arrivalTime; - int burstTime; - int remainingTime; - int completionTime; - int waitingTime; - int turnaroundTime; - int responseTime; - boolean completed; - - Process(int pid, int priority, int arrivalTime, int burstTime) - { +import java.util.Scanner; +import java.util.Arrays; +import java.util.Comparator; + +class SchedulingAlgorithms { + static class Process { + private int pid; + private int priority; + private int arrivalTime; + private int burstTime; + private int remainingTime; + private int completionTime; + private int waitingTime; + private int turnaroundTime; + private int responseTime; + + Process(int pid, int priority, int arrivalTime, int burstTime) { this.pid = pid; this.priority = priority; this.arrivalTime = arrivalTime; this.burstTime = burstTime; this.remainingTime = burstTime; - this.completed = false; + this.responseTime = -1; } + public int getPid() { return pid; } + public int getPriority() { return priority; } + public int getArrivalTime() { return arrivalTime; } + public int getBurstTime() { return burstTime; } + public int getRemainingTime() { return remainingTime; } + public int getCompletionTime() { return completionTime; } + public int getWaitingTime() { return waitingTime; } + public int getTurnaroundTime() { return turnaroundTime; } + public int getResponseTime() { return responseTime; } + + public void setRemainingTime(int remainingTime) { this.remainingTime = remainingTime; } + public void setCompletionTime(int completionTime) { this.completionTime = completionTime; } + public void setWaitingTime(int waitingTime) { this.waitingTime = waitingTime; } + public void setTurnaroundTime(int turnaroundTime) { this.turnaroundTime = turnaroundTime; } + public void setResponseTime(int responseTime) { this.responseTime = responseTime; } } - public static void main(String[] args) - { - Scanner sc = new Scanner(System.in); - - System.out.print("Enter number of processes: "); - int n = sc.nextInt(); - - Process[] processes = new Process[n]; - for (int i = 0; i < n; i++) - { - System.out.println("Process " + (i + 1)); - System.out.print("Enter arrival time: "); - int at = sc.nextInt(); - System.out.print("Enter burst time: "); - int bt = sc.nextInt(); - System.out.print("Enter priority (integer): "); - int priority = sc.nextInt(); - processes[i] = new Process(i + 1, priority, at, bt); - } + public static void main(String[] args) { + try (Scanner sc = new Scanner(System.in)) { + System.out.print("Enter number of processes: "); + int n = sc.nextInt(); + + Process[] processes = new Process[n]; + for (int i = 0; i < n; i++) { + System.out.println("Process " + (i + 1)); + System.out.print("Enter arrival time: "); + int at = sc.nextInt(); + System.out.print("Enter burst time: "); + int bt = sc.nextInt(); + System.out.print("Enter priority (integer): "); + int priority = sc.nextInt(); + processes[i] = new Process(i + 1, priority, at, bt); + } - System.out.println("Choose the scheduling algorithm:"); - System.out.println("1. Non-Preemptive"); - System.out.println("2. Preemptive"); - System.out.print("Enter your choice (1 or 2): "); - int choice = sc.nextInt(); - - switch (choice) - { - case 1: - nonPreemptive(processes); - break; - case 2: - preemptive(processes); - break; - default: - System.out.println("Invalid choice."); - return; + System.out.println("\nChoose the scheduling algorithm:"); + System.out.println("1. Non-Preemptive (Shortest Job First with Priority)"); + System.out.println("2. Preemptive (Shortest Remaining Time First)"); + System.out.print("Enter your choice (1 or 2): "); + int choice = sc.nextInt(); + + switch (choice) { + case 1: + nonPreemptive(processes); + break; + case 2: + preemptive(processes); + break; + default: + System.out.println("Invalid choice. Exiting."); + return; + } } } - - private static void nonPreemptive(Process[] processes) - { + + private static void nonPreemptive(Process[] processes) { int n = processes.length; - int completed = 0; + int completedProcesses = 0; int currentTime = 0; boolean[] isCompleted = new boolean[n]; - while (completed != n) - { - int idx = -1; - int minBurst = Integer.MAX_VALUE; - for (int i = 0; i < n; i++) - { - if ((processes[i].arrivalTime <= currentTime) && !isCompleted[i]) - { - if (processes[i].burstTime < minBurst) - { - minBurst = processes[i].burstTime; - idx = i; - } - if (processes[i].burstTime == minBurst) - { - if (processes[i].priority < processes[idx].priority) - { - idx = i; + while (completedProcesses < n) { + int selectedProcessIndex = -1; + int minBurstTime = Integer.MAX_VALUE; + int highestPriority = Integer.MAX_VALUE; + + for (int i = 0; i < n; i++) { + if (processes[i].getArrivalTime() <= currentTime && !isCompleted[i]) { + if (processes[i].getBurstTime() < minBurstTime) { + minBurstTime = processes[i].getBurstTime(); + highestPriority = processes[i].getPriority(); + selectedProcessIndex = i; + } + else if (processes[i].getBurstTime() == minBurstTime) { + if (processes[i].getPriority() < highestPriority) { + highestPriority = processes[i].getPriority(); + selectedProcessIndex = i; } } } } - if (idx != -1) - { - processes[idx].waitingTime = currentTime - processes[idx].arrivalTime; - currentTime += processes[idx].burstTime; - processes[idx].completionTime = currentTime; - processes[idx].turnaroundTime = processes[idx].completionTime - processes[idx].arrivalTime; - processes[idx].responseTime = processes[idx].waitingTime; - isCompleted[idx] = true; - completed++; - } - else - { + if (selectedProcessIndex != -1) { + Process currentProcess = processes[selectedProcessIndex]; + + if (currentProcess.getResponseTime() == -1) { + currentProcess.setResponseTime(currentTime - currentProcess.getArrivalTime()); + } + + currentProcess.setWaitingTime(currentTime - currentProcess.getArrivalTime()); + currentTime += currentProcess.getBurstTime(); + currentProcess.setCompletionTime(currentTime); + currentProcess.setTurnaroundTime(currentProcess.getCompletionTime() - currentProcess.getArrivalTime()); + + isCompleted[selectedProcessIndex] = true; + completedProcesses++; + } else { currentTime++; } } - printResults(processes, "Non-Preemptive SJF (Shortest Job First) Scheduling"); + printResults(processes, "Non-Preemptive SJF (with Priority) Scheduling"); } - private static void preemptive(Process[] processes) - { + private static void preemptive(Process[] processes) { int n = processes.length; - int completed = 0; + int completedProcesses = 0; int currentTime = 0; - int minRemainingTime; - int shortest = -1; - boolean check = false; - - int[] waitingTime = new int[n]; - int[] turnaroundTime = new int[n]; - int[] completionTime = new int[n]; - int[] responseTime = new int[n]; - - int[] remainingTime = new int[n]; - for (int i = 0; i < n; i++) - { - remainingTime[i] = processes[i].burstTime; - } - while (completed != n) - { - minRemainingTime = Integer.MAX_VALUE; - shortest = -1; - check = false; - - for (int i = 0; i < n; i++) - { - if ((processes[i].arrivalTime <= currentTime) && - (remainingTime[i] < minRemainingTime) && ( remainingTime[i] > 0)) - { - minRemainingTime = remainingTime[i]; - shortest = i; - check = true; + while (completedProcesses < n) { + int shortestJobIndex = -1; + int minRemainingTime = Integer.MAX_VALUE; + + for (int i = 0; i < n; i++) { + if (processes[i].getArrivalTime() <= currentTime && processes[i].getRemainingTime() > 0) { + if (processes[i].getRemainingTime() < minRemainingTime) { + minRemainingTime = processes[i].getRemainingTime(); + shortestJobIndex = i; + } } } - if (!check) - { + if (shortestJobIndex == -1) { currentTime++; continue; } - if (responseTime[shortest] == 0) - { - responseTime[shortest] = currentTime - processes[shortest].arrivalTime; + Process currentProcess = processes[shortestJobIndex]; + + if (currentProcess.getResponseTime() == -1) { + currentProcess.setResponseTime(currentTime - currentProcess.getArrivalTime()); } - remainingTime[shortest]--; + currentProcess.setRemainingTime(currentProcess.getRemainingTime() - 1); currentTime++; - if (remainingTime[shortest] == 0) - { - completed++; - completionTime[shortest] = currentTime; - turnaroundTime[shortest] = completionTime[shortest] - processes[shortest].arrivalTime; - waitingTime[shortest] = turnaroundTime[shortest] - processes[shortest].burstTime; - if (waitingTime[shortest] < 0) - { - waitingTime[shortest] = 0; + if (currentProcess.getRemainingTime() == 0) { + completedProcesses++; + currentProcess.setCompletionTime(currentTime); + currentProcess.setTurnaroundTime(currentProcess.getCompletionTime() - currentProcess.getArrivalTime()); + currentProcess.setWaitingTime(currentProcess.getTurnaroundTime() - currentProcess.getBurstTime()); + + if (currentProcess.getWaitingTime() < 0) { + currentProcess.setWaitingTime(0); } } } - for (int i = 0; i < n; i++) - { - processes[i].completionTime = completionTime[i]; - processes[i].turnaroundTime = turnaroundTime[i]; - processes[i].waitingTime = waitingTime[i]; - processes[i].responseTime = responseTime[i]; - } - - printResults(processes, "Preemptive SRTF (Shortest Remaining Time First) Scheduling"); + printResults(processes, "Preemptive SRTF Scheduling"); } - private static void printResults(Process[] processes, String title) - { + private static void printResults(Process[] processes, String title) { System.out.println("\n" + title); System.out.printf("%-10s%-10s%-15s%-15s%-17s%-15s%-17s%-15s\n", - "Process", "Priority", "Arrival Time", "Burst Time", - "Completion Time", "Waiting Time", "Turnaround Time", "Response Time\n"); + "Process", "Priority", "Arrival Time", "Burst Time", + "Completion Time", "Waiting Time", "Turnaround Time", "Response Time"); double totalWT = 0, totalTAT = 0; - Arrays.sort(processes, (a, b) -> a.pid - b.pid); + Comparator byPid = (p1, p2) -> Integer.compare(p1.getPid(), p2.getPid()); + Arrays.sort(processes, byPid); - for (Process p : processes) - { - totalWT += p.waitingTime; - totalTAT += p.turnaroundTime; + for (Process p : processes) { + totalWT += p.getWaitingTime(); + totalTAT += p.getTurnaroundTime(); System.out.printf("%-10d%-10d%-15d%-15d%-17d%-15d%-17d%-15d\n", - p.pid, p.priority, p.arrivalTime, p.burstTime, - p.completionTime, p.waitingTime, p.turnaroundTime, p.responseTime); + p.getPid(), p.getPriority(), p.getArrivalTime(), p.getBurstTime(), + p.getCompletionTime(), p.getWaitingTime(), p.getTurnaroundTime(), p.getResponseTime()); } - System.out.printf("Average Waiting Time: %.2f\n", totalWT / processes.length); + System.out.printf("\nAverage Waiting Time: %.2f\n", totalWT / processes.length); System.out.printf("Average Turnaround Time: %.2f\n", totalTAT / processes.length); } -} \ No newline at end of file +} From d2c8ec4d5b7770a6d13b7b0f83f7205646041cb0 Mon Sep 17 00:00:00 2001 From: Chaitanya Shah <149926880+ChaitanyaShah26@users.noreply.github.com> Date: Thu, 23 Oct 2025 15:18:13 +0530 Subject: [PATCH 3/4] Enhance code readability with comments and formatting Added comments and improved code formatting for clarity. --- .../Preemptive&NonPreemptiveScheduling.java | 97 +++++++++++++------ 1 file changed, 70 insertions(+), 27 deletions(-) diff --git a/src/main/java/com/thealgorithms/scheduling/Preemptive&NonPreemptiveScheduling.java b/src/main/java/com/thealgorithms/scheduling/Preemptive&NonPreemptiveScheduling.java index c1a6fe48551f..d9001473a388 100644 --- a/src/main/java/com/thealgorithms/scheduling/Preemptive&NonPreemptiveScheduling.java +++ b/src/main/java/com/thealgorithms/scheduling/Preemptive&NonPreemptiveScheduling.java @@ -1,8 +1,16 @@ -import java.util.Scanner; import java.util.Arrays; import java.util.Comparator; +import java.util.Scanner; +/** + * Implements and compares non-preemptive (SJF) and preemptive (SRTF) CPU + * scheduling algorithms. + */ class SchedulingAlgorithms { + + /** + * Represents a process with its attributes for scheduling. + */ static class Process { private int pid; private int priority; @@ -20,8 +28,10 @@ static class Process { this.arrivalTime = arrivalTime; this.burstTime = burstTime; this.remainingTime = burstTime; - this.responseTime = -1; + this.responseTime = -1; // Initialize response time as not set } + + // --- Getters --- public int getPid() { return pid; } public int getPriority() { return priority; } public int getArrivalTime() { return arrivalTime; } @@ -32,6 +42,7 @@ static class Process { public int getTurnaroundTime() { return turnaroundTime; } public int getResponseTime() { return responseTime; } + // --- Setters --- public void setRemainingTime(int remainingTime) { this.remainingTime = remainingTime; } public void setCompletionTime(int completionTime) { this.completionTime = completionTime; } public void setWaitingTime(int waitingTime) { this.waitingTime = waitingTime; } @@ -40,6 +51,7 @@ static class Process { } public static void main(String[] args) { + // Use try-with-resources to ensure the scanner is closed automatically try (Scanner sc = new Scanner(System.in)) { System.out.print("Enter number of processes: "); int n = sc.nextInt(); @@ -63,19 +75,24 @@ public static void main(String[] args) { int choice = sc.nextInt(); switch (choice) { - case 1: - nonPreemptive(processes); - break; - case 2: - preemptive(processes); - break; - default: - System.out.println("Invalid choice. Exiting."); - return; + case 1: + nonPreemptive(processes); + break; + case 2: + preemptive(processes); + break; + default: + System.out.println("Invalid choice. Exiting."); + return; } } } - + + /** + * Executes the Non-Preemptive Shortest Job First (SJF) scheduling + * algorithm. Tie-breaking is done based on priority. + * @param processes Array of processes to be scheduled. + */ private static void nonPreemptive(Process[] processes) { int n = processes.length; int completedProcesses = 0; @@ -87,13 +104,16 @@ private static void nonPreemptive(Process[] processes) { int minBurstTime = Integer.MAX_VALUE; int highestPriority = Integer.MAX_VALUE; + // Find the process with the shortest burst time among arrived processes for (int i = 0; i < n; i++) { if (processes[i].getArrivalTime() <= currentTime && !isCompleted[i]) { if (processes[i].getBurstTime() < minBurstTime) { minBurstTime = processes[i].getBurstTime(); highestPriority = processes[i].getPriority(); selectedProcessIndex = i; - } + } + // Tie-breaker: if burst times are equal, choose based on + // priority else if (processes[i].getBurstTime() == minBurstTime) { if (processes[i].getPriority() < highestPriority) { highestPriority = processes[i].getPriority(); @@ -105,19 +125,22 @@ else if (processes[i].getBurstTime() == minBurstTime) { if (selectedProcessIndex != -1) { Process currentProcess = processes[selectedProcessIndex]; - + + // Set response time if it's the first time the process runs if (currentProcess.getResponseTime() == -1) { - currentProcess.setResponseTime(currentTime - currentProcess.getArrivalTime()); + currentProcess.setResponseTime(currentTime - currentProcess.getArrivalTime()); } currentProcess.setWaitingTime(currentTime - currentProcess.getArrivalTime()); currentTime += currentProcess.getBurstTime(); currentProcess.setCompletionTime(currentTime); - currentProcess.setTurnaroundTime(currentProcess.getCompletionTime() - currentProcess.getArrivalTime()); - + currentProcess.setTurnaroundTime( + currentProcess.getCompletionTime() - currentProcess.getArrivalTime()); + isCompleted[selectedProcessIndex] = true; completedProcesses++; } else { + // If no process is available, increment time currentTime++; } } @@ -125,6 +148,11 @@ else if (processes[i].getBurstTime() == minBurstTime) { printResults(processes, "Non-Preemptive SJF (with Priority) Scheduling"); } + /** + * Executes the Preemptive Shortest Remaining Time First (SRTF) scheduling + * algorithm. + * @param processes Array of processes to be scheduled. + */ private static void preemptive(Process[] processes) { int n = processes.length; int completedProcesses = 0; @@ -134,6 +162,8 @@ private static void preemptive(Process[] processes) { int shortestJobIndex = -1; int minRemainingTime = Integer.MAX_VALUE; + // Find the process with the minimum remaining time among the arrived + // processes for (int i = 0; i < n; i++) { if (processes[i].getArrivalTime() <= currentTime && processes[i].getRemainingTime() > 0) { if (processes[i].getRemainingTime() < minRemainingTime) { @@ -150,19 +180,25 @@ private static void preemptive(Process[] processes) { Process currentProcess = processes[shortestJobIndex]; + // Set response time when the process gets CPU for the first time if (currentProcess.getResponseTime() == -1) { currentProcess.setResponseTime(currentTime - currentProcess.getArrivalTime()); } + // Decrement remaining time and increment current time currentProcess.setRemainingTime(currentProcess.getRemainingTime() - 1); currentTime++; + // If a process is completed if (currentProcess.getRemainingTime() == 0) { completedProcesses++; currentProcess.setCompletionTime(currentTime); - currentProcess.setTurnaroundTime(currentProcess.getCompletionTime() - currentProcess.getArrivalTime()); - currentProcess.setWaitingTime(currentProcess.getTurnaroundTime() - currentProcess.getBurstTime()); - + currentProcess.setTurnaroundTime( + currentProcess.getCompletionTime() - currentProcess.getArrivalTime()); + currentProcess.setWaitingTime( + currentProcess.getTurnaroundTime() - currentProcess.getBurstTime()); + + // Ensure waiting time is not negative if (currentProcess.getWaitingTime() < 0) { currentProcess.setWaitingTime(0); } @@ -172,23 +208,30 @@ private static void preemptive(Process[] processes) { printResults(processes, "Preemptive SRTF Scheduling"); } + /** + * Prints the scheduling results in a formatted table. + * @param processes Array of completed processes. + * @param title The title of the scheduling algorithm. + */ private static void printResults(Process[] processes, String title) { System.out.println("\n" + title); - System.out.printf("%-10s%-10s%-15s%-15s%-17s%-15s%-17s%-15s\n", - "Process", "Priority", "Arrival Time", "Burst Time", - "Completion Time", "Waiting Time", "Turnaround Time", "Response Time"); + System.out.printf("%-10s%-10s%-15s%-15s%-17s%-15s%-17s%-15s\n", "Process", "Priority", + "Arrival Time", "Burst Time", "Completion Time", "Waiting Time", "Turnaround Time", + "Response Time"); - double totalWT = 0, totalTAT = 0; + double totalWT = 0; + double totalTAT = 0; + // Define a comparator to sort processes by PID for consistent output Comparator byPid = (p1, p2) -> Integer.compare(p1.getPid(), p2.getPid()); Arrays.sort(processes, byPid); for (Process p : processes) { totalWT += p.getWaitingTime(); totalTAT += p.getTurnaroundTime(); - System.out.printf("%-10d%-10d%-15d%-15d%-17d%-15d%-17d%-15d\n", - p.getPid(), p.getPriority(), p.getArrivalTime(), p.getBurstTime(), - p.getCompletionTime(), p.getWaitingTime(), p.getTurnaroundTime(), p.getResponseTime()); + System.out.printf("%-10d%-10d%-15d%-15d%-17d%-15d%-17d%-15d\n", p.getPid(), + p.getPriority(), p.getArrivalTime(), p.getBurstTime(), p.getCompletionTime(), + p.getWaitingTime(), p.getTurnaroundTime(), p.getResponseTime()); } System.out.printf("\nAverage Waiting Time: %.2f\n", totalWT / processes.length); From 1e10906bce882ed433d7a1aef3b57cfa5619ab7e Mon Sep 17 00:00:00 2001 From: Chaitanya Shah <149926880+ChaitanyaShah26@users.noreply.github.com> Date: Thu, 23 Oct 2025 15:22:47 +0530 Subject: [PATCH 4/4] Add reference link in comments --- .../scheduling/Preemptive&NonPreemptiveScheduling.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/java/com/thealgorithms/scheduling/Preemptive&NonPreemptiveScheduling.java b/src/main/java/com/thealgorithms/scheduling/Preemptive&NonPreemptiveScheduling.java index d9001473a388..b62961e22084 100644 --- a/src/main/java/com/thealgorithms/scheduling/Preemptive&NonPreemptiveScheduling.java +++ b/src/main/java/com/thealgorithms/scheduling/Preemptive&NonPreemptiveScheduling.java @@ -1,3 +1,7 @@ +/* +Preemptive & Non-Preemptive Scheduling Algorithm reference: https://www.geeksforgeeks.org/operating-systems/preemptive-and-non-preemptive-scheduling/ +*/ + import java.util.Arrays; import java.util.Comparator; import java.util.Scanner;