-
Notifications
You must be signed in to change notification settings - Fork 0
/
RoundRobinScheduling
197 lines (174 loc) · 5.45 KB
/
RoundRobinScheduling
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package laplichroundrobin;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
public class LapLichRoundRobin{
private static Scanner inp = new Scanner(System.in);
//Driver Code
public static void main(String[] args){
int ip_n = 0,ip_tq = 0;
int ip_arrival[] = new int[20];
int ip_burst[] = new int[20];
try {
File f = new File("Input.txt");
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);
String str;
int i = 1;
while((str = br.readLine())!=null)
{
if(i==1)
{ //get time quantum and number of processes
String obj[] = str.split(" ");
ip_tq = Integer.parseInt(obj[0]);
ip_n = Integer.parseInt(obj[1]);
i++;
continue;
}
if(i==2)
{
//get the arrival time of the processes
String ob[] = str.split(" ");
for(int k = 0; k < ob.length; k++)
ip_arrival[k] = Integer.parseInt(ob[k]);
i++;
continue;
}
if(i==3)
{
//get the burst time of the processes
String oj[] = str.split(" ");
for(int l = 0; l < oj.length; l++)
{
ip_burst[l] = Integer.parseInt(oj[l]);
}
}
}
br.close();
fr.close();
} catch (IOException e) {
}
int n = 0,tq, timer = 0, maxProccessIndex = 0;
float avgWait = 0, avgTT = 0;
//get the timequantum from input
tq = ip_tq;
//get the number of processes from input
n = ip_n;
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];
//get the arrival time from input
for(int i = 0; i < n; i++)
arrival[i] = ip_arrival[i];
//get the burst time from input
for(int i = 0; i < n; i++){
burst[i] = ip_burst[i];
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;
}
}
}