-
Notifications
You must be signed in to change notification settings - Fork 3
/
RR_Scheduler_Function.cs
210 lines (176 loc) · 5.34 KB
/
RR_Scheduler_Function.cs
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
198
199
200
201
202
203
204
205
206
207
208
209
210
using System;
using System.Collections.Generic;
using System.Linq;
class Proc
{
public int index;
public float arrival;
public float burst_time;
public Proc(int i, float value, float b)
{
index = i;
arrival = value;
burst_time = b;
}
}
public class RR_Scheduler_Function
{
public static int quantum;
public static int prcs_no;
public static float[] arrival_time;
public static float[] cpu_brustTime;
public static int[] prc;
public static float[] start;
public static float[] end;
public static float[] wait;
public static int last;
public const int MAXX = 1000;
public static List<JobsScheduler.outputProcesses> outputList;
public static float avg_wait;
public static void RRSchedulerFunction(List<JobsScheduler.Process> processes, int q)
{
prcs_no = processes.Count;
arrival_time = new float[MAXX];
cpu_brustTime = new float[MAXX];
for (int i = 0; i < prcs_no; i++)
{
arrival_time[i] = processes[i].arrivalTime;
cpu_brustTime[i] = processes[i].burstTime;
}
prc = new int[MAXX];
start = new float[MAXX];
end = new float[MAXX];
wait = new float[MAXX];
quantum = q;
for (int i = 0; i < MAXX; i++)
{
prc[i] = -1;
start[i] = 0;
end[i] = 0;
wait[i] = 0;
}
List<Proc> p = new List<Proc>(prcs_no);
for (int i = 0; i < prcs_no; i++)
{
int x = (int)Char.GetNumericValue((processes[i].processNumber)[1]);
p.Add(new Proc(x, arrival_time[i], cpu_brustTime[i]));
}
sort(p);
float clk = 0.0f;
int idx = 0;
while (p.Count != 0)
{
List<Proc> ready = new List<Proc>(prcs_no);
if (p.First().arrival > clk)
{
start[idx] = clk;
end[idx] = p.First().arrival;
prc[idx] = -1;
idx++;
clk = p.First().arrival;
}
for (int i = 0, j = 0; j < p.Count; i++)
{
if (p[j].arrival <= clk)
{
ready.Add(p[j]);
p.RemoveAt(j);
}
else j++;
}
while (ready.Count != 0)
{
Proc readyProcess = ready[0];
ready.RemoveAt(0);
if (quantum < readyProcess.burst_time)
{
start[idx] = clk;
end[idx] = clk + quantum;
prc[idx] = readyProcess.index;
clk += quantum;
readyProcess.burst_time -= quantum;
for (int i = 0, j = 0; j < p.Count; i++)
{
if (p[j].arrival <= clk)
{
ready.Add(p[j]);
p.RemoveAt(j);
}
else j++;
}
ready.Add(readyProcess);
idx++;
}
else
{
start[idx] = clk;
end[idx] = clk + readyProcess.burst_time;
prc[idx] = readyProcess.index;
clk += readyProcess.burst_time;
for (int i = 0, j = 0; j < p.Count; i++)
{
if (p[j].arrival <= clk)
{
ready.Add(p[j]);
p.RemoveAt(j);
}
else j++;
}
idx++;
}
}
}
for (int j = 0; j < prcs_no; j++)
{
bool k = false;
float w = 0.0f;
for (int i = 0; i <= idx; i++)
{
if ((prc[i] - 1) == j)
{
if (!k)
{
wait[j] = start[i] - arrival_time[j];
k = true;
w = end[i];
}
else if (k)
{
wait[j] += start[i] - w;
w = end[i];
}
}
}
}
avg_wait = 0.0f;
for (int i = 0; i < prcs_no; i++)
avg_wait += wait[i];
avg_wait /= prcs_no;
last = idx;
outputList = new List<JobsScheduler.outputProcesses>();
for (int i = 0; i < last; i++)
{
JobsScheduler.outputProcesses op = new JobsScheduler.outputProcesses();
if (prc[i] == -1) op.processNumber = "idle";
else op.processNumber = "P" + prc[i];
op.startTime = start[i];
op.usingTime = end[i] - start[i];
outputList.Add(op);
}
}
static void sort(List<Proc> l)
{
for (int i = 0; i < l.Count; i++)
{
for (int j = 0; j < l.Count; j++)
{
if (l[i].arrival < l[j].arrival)
{
Proc temp = l[i];
l[i] = l[j];
l[j] = temp;
}
}
}
}
}