|
| 1 | +import java.util.PriorityQueue; |
| 2 | +import java.util.Scanner; |
| 3 | + |
| 4 | +// Process class representing a process with attributes: id, weight, burst time, arrival time, and time received |
| 5 | +class Process { |
| 6 | + int id; |
| 7 | + int weight; |
| 8 | + int burstTime; |
| 9 | + int timeReceived; |
| 10 | + int arrivalTime; |
| 11 | + int priorityBoost; // Boost to prevent starvation |
| 12 | + |
| 13 | + public Process(int id, int weight, int burstTime, int arrivalTime) { |
| 14 | + this.id = id; |
| 15 | + this.weight = weight; |
| 16 | + this.burstTime = burstTime; |
| 17 | + this.arrivalTime = arrivalTime; |
| 18 | + this.timeReceived = 0; |
| 19 | + this.priorityBoost = 0; |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +// Comparator to prioritize processes by weight and starvation prevention |
| 24 | +class ProcessComparator implements java.util.Comparator<Process> { |
| 25 | + @Override |
| 26 | + public int compare(Process p1, Process p2) { |
| 27 | + int effectiveWeight1 = p1.weight + p1.priorityBoost; |
| 28 | + int effectiveWeight2 = p2.weight + p2.priorityBoost; |
| 29 | + return Integer.compare(effectiveWeight2, effectiveWeight1); // Higher weight gets higher priority |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +public class ProportionalShareScheduling { |
| 34 | + |
| 35 | + // Method to implement the scheduling logic |
| 36 | + public static void proportionalShareScheduling(PriorityQueue<Process> readyQueue, int totalTimeQuantum) { |
| 37 | + int currentTime = 0; |
| 38 | + while (!readyQueue.isEmpty()) { |
| 39 | + Process current = readyQueue.poll(); |
| 40 | + |
| 41 | + // Determine time quantum to allocate to the current process |
| 42 | + int timeGiven = Math.min(current.weight, current.burstTime - current.timeReceived); |
| 43 | + |
| 44 | + currentTime += timeGiven; |
| 45 | + current.timeReceived += timeGiven; |
| 46 | + |
| 47 | + System.out.println("Process " + current.id + " received " + timeGiven + " units at time " + currentTime); |
| 48 | + |
| 49 | + // If the process has finished execution |
| 50 | + if (current.timeReceived >= current.burstTime) { |
| 51 | + System.out.println("Process " + current.id + " completed at time " + currentTime); |
| 52 | + } else { |
| 53 | + // Starvation prevention: increase priority boost for unfinished processes |
| 54 | + current.priorityBoost += 1; |
| 55 | + readyQueue.add(current); // Reinsert into the queue for future execution |
| 56 | + } |
| 57 | + |
| 58 | + // Starvation prevention logic for all other processes in the queue |
| 59 | + for (Process p : readyQueue) { |
| 60 | + p.priorityBoost += 1; // Increase the boost to prevent starvation |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + public static void main(String[] args) { |
| 66 | + Scanner sc = new Scanner(System.in); |
| 67 | + |
| 68 | + // Input number of processes |
| 69 | + System.out.print("Enter the number of processes: "); |
| 70 | + int n = sc.nextInt(); |
| 71 | + |
| 72 | + // Input total time quantum |
| 73 | + System.out.print("Enter the total time quantum: "); |
| 74 | + int totalTimeQuantum = sc.nextInt(); |
| 75 | + |
| 76 | + // Create a priority queue to store processes based on the comparator |
| 77 | + PriorityQueue<Process> readyQueue = new PriorityQueue<>(new ProcessComparator()); |
| 78 | + |
| 79 | + // Input the process details (id, weight, burst time, arrival time) |
| 80 | + for (int i = 0; i < n; i++) { |
| 81 | + System.out.println("Enter Process ID, Weight, Burst Time, Arrival Time for Process " + (i + 1) + ":"); |
| 82 | + int id = sc.nextInt(); |
| 83 | + int weight = sc.nextInt(); |
| 84 | + int burstTime = sc.nextInt(); |
| 85 | + int arrivalTime = sc.nextInt(); |
| 86 | + |
| 87 | + Process process = new Process(id, weight, burstTime, arrivalTime); |
| 88 | + readyQueue.add(process); // Add each process to the priority queue |
| 89 | + } |
| 90 | + |
| 91 | + // Call the scheduling function |
| 92 | + proportionalShareScheduling(readyQueue, totalTimeQuantum); |
| 93 | + |
| 94 | + sc.close(); |
| 95 | + } |
| 96 | +} |
0 commit comments