forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoundRobin.java
More file actions
141 lines (120 loc) · 5 KB
/
RoundRobin.java
File metadata and controls
141 lines (120 loc) · 5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
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 < 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;
}
//initialize an array list to emulate the queue of ready processes
ArrayList<Integer> readyQueue = new ArrayList<>();
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;
//calculate waiting times while there are uncompleted processes
while (!readyQueue.isEmpty()) {
//check if a process has finished
if (executeTIme[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 < n; i++) {
turnAroundTime[i] = waitingTime[i] + burstTime[i];
}
//return the turnaround time table
return turnAroundTime;
}
/**
* 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
*/
void printAvgTimes(int[] burstTime, int quantum) {
int n = burstTime.length;
int totalWaitingTime = 0;
int totalTurnAroundTime = 0;
// Find waiting time of all processes
int[] waitingTime = calcWaitingTime(burstTime, quantum);
// Find turn around time for all processes
int[] turnAroundTime = calcTurnAroundTime(burstTime, waitingTime);
// Display processes along with all details
System.out.println("Process " + " Burst Time " +
" Waiting Time " + " Turnaround Time");
System.out.println("======= ========== ============ ===============");
// Calculate total waiting time and total turn around time
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("\nAverage waiting time = " +
(float) totalWaitingTime / (float) n);
System.out.println("Average turnaround time = " +
(float) totalTurnAroundTime / (float) n);
}
}