-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRoundRobin.java
53 lines (53 loc) · 1.91 KB
/
RoundRobin.java
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
import java.util.*;
public class RoundRobin {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter no. of processes in the system: ");
int n = sc.nextInt();
Process p[] = new Process[n];
for (int i = 0; i < n; i++) {
p[i] = new Process();
p[i].pid = i;
System.out.print("Enter arrival time and burst time for P" + i + ": ");
p[i].at = sc.nextInt();
p[i].bt = sc.nextInt();
p[i].rt = p[i].bt;
}
System.out.print("Enter time quantum: ");
int tq = sc.nextInt();
Arrays.sort(p, Comparator.comparing(pr -> pr.at));
double awt = 0, att = 0;
System.out.println("Process\t\tArrival Time\tBurst Time\tWaiting Time\tTurnaround Time");
for (int i = 0, time = 0, x = n, flag = 0; x != 0; ) {
if (p[i].rt <= tq && p[i].rt > 0) {
time += p[i].rt;
p[i].rt = 0;
flag = 1;
} else if (p[i].rt > 0) {
p[i].rt -= tq;
time += tq;
}
if (p[i].rt == 0 && flag == 1) {
x--;
p[i].wt = time - p[i].at - p[i].bt;
p[i].tt = time - p[i].at;
awt += p[i].wt;
att += p[i].tt;
flag = 0;
System.out.println("P" + p[i].pid + "\t\t\t\t" + p[i].at + "\t\t\t\t" + p[i].bt + "\t\t\t\t" + p[i].wt +
"\t\t\t\t" + p[i].tt);
}
if (i == n - 1) {
i = 0;
} else if (p[i + 1].at <= time) {
i++;
} else {
i = 0;
}
}
awt /= (double) n;
att /= (double) n;
System.out.println("Average waiting time: " + awt);
System.out.println("Average turn around time: " + att);
}
}