|
| 1 | +package src.main.java.com.others; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | + |
| 5 | +/** |
| 6 | + * This class implements the Round Robin Algorithm which is an cpu scheduling algorithm. |
| 7 | + * |
| 8 | + * @author George Giannios |
| 9 | + */ |
| 10 | +public class RoundRobin { |
| 11 | + /** |
| 12 | + * This method calculates the waiting time for all processes |
| 13 | + * |
| 14 | + * @param burstTime an array with burst time for all processes |
| 15 | + * @param quantum the quantum quantity |
| 16 | + * |
| 17 | + * @return an array with waiting time for all processes |
| 18 | + */ |
| 19 | + public int[] calcWaitingTime(int[] burstTime, int quantum) |
| 20 | + { |
| 21 | + int n= burstTime.length; |
| 22 | + //create a copy of burstTime table to executeTime table |
| 23 | + int[] executeTIme= new int [n]; |
| 24 | + for (int i=0;i<n;i++) |
| 25 | + executeTIme[i]=burstTime[i]; |
| 26 | + |
| 27 | + //initialize the waiting time table and set all waiting times equal to zero |
| 28 | + int[] waitingTime= new int [n]; |
| 29 | + for (int i=0;i<n;i++) |
| 30 | + waitingTime[i]=0; |
| 31 | + |
| 32 | + //initialize an array list to emulate the queue of ready processes |
| 33 | + ArrayList<Integer> readyQueue = new ArrayList<>(); |
| 34 | + for(int i=0;i<n;i++) |
| 35 | + readyQueue.add(i); |
| 36 | + |
| 37 | + //the total time that processes need to be finished |
| 38 | + int time=0; |
| 39 | + int i=0; |
| 40 | + //calculate waiting times while there are uncompleted processes |
| 41 | + while (!readyQueue.isEmpty()) |
| 42 | + { |
| 43 | + //check if a process has finished |
| 44 | + if (executeTIme[i]>=0) { |
| 45 | + if (executeTIme[i] - quantum > 0) { |
| 46 | + //add time that have been passed |
| 47 | + time += quantum; |
| 48 | + //this is the remaining burst time for the process i |
| 49 | + executeTIme[i] -= quantum; |
| 50 | + |
| 51 | + } else if (executeTIme[i] - quantum == 0) { |
| 52 | + //add time that have been passed |
| 53 | + time += quantum; |
| 54 | + //calculate the total waiting time |
| 55 | + waitingTime[i] = time - burstTime[i]; |
| 56 | + |
| 57 | + //mark the process as finished |
| 58 | + executeTIme[i] = -1; |
| 59 | + //remove the process that have finished by shrinking queue's length |
| 60 | + readyQueue.remove(readyQueue.size()-1); |
| 61 | + |
| 62 | + } else { |
| 63 | + //add time that have been passed |
| 64 | + time += executeTIme[i]; |
| 65 | + //calculate the total waiting time |
| 66 | + waitingTime[i] = time - burstTime[i]; |
| 67 | + |
| 68 | + //mark the process as finished |
| 69 | + executeTIme[i] = -1; |
| 70 | + //remove the process that have finished by shrinking queue's length |
| 71 | + readyQueue.remove(readyQueue.size()-1); |
| 72 | + } |
| 73 | + } |
| 74 | + i++; |
| 75 | + if(i>=n) i=0; |
| 76 | + } |
| 77 | + |
| 78 | + return waitingTime; |
| 79 | + } |
| 80 | + |
| 81 | + |
| 82 | + /** |
| 83 | + * This method calculates turn around time for all processes |
| 84 | + * |
| 85 | + * @param burstTime an array with burst time for all processes |
| 86 | + * @param waitingTime an array with waiting time for all processes |
| 87 | + * |
| 88 | + * @return an array with turnaround time for all processes |
| 89 | + */ |
| 90 | + public int[] calcTurnAroundTime(int[] burstTime, int[] waitingTime) |
| 91 | + { |
| 92 | + int n= burstTime.length; |
| 93 | + //initialize the turnaround time table |
| 94 | + int[] turnAroundTime= new int [n]; |
| 95 | + |
| 96 | + //calculate turnaround time for each process (T.T= W.T + B.T) |
| 97 | + for (int i=0; i<n;i++) |
| 98 | + turnAroundTime[i]=waitingTime[i]+burstTime[i]; |
| 99 | + |
| 100 | + //return the turnaround time table |
| 101 | + return turnAroundTime; |
| 102 | + } |
| 103 | + |
| 104 | + |
| 105 | + /** |
| 106 | + * This method prints the results and calculates the average waiting and turnaround times |
| 107 | + * |
| 108 | + * @param burstTime an array with burst time for all processes |
| 109 | + * @param quantum the quantum quantity |
| 110 | + */ |
| 111 | + void printAvgTimes(int[] burstTime, int quantum) |
| 112 | + { |
| 113 | + int n = burstTime.length; |
| 114 | + int totalWaitingTime = 0; |
| 115 | + int totalTurnAroundTime = 0; |
| 116 | + |
| 117 | + // Find waiting time of all processes |
| 118 | + int[] waitingTime = calcWaitingTime(burstTime, quantum); |
| 119 | + |
| 120 | + // Find turn around time for all processes |
| 121 | + int[] turnAroundTime = calcTurnAroundTime(burstTime, waitingTime); |
| 122 | + |
| 123 | + // Display processes along with all details |
| 124 | + System.out.println("Process " + " Burst Time " + |
| 125 | + " Waiting Time " + " Turnaround Time"); |
| 126 | + System.out.println("======= ========== ============ ==============="); |
| 127 | + // Calculate total waiting time and total turn around time |
| 128 | + for (int i = 0; i < n; i++) { |
| 129 | + totalWaitingTime += waitingTime[i]; |
| 130 | + totalTurnAroundTime += turnAroundTime[i]; |
| 131 | + System.out.println(i + "\t\t " + burstTime[i] +"\t\t\t " + |
| 132 | + waitingTime[i] +"\t\t\t " + turnAroundTime[i]); |
| 133 | + } |
| 134 | + |
| 135 | + System.out.println("\nAverage waiting time = " + |
| 136 | + (float)totalWaitingTime / (float)n); |
| 137 | + System.out.println("Average turnaround time = " + |
| 138 | + (float)totalTurnAroundTime / (float)n); |
| 139 | + } |
| 140 | +} |
| 141 | + |
0 commit comments