From 993fdcdeb11947c28b14abbc61ce71a5ec3a051a Mon Sep 17 00:00:00 2001 From: Khwaish Chawla <126390524+khwaishchawla@users.noreply.github.com> Date: Wed, 23 Oct 2024 23:42:38 +0530 Subject: [PATCH 1/2] Create pss.java --- .../com/thealgorithms/scheduling/pss.java | 96 +++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 src/main/java/com/thealgorithms/scheduling/pss.java diff --git a/src/main/java/com/thealgorithms/scheduling/pss.java b/src/main/java/com/thealgorithms/scheduling/pss.java new file mode 100644 index 000000000000..ed5a789dd133 --- /dev/null +++ b/src/main/java/com/thealgorithms/scheduling/pss.java @@ -0,0 +1,96 @@ +import java.util.PriorityQueue; +import java.util.Scanner; + +// Process class representing a process with attributes: id, weight, burst time, arrival time, and time received +class Process { + int id; + int weight; + int burstTime; + int timeReceived; + int arrivalTime; + int priorityBoost; // Boost to prevent starvation + + public Process(int id, int weight, int burstTime, int arrivalTime) { + this.id = id; + this.weight = weight; + this.burstTime = burstTime; + this.arrivalTime = arrivalTime; + this.timeReceived = 0; + this.priorityBoost = 0; + } +} + +// Comparator to prioritize processes by weight and starvation prevention +class ProcessComparator implements java.util.Comparator { + @Override + public int compare(Process p1, Process p2) { + int effectiveWeight1 = p1.weight + p1.priorityBoost; + int effectiveWeight2 = p2.weight + p2.priorityBoost; + return Integer.compare(effectiveWeight2, effectiveWeight1); // Higher weight gets higher priority + } +} + +public class ProportionalShareScheduling { + + // Method to implement the scheduling logic + public static void proportionalShareScheduling(PriorityQueue readyQueue, int totalTimeQuantum) { + int currentTime = 0; + while (!readyQueue.isEmpty()) { + Process current = readyQueue.poll(); + + // Determine time quantum to allocate to the current process + int timeGiven = Math.min(current.weight, current.burstTime - current.timeReceived); + + currentTime += timeGiven; + current.timeReceived += timeGiven; + + System.out.println("Process " + current.id + " received " + timeGiven + " units at time " + currentTime); + + // If the process has finished execution + if (current.timeReceived >= current.burstTime) { + System.out.println("Process " + current.id + " completed at time " + currentTime); + } else { + // Starvation prevention: increase priority boost for unfinished processes + current.priorityBoost += 1; + readyQueue.add(current); // Reinsert into the queue for future execution + } + + // Starvation prevention logic for all other processes in the queue + for (Process p : readyQueue) { + p.priorityBoost += 1; // Increase the boost to prevent starvation + } + } + } + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + + // Input number of processes + System.out.print("Enter the number of processes: "); + int n = sc.nextInt(); + + // Input total time quantum + System.out.print("Enter the total time quantum: "); + int totalTimeQuantum = sc.nextInt(); + + // Create a priority queue to store processes based on the comparator + PriorityQueue readyQueue = new PriorityQueue<>(new ProcessComparator()); + + // Input the process details (id, weight, burst time, arrival time) + for (int i = 0; i < n; i++) { + System.out.println("Enter Process ID, Weight, Burst Time, Arrival Time for Process " + (i + 1) + ":"); + int id = sc.nextInt(); + int weight = sc.nextInt(); + int burstTime = sc.nextInt(); + int arrivalTime = sc.nextInt(); + + Process process = new Process(id, weight, burstTime, arrivalTime); + readyQueue.add(process); // Add each process to the priority queue + } + + // Call the scheduling function + proportionalShareScheduling(readyQueue, totalTimeQuantum); + + sc.close(); + } +} From 4e330443281fbe5bd7461e76db5590e4550eb3cd Mon Sep 17 00:00:00 2001 From: Khwaish Chawla <126390524+khwaishchawla@users.noreply.github.com> Date: Thu, 24 Oct 2024 08:31:45 +0530 Subject: [PATCH 2/2] Update pss.java --- .../com/thealgorithms/scheduling/pss.java | 164 +++++++++++------- 1 file changed, 102 insertions(+), 62 deletions(-) diff --git a/src/main/java/com/thealgorithms/scheduling/pss.java b/src/main/java/com/thealgorithms/scheduling/pss.java index ed5a789dd133..69dc05f04d52 100644 --- a/src/main/java/com/thealgorithms/scheduling/pss.java +++ b/src/main/java/com/thealgorithms/scheduling/pss.java @@ -1,96 +1,136 @@ +package com.thealgorithms.scheduling; + +import java.util.ArrayList; +import java.util.Comparator; +import java.util.List; import java.util.PriorityQueue; -import java.util.Scanner; - -// Process class representing a process with attributes: id, weight, burst time, arrival time, and time received -class Process { - int id; - int weight; - int burstTime; - int timeReceived; - int arrivalTime; - int priorityBoost; // Boost to prevent starvation - - public Process(int id, int weight, int burstTime, int arrivalTime) { - this.id = id; - this.weight = weight; - this.burstTime = burstTime; - this.arrivalTime = arrivalTime; - this.timeReceived = 0; - this.priorityBoost = 0; - } -} -// Comparator to prioritize processes by weight and starvation prevention -class ProcessComparator implements java.util.Comparator { - @Override - public int compare(Process p1, Process p2) { - int effectiveWeight1 = p1.weight + p1.priorityBoost; - int effectiveWeight2 = p2.weight + p2.priorityBoost; - return Integer.compare(effectiveWeight2, effectiveWeight1); // Higher weight gets higher priority +/** + * The Proportional Share Scheduling (PSS) class implements a dynamic scheduling algorithm. + * It assigns the CPU to processes based on their weight, ensuring that each process gets CPU time proportional to its weight. + * The priority boost prevents starvation, ensuring that lower-weight processes still receive CPU time. + */ +public final class ProportionalShareScheduling { + + private ProportionalShareScheduling() { } -} -public class ProportionalShareScheduling { + private List processes; - // Method to implement the scheduling logic - public static void proportionalShareScheduling(PriorityQueue readyQueue, int totalTimeQuantum) { + /** + * Constructs a ProportionalShareScheduling object with a list of processes. + * + * @param processes List of processes to be scheduled. + */ + public ProportionalShareScheduling(final List processes) { + this.processes = processes; + } + + /** + * Schedules the processes using Proportional Share Scheduling (PSS). + * Processes are scheduled based on their weight and priority boost. + * It simulates their execution, calculates the waiting time, and ensures no starvation. + * + * @return List of processes after they have been executed in proportion to their weight. + */ + public List scheduleProcesses(int totalTimeQuantum) { + PriorityQueue readyQueue = new PriorityQueue<>(new ProcessComparator()); + readyQueue.addAll(processes); int currentTime = 0; + List executedProcesses = new ArrayList<>(); + while (!readyQueue.isEmpty()) { Process current = readyQueue.poll(); - - // Determine time quantum to allocate to the current process - int timeGiven = Math.min(current.weight, current.burstTime - current.timeReceived); + // Determine time quantum to allocate to the current process + int timeGiven = Math.min(current.getWeight(), current.getBurstTime() - current.getTimeReceived()); currentTime += timeGiven; - current.timeReceived += timeGiven; + current.setTimeReceived(current.getTimeReceived() + timeGiven); - System.out.println("Process " + current.id + " received " + timeGiven + " units at time " + currentTime); + System.out.println("Process " + current.getProcessId() + " received " + timeGiven + " units at time " + currentTime); // If the process has finished execution - if (current.timeReceived >= current.burstTime) { - System.out.println("Process " + current.id + " completed at time " + currentTime); + if (current.getTimeReceived() >= current.getBurstTime()) { + System.out.println("Process " + current.getProcessId() + " completed at time " + currentTime); } else { // Starvation prevention: increase priority boost for unfinished processes - current.priorityBoost += 1; + current.setPriorityBoost(current.getPriorityBoost() + 1); readyQueue.add(current); // Reinsert into the queue for future execution } // Starvation prevention logic for all other processes in the queue for (Process p : readyQueue) { - p.priorityBoost += 1; // Increase the boost to prevent starvation + p.setPriorityBoost(p.getPriorityBoost() + 1); // Increase the boost to prevent starvation } + + executedProcesses.add(current); } + + return executedProcesses; } - public static void main(String[] args) { - Scanner sc = new Scanner(System.in); + /** + * The Process class represents a process with an ID, weight, burst time, arrival time, time received, and priority boost. + */ + public static class Process { + private String processId; + private int weight; + private int burstTime; + private int arrivalTime; + private int timeReceived; + private int priorityBoost; + + public Process(String processId, int weight, int burstTime, int arrivalTime) { + this.processId = processId; + this.weight = weight; + this.burstTime = burstTime; + this.arrivalTime = arrivalTime; + this.timeReceived = 0; + this.priorityBoost = 0; + } - // Input number of processes - System.out.print("Enter the number of processes: "); - int n = sc.nextInt(); + public String getProcessId() { + return processId; + } - // Input total time quantum - System.out.print("Enter the total time quantum: "); - int totalTimeQuantum = sc.nextInt(); + public int getWeight() { + return weight; + } - // Create a priority queue to store processes based on the comparator - PriorityQueue readyQueue = new PriorityQueue<>(new ProcessComparator()); + public int getBurstTime() { + return burstTime; + } + + public int getArrivalTime() { + return arrivalTime; + } - // Input the process details (id, weight, burst time, arrival time) - for (int i = 0; i < n; i++) { - System.out.println("Enter Process ID, Weight, Burst Time, Arrival Time for Process " + (i + 1) + ":"); - int id = sc.nextInt(); - int weight = sc.nextInt(); - int burstTime = sc.nextInt(); - int arrivalTime = sc.nextInt(); + public int getTimeReceived() { + return timeReceived; + } + + public void setTimeReceived(int timeReceived) { + this.timeReceived = timeReceived; + } - Process process = new Process(id, weight, burstTime, arrivalTime); - readyQueue.add(process); // Add each process to the priority queue + public int getPriorityBoost() { + return priorityBoost; } - // Call the scheduling function - proportionalShareScheduling(readyQueue, totalTimeQuantum); + public void setPriorityBoost(int priorityBoost) { + this.priorityBoost = priorityBoost; + } + } - sc.close(); + /** + * The ProcessComparator class compares processes based on their effective weight (weight + priority boost). + */ + public static class ProcessComparator implements Comparator { + @Override + public int compare(Process p1, Process p2) { + int effectiveWeight1 = p1.getWeight() + p1.getPriorityBoost(); + int effectiveWeight2 = p2.getWeight() + p2.getPriorityBoost(); + return Integer.compare(effectiveWeight2, effectiveWeight1); // Higher weight gets higher priority + } } }