-
Notifications
You must be signed in to change notification settings - Fork 0
/
Roundrobin2.java
141 lines (126 loc) · 3.67 KB
/
Roundrobin2.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
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
141
import java.util.*;
public class Roundrobin{
private static Scanner inp = new Scanner(System.in);
public static void main(String[] args){
int n,tq, timer = 0, maxProccessIndex = 0;
float avgWait = 0, avgTT = 0;
System.out.print("\nEnter the time quanta : ");
tq = inp.nextInt();
System.out.print("\nEnter the number of processes : ");
n = inp.nextInt();
int arrival[] = new int[n];
int burst[] = new int[n];
int wait[] = new int[n];
int turn[] = new int[n];
int queue[] = new int[n];
int temp_burst[] = new int[n];
boolean complete[] = new boolean[n];
System.out.print("\nEnter the arrival time of the processes : ");
for(int i = 0; i < n; i++)
arrival[i] = inp.nextInt();
System.out.print("\nEnter the burst time of the processes : ");
for(int i = 0; i < n; i++){
burst[i] = inp.nextInt();
temp_burst[i] = burst[i];
}
for(int i = 0; i < n; i++){ //Initializing the queue and complete array
complete[i] = false;
queue[i] = 0;
}
while(timer < arrival[0]) //Incrementing Timer until the first process arrives
timer++;
queue[0] = 1;
while(true){
boolean flag = true;
for(int i = 0; i < n; i++){
if(temp_burst[i] != 0){
flag = false;
break;
}
}
if(flag)
break;
for(int i = 0; (i < n) && (queue[i] != 0); i++){
int ctr = 0;
while((ctr < tq) && (temp_burst[queue[0]-1] > 0)){
temp_burst[queue[0]-1] -= 1;
timer += 1;
ctr++;
//Updating the ready queue until all the processes arrive
checkNewArrival(timer, arrival, n, maxProccessIndex, queue);
}
if((temp_burst[queue[0]-1] == 0) && (complete[queue[0]-1] == false)){
turn[queue[0]-1] = timer; //turn currently stores exit times
complete[queue[0]-1] = true;
}
//checks whether or not CPU is idle
boolean idle = true;
if(queue[n-1] == 0){
for(int k = 0; k < n && queue[k] != 0; k++){
if(complete[queue[k]-1] == false){
idle = false;
}
}
}
else
idle = false;
if(idle){
timer++;
checkNewArrival(timer, arrival, n, maxProccessIndex, queue);
}
//Maintaining the entries of processes after each premption in the ready Queue
queueMaintainence(queue,n);
}
}
for(int i = 0; i < n; i++){
turn[i] = turn[i] - arrival[i];
wait[i] = turn[i] - burst[i];
}
System.out.print("\nProgram No.\tArrival Time\tBurst Time\tWait Time\tTurnAround Time"
+ "\n");
for(int i = 0; i < n; i++){
System.out.print(i+1+"\t\t"+arrival[i]+"\t\t"+burst[i]
+"\t\t"+wait[i]+"\t\t"+turn[i]+ "\n");
}
for(int i =0; i< n; i++){
avgWait += wait[i];
avgTT += turn[i];
}
System.out.print("\nAverage wait time : "+(avgWait/n)
+"\nAverage Turn Around Time : "+(avgTT/n));
}
public static void queueUpdation(int queue[],int timer,int arrival[],int n, int maxProccessIndex){
int zeroIndex = -1;
for(int i = 0; i < n; i++){
if(queue[i] == 0){
zeroIndex = i;
break;
}
}
if(zeroIndex == -1)
return;
queue[zeroIndex] = maxProccessIndex + 1;
}
public static void checkNewArrival(int timer, int arrival[], int n, int maxProccessIndex,int queue[]){
if(timer <= arrival[n-1]){
boolean newArrival = false;
for(int j = (maxProccessIndex+1); j < n; j++){
if(arrival[j] <= timer){
if(maxProccessIndex < j){
maxProccessIndex = j;
newArrival = true;
}
}
}
if(newArrival) //adds the index of the arriving process(if any)
queueUpdation(queue,timer,arrival,n, maxProccessIndex);
}
}
public static void queueMaintainence(int queue[], int n){
for(int i = 0; (i < n-1) && (queue[i+1] != 0) ; i++){
int temp = queue[i];
queue[i] = queue[i+1];
queue[i+1] = temp;
}
}
}