Skip to content

Commit 56a611a

Browse files
Merge pull request TheAlgorithms#1462 from geogiadim/Development
Implementation for Round Robin Algorithm in Java with tests
2 parents 3c71071 + be160de commit 56a611a

File tree

2 files changed

+167
-0
lines changed

2 files changed

+167
-0
lines changed
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
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+
* @return an array with waiting time for all processes
17+
*/
18+
public int[] calcWaitingTime(int[] burstTime, int quantum) {
19+
int n = burstTime.length;
20+
//create a copy of burstTime table to executeTime table
21+
int[] executeTIme = new int[n];
22+
for (int i = 0; i < n; i++) {
23+
executeTIme[i] = burstTime[i];
24+
}
25+
26+
//initialize the waiting time table and set all waiting times equal to zero
27+
int[] waitingTime = new int[n];
28+
for (int i = 0; i < n; i++) {
29+
waitingTime[i] = 0;
30+
}
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+
38+
//the total time that processes need to be finished
39+
int time = 0;
40+
int i = 0;
41+
//calculate waiting times while there are uncompleted processes
42+
while (!readyQueue.isEmpty()) {
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) {
76+
i = 0;
77+
}
78+
}
79+
80+
return waitingTime;
81+
}
82+
83+
84+
/**
85+
* This method calculates turn around time for all processes
86+
*
87+
* @param burstTime an array with burst time for all processes
88+
* @param waitingTime an array with waiting time for all processes
89+
* @return an array with turnaround time for all processes
90+
*/
91+
public int[] calcTurnAroundTime(int[] burstTime, int[] waitingTime) {
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+
101+
//return the turnaround time table
102+
return turnAroundTime;
103+
}
104+
105+
106+
/**
107+
* This method prints the results and calculates the average waiting and turnaround times
108+
*
109+
* @param burstTime an array with burst time for all processes
110+
* @param quantum the quantum quantity
111+
*/
112+
void printAvgTimes(int[] burstTime, int quantum) {
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+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package src.test.java.com.others;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
6+
import src.main.java.com.others.RoundRobin;
7+
8+
public class RoundRobinTest {
9+
private final int[] burstTime = {5, 15, 4, 3};
10+
private final RoundRobin roundRobin = new RoundRobin();
11+
12+
@Test
13+
public void testWaitingTime() {
14+
int[] expectedTime = {9, 12, 14, 9};
15+
int[] realtime = roundRobin.calcWaitingTime(burstTime, 3);
16+
Assert.assertArrayEquals(realtime, expectedTime);
17+
}
18+
19+
@Test
20+
public void testTurnAroundTIme() {
21+
int[] expectedTIme = {14, 27, 18, 12};
22+
int[] waitingTime = {9, 12, 14, 9};
23+
int[] realTime = roundRobin.calcTurnAroundTime(burstTime, waitingTime);
24+
Assert.assertArrayEquals(realTime, expectedTIme);
25+
}
26+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy