Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit56a611a

Browse files
Merge pull requestTheAlgorithms#1462 from geogiadim/Development
Implementation for Round Robin Algorithm in Java with tests
2 parents3c71071 +be160de commit56a611a

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+
packagesrc.main.java.com.others;
2+
3+
importjava.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+
publicclassRoundRobin {
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+
publicint[]calcWaitingTime(int[]burstTime,intquantum) {
19+
intn =burstTime.length;
20+
//create a copy of burstTime table to executeTime table
21+
int[]executeTIme =newint[n];
22+
for (inti =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 =newint[n];
28+
for (inti =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 =newArrayList<>();
34+
for (inti =0;i <n;i++) {
35+
readyQueue.add(i);
36+
}
37+
38+
//the total time that processes need to be finished
39+
inttime =0;
40+
inti =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+
}elseif (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+
returnwaitingTime;
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+
publicint[]calcTurnAroundTime(int[]burstTime,int[]waitingTime) {
92+
intn =burstTime.length;
93+
//initialize the turnaround time table
94+
int[]turnAroundTime =newint[n];
95+
96+
//calculate turnaround time for each process (T.T= W.T + B.T)
97+
for (inti =0;i <n;i++) {
98+
turnAroundTime[i] =waitingTime[i] +burstTime[i];
99+
}
100+
101+
//return the turnaround time table
102+
returnturnAroundTime;
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+
voidprintAvgTimes(int[]burstTime,intquantum) {
113+
intn =burstTime.length;
114+
inttotalWaitingTime =0;
115+
inttotalTurnAroundTime =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 (inti =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+
packagesrc.test.java.com.others;
2+
3+
importorg.junit.Assert;
4+
importorg.junit.Test;
5+
6+
importsrc.main.java.com.others.RoundRobin;
7+
8+
publicclassRoundRobinTest {
9+
privatefinalint[]burstTime = {5,15,4,3};
10+
privatefinalRoundRobinroundRobin =newRoundRobin();
11+
12+
@Test
13+
publicvoidtestWaitingTime() {
14+
int[]expectedTime = {9,12,14,9};
15+
int[]realtime =roundRobin.calcWaitingTime(burstTime,3);
16+
Assert.assertArrayEquals(realtime,expectedTime);
17+
}
18+
19+
@Test
20+
publicvoidtestTurnAroundTIme() {
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)

[8]ページ先頭

©2009-2025 Movatter.jp