|
| 1 | +// Java program to implement Shortest Remaining Time First |
| 2 | +// Shortest Remaining Time First (SRTF) |
| 3 | + |
| 4 | +class Process |
| 5 | +{ |
| 6 | + int pid; // Process ID |
| 7 | + int bt; // Burst Time |
| 8 | + int art; // Arrival Time |
| 9 | + |
| 10 | + public Process(int pid, int bt, int art) |
| 11 | + { |
| 12 | + this.pid = pid; |
| 13 | + this.bt = bt; |
| 14 | + this.art = art; |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +public class SRTF |
| 19 | +{ |
| 20 | + // Method to find the waiting time for all |
| 21 | + // processes |
| 22 | + static void findWaitingTime(Process proc[], int n, |
| 23 | + int wt[]) |
| 24 | + { |
| 25 | + int rt[] = new int[n]; |
| 26 | + |
| 27 | + // Copy the burst time into rt[] |
| 28 | + for (int i = 0; i < n; i++) |
| 29 | + rt[i] = proc[i].bt; |
| 30 | + |
| 31 | + int complete = 0, t = 0, minm = Integer.MAX_VALUE; |
| 32 | + int shortest = 0, finish_time; |
| 33 | + boolean check = false; |
| 34 | + |
| 35 | + // Process until all processes gets |
| 36 | + // completed |
| 37 | + while (complete != n) { |
| 38 | + |
| 39 | + // Find process with minimum |
| 40 | + // remaining time among the |
| 41 | + // processes that arrives till the |
| 42 | + // current time` |
| 43 | + for (int j = 0; j < n; j++) |
| 44 | + { |
| 45 | + if ((proc[j].art <= t) && |
| 46 | + (rt[j] < minm) && rt[j] > 0) { |
| 47 | + minm = rt[j]; |
| 48 | + shortest = j; |
| 49 | + check = true; |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + if (check == false) { |
| 54 | + t++; |
| 55 | + continue; |
| 56 | + } |
| 57 | + |
| 58 | + // Reduce remaining time by one |
| 59 | + rt[shortest]--; |
| 60 | + |
| 61 | + // Update minimum |
| 62 | + minm = rt[shortest]; |
| 63 | + if (minm == 0) |
| 64 | + minm = Integer.MAX_VALUE; |
| 65 | + |
| 66 | + // If a process gets completely |
| 67 | + // executed |
| 68 | + if (rt[shortest] == 0) { |
| 69 | + |
| 70 | + // Increment complete |
| 71 | + complete++; |
| 72 | + check = false; |
| 73 | + |
| 74 | + // Find finish time of current |
| 75 | + // process |
| 76 | + finish_time = t + 1; |
| 77 | + |
| 78 | + // Calculate waiting time |
| 79 | + wt[shortest] = finish_time - |
| 80 | + proc[shortest].bt - |
| 81 | + proc[shortest].art; |
| 82 | + |
| 83 | + if (wt[shortest] < 0) |
| 84 | + wt[shortest] = 0; |
| 85 | + } |
| 86 | + // Increment time |
| 87 | + t++; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + // Method to calculate turn around time |
| 92 | + static void findTurnAroundTime(Process proc[], int n, |
| 93 | + int wt[], int tat[]) |
| 94 | + { |
| 95 | + // calculating turnaround time by adding |
| 96 | + // bt[i] + wt[i] |
| 97 | + for (int i = 0; i < n; i++) |
| 98 | + tat[i] = proc[i].bt + wt[i]; |
| 99 | + } |
| 100 | + |
| 101 | + // Method to calculate average time |
| 102 | + static void findavgTime(Process proc[], int n) |
| 103 | + { |
| 104 | + int wt[] = new int[n], tat[] = new int[n]; |
| 105 | + int total_wt = 0, total_tat = 0; |
| 106 | + |
| 107 | + // Function to find waiting time of all |
| 108 | + // processes |
| 109 | + findWaitingTime(proc, n, wt); |
| 110 | + |
| 111 | + // Function to find turn around time for |
| 112 | + // all processes |
| 113 | + findTurnAroundTime(proc, n, wt, tat); |
| 114 | + |
| 115 | + // Display processes along with all |
| 116 | + // details |
| 117 | + System.out.println("Processes " + |
| 118 | + " Burst time " + |
| 119 | + " Waiting time " + |
| 120 | + " Turn around time"); |
| 121 | + |
| 122 | + // Calculate total waiting time and |
| 123 | + // total turnaround time |
| 124 | + for (int i = 0; i < n; i++) { |
| 125 | + total_wt = total_wt + wt[i]; |
| 126 | + total_tat = total_tat + tat[i]; |
| 127 | + System.out.println(" " + proc[i].pid + "\t\t" |
| 128 | + + proc[i].bt + "\t\t " + wt[i] |
| 129 | + + "\t\t" + tat[i]); |
| 130 | + } |
| 131 | + |
| 132 | + System.out.println("Average waiting time = " + |
| 133 | + (float)total_wt / (float)n); |
| 134 | + System.out.println("Average turn around time = " + |
| 135 | + (float)total_tat / (float)n); |
| 136 | + } |
| 137 | + |
| 138 | + // Driver Method |
| 139 | + public static void main(String[] args) |
| 140 | + { |
| 141 | + Process proc[] = { new Process(1, 6, 1), |
| 142 | + new Process(2, 8, 1), |
| 143 | + new Process(3, 7, 2), |
| 144 | + new Process(4, 3, 3)}; |
| 145 | + |
| 146 | + findavgTime(proc, proc.length); |
| 147 | + } |
| 148 | +} |
| 149 | + |
0 commit comments