From 72efcaa5647f25a29b625ccf7b4c9e762fa951a2 Mon Sep 17 00:00:00 2001 From: geogiadim Date: Wed, 16 Sep 2020 19:19:50 +0300 Subject: [PATCH] Implementation for Round Robin Algorithm in Java with tests --- src/main/java/com/others/RoundRobin.java | 141 +++++++++++++++++++ src/test/java/com/others/RoundRobinTest.java | 26 ++++ 2 files changed, 167 insertions(+) create mode 100644 src/main/java/com/others/RoundRobin.java create mode 100644 src/test/java/com/others/RoundRobinTest.java diff --git a/src/main/java/com/others/RoundRobin.java b/src/main/java/com/others/RoundRobin.java new file mode 100644 index 00000000..cb17853d --- /dev/null +++ b/src/main/java/com/others/RoundRobin.java @@ -0,0 +1,141 @@ +package src.main.java.com.others; + +import java.util.ArrayList; + +/** + * This class implements the Round Robin Algorithm which is an cpu scheduling algorithm. + * + * @author George Giannios + */ +public class RoundRobin { + /** + * This method calculates the waiting time for all processes + * + * @param burstTime an array with burst time for all processes + * @param quantum the quantum quantity + * + * @return an array with waiting time for all processes + */ + public int[] calcWaitingTime(int[] burstTime, int quantum) + { + int n= burstTime.length; + //create a copy of burstTime table to executeTime table + int[] executeTIme= new int [n]; + for (int i=0;i readyQueue = new ArrayList<>(); + for(int i=0;i=0) { + if (executeTIme[i] - quantum > 0) { + //add time that have been passed + time += quantum; + //this is the remaining burst time for the process i + executeTIme[i] -= quantum; + + } else if (executeTIme[i] - quantum == 0) { + //add time that have been passed + time += quantum; + //calculate the total waiting time + waitingTime[i] = time - burstTime[i]; + + //mark the process as finished + executeTIme[i] = -1; + //remove the process that have finished by shrinking queue's length + readyQueue.remove(readyQueue.size()-1); + + } else { + //add time that have been passed + time += executeTIme[i]; + //calculate the total waiting time + waitingTime[i] = time - burstTime[i]; + + //mark the process as finished + executeTIme[i] = -1; + //remove the process that have finished by shrinking queue's length + readyQueue.remove(readyQueue.size()-1); + } + } + i++; + if(i>=n) i=0; + } + + return waitingTime; + } + + + /** + * This method calculates turn around time for all processes + * + * @param burstTime an array with burst time for all processes + * @param waitingTime an array with waiting time for all processes + * + * @return an array with turnaround time for all processes + */ + public int[] calcTurnAroundTime(int[] burstTime, int[] waitingTime) + { + int n= burstTime.length; + //initialize the turnaround time table + int[] turnAroundTime= new int [n]; + + //calculate turnaround time for each process (T.T= W.T + B.T) + for (int i=0; i