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