format code
This commit is contained in:
parent
72efcaa564
commit
be160de58b
@ -12,36 +12,36 @@ 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
|
||||
*
|
||||
* @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;
|
||||
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<n;i++)
|
||||
executeTIme[i]=burstTime[i];
|
||||
int[] executeTIme = new int[n];
|
||||
for (int i = 0; i < n; i++) {
|
||||
executeTIme[i] = burstTime[i];
|
||||
}
|
||||
|
||||
//initialize the waiting time table and set all waiting times equal to zero
|
||||
int[] waitingTime= new int [n];
|
||||
for (int i=0;i<n;i++)
|
||||
waitingTime[i]=0;
|
||||
int[] waitingTime = new int[n];
|
||||
for (int i = 0; i < n; i++) {
|
||||
waitingTime[i] = 0;
|
||||
}
|
||||
|
||||
//initialize an array list to emulate the queue of ready processes
|
||||
ArrayList<Integer> readyQueue = new ArrayList<>();
|
||||
for(int i=0;i<n;i++)
|
||||
for (int i = 0; i < n; i++) {
|
||||
readyQueue.add(i);
|
||||
}
|
||||
|
||||
//the total time that processes need to be finished
|
||||
int time=0;
|
||||
int i=0;
|
||||
int time = 0;
|
||||
int i = 0;
|
||||
//calculate waiting times while there are uncompleted processes
|
||||
while (!readyQueue.isEmpty())
|
||||
{
|
||||
while (!readyQueue.isEmpty()) {
|
||||
//check if a process has finished
|
||||
if (executeTIme[i]>=0) {
|
||||
if (executeTIme[i] >= 0) {
|
||||
if (executeTIme[i] - quantum > 0) {
|
||||
//add time that have been passed
|
||||
time += quantum;
|
||||
@ -57,7 +57,7 @@ public class RoundRobin {
|
||||
//mark the process as finished
|
||||
executeTIme[i] = -1;
|
||||
//remove the process that have finished by shrinking queue's length
|
||||
readyQueue.remove(readyQueue.size()-1);
|
||||
readyQueue.remove(readyQueue.size() - 1);
|
||||
|
||||
} else {
|
||||
//add time that have been passed
|
||||
@ -68,11 +68,13 @@ public class RoundRobin {
|
||||
//mark the process as finished
|
||||
executeTIme[i] = -1;
|
||||
//remove the process that have finished by shrinking queue's length
|
||||
readyQueue.remove(readyQueue.size()-1);
|
||||
readyQueue.remove(readyQueue.size() - 1);
|
||||
}
|
||||
}
|
||||
i++;
|
||||
if(i>=n) i=0;
|
||||
if (i >= n) {
|
||||
i = 0;
|
||||
}
|
||||
}
|
||||
|
||||
return waitingTime;
|
||||
@ -82,20 +84,19 @@ public class RoundRobin {
|
||||
/**
|
||||
* This method calculates turn around time for all processes
|
||||
*
|
||||
* @param burstTime an array with burst 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;
|
||||
public int[] calcTurnAroundTime(int[] burstTime, int[] waitingTime) {
|
||||
int n = burstTime.length;
|
||||
//initialize the turnaround time table
|
||||
int[] turnAroundTime= new int [n];
|
||||
int[] turnAroundTime = new int[n];
|
||||
|
||||
//calculate turnaround time for each process (T.T= W.T + B.T)
|
||||
for (int i=0; i<n;i++)
|
||||
turnAroundTime[i]=waitingTime[i]+burstTime[i];
|
||||
for (int i = 0; i < n; i++) {
|
||||
turnAroundTime[i] = waitingTime[i] + burstTime[i];
|
||||
}
|
||||
|
||||
//return the turnaround time table
|
||||
return turnAroundTime;
|
||||
@ -106,10 +107,9 @@ public class RoundRobin {
|
||||
* This method prints the results and calculates the average waiting and turnaround times
|
||||
*
|
||||
* @param burstTime an array with burst time for all processes
|
||||
* @param quantum the quantum quantity
|
||||
* @param quantum the quantum quantity
|
||||
*/
|
||||
void printAvgTimes(int[] burstTime, int quantum)
|
||||
{
|
||||
void printAvgTimes(int[] burstTime, int quantum) {
|
||||
int n = burstTime.length;
|
||||
int totalWaitingTime = 0;
|
||||
int totalTurnAroundTime = 0;
|
||||
@ -128,14 +128,14 @@ public class RoundRobin {
|
||||
for (int i = 0; i < n; i++) {
|
||||
totalWaitingTime += waitingTime[i];
|
||||
totalTurnAroundTime += turnAroundTime[i];
|
||||
System.out.println(i + "\t\t " + burstTime[i] +"\t\t\t " +
|
||||
waitingTime[i] +"\t\t\t " + turnAroundTime[i]);
|
||||
System.out.println(i + "\t\t " + burstTime[i] + "\t\t\t " +
|
||||
waitingTime[i] + "\t\t\t " + turnAroundTime[i]);
|
||||
}
|
||||
|
||||
System.out.println("\nAverage waiting time = " +
|
||||
(float)totalWaitingTime / (float)n);
|
||||
(float) totalWaitingTime / (float) n);
|
||||
System.out.println("Average turnaround time = " +
|
||||
(float)totalTurnAroundTime / (float)n);
|
||||
(float) totalTurnAroundTime / (float) n);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -10,17 +10,17 @@ public class RoundRobinTest {
|
||||
private final RoundRobin roundRobin = new RoundRobin();
|
||||
|
||||
@Test
|
||||
public void testWaitingTime(){
|
||||
int[] expectedTime = {9,12,14,9};
|
||||
int[] realtime= roundRobin.calcWaitingTime(burstTime,3);
|
||||
public void testWaitingTime() {
|
||||
int[] expectedTime = {9, 12, 14, 9};
|
||||
int[] realtime = roundRobin.calcWaitingTime(burstTime, 3);
|
||||
Assert.assertArrayEquals(realtime, expectedTime);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTurnAroundTIme(){
|
||||
int[] expectedTIme={14,27,18,12};
|
||||
int[] waitingTime = {9,12,14,9};
|
||||
int[] realTime= roundRobin.calcTurnAroundTime(burstTime,waitingTime);
|
||||
Assert.assertArrayEquals(realTime,expectedTIme);
|
||||
public void testTurnAroundTIme() {
|
||||
int[] expectedTIme = {14, 27, 18, 12};
|
||||
int[] waitingTime = {9, 12, 14, 9};
|
||||
int[] realTime = roundRobin.calcTurnAroundTime(burstTime, waitingTime);
|
||||
Assert.assertArrayEquals(realTime, expectedTIme);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user