-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscheduler.cpp
More file actions
380 lines (356 loc) · 12.7 KB
/
scheduler.cpp
File metadata and controls
380 lines (356 loc) · 12.7 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
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/*
Student : Jung Hyun Sohn
ID : 30017825
TA: Sina
CPSC457
Assignment 3
Simulates execution of processes using non-preemptive shortest-job-first and preemptive round-robin.
*/
#include<iostream>
#include<fstream>
#include<string> // for string class
#define MAX_NUMBER 30
using namespace std;
int queue[100], num = 100, front = - 1, rear = - 1;
typedef struct {
int arrivalTime;
int burstTime;
int status;//0 = not ini / finished, 1 = ready, 2 = running
double waitTime;
} Process;
//used queues taught from this tutorial. The next three methods are with queues
//https://www.tutorialspoint.com/cplusplus-program-to-implement-queue-using-array
void ins(int ele){
int value;
if (rear == num - 1){
cout<<"Queue Error, too much"<<endl;
}else {
if (front == -1){
front = 0;
}
value = ele;
rear++;
queue[rear] = value;
}
}
void del(){
if (front == -1 || front>rear) {
cout<<"Queue Error, too little ";
return ;
} else {
front++;
}
}
void display(){
if (front == - 1)
cout<<"Queue is empty";
else {
for (int i = front; i <= rear; i++)
cout<<queue[i]<<" ";
}
}
void RR(Process *myProc, int numProcess, int totalTime, int timeQuantum ){
int currentProcess = -1;
int quantum = timeQuantum;
bool changed = false;
cout<<"Time ";
for(int i=0;i<numProcess;i++){
printf("P%d\t", i);
}
cout<<"\n---------------------------------------------------------------\n";
for(int i = 0; i<totalTime;i++){
printf(" %2d ", i);
for(int j=0;j<numProcess;j++){
//printf("tester %d ", myProc[j].burstTime);
if(myProc[j].status == 0){
//if its not done
if(myProc[j].burstTime>0){
//if the arrival time is this specific time then ready it
if(myProc[j].arrivalTime == i){
//if theres nothing else then immediately goes to running
//printf("test");
if(currentProcess == -1){
myProc[j].status = 2;
ins(j);
currentProcess = j;
printf(".\t");
myProc[j].burstTime--;
quantum--;
}else{
if(!changed){
ins(j);
//changed = false;
}else{
changed = false;
}
myProc[j].status = 1;
myProc[j].waitTime++;
printf("+\t");
}
}else printf(" \t");
}else{ //it is fully done
printf(" \t");
}
}else if(myProc[j].status == 1){
//if the process is ready state
//if there is no current process currently
//printf("lop");
if(currentProcess == j){
//if currentProcess changed to be this process
myProc[j].status = 2;
//currentProcess = j;
printf(".\t");
myProc[j].burstTime--;
quantum--;
}else{//if there is a current process running already
myProc[j].waitTime++;
printf("+\t");
}
//if the process is running
}else if(myProc[j].status == 2 ){
//if there is still fuel left then stay as current process
//and decrement the bursttime, otherwise change it to done
//printf("check");
if(myProc[j].burstTime > 1){
if(quantum>1){
//currentProcess = j;
printf(".\t");
quantum--;
myProc[j].burstTime--;
}else if(quantum<=1){
for(int p = j+1; p < numProcess;p++){
if(myProc[p].status == 0 && myProc[p].arrivalTime ==i){
changed = true;
ins(p);
}
}
del();
ins(j);
myProc[j].status = 1;
//currentProcess = queue[front];
//cout<<currentProcess;
myProc[j].burstTime--;
quantum = timeQuantum;
printf(".\t");
}
}else{
del();
myProc[j].status = 0;
quantum = timeQuantum;
printf(".\t");
}
}
}
if (currentProcess != -1) currentProcess = queue[front];
//display();
//printf("quantum: %d", quantum);
printf("\n");
}
myProc[0].waitTime--;
cout<<"\n---------------------------------------------------------------\n";
double avgWaitTime=0;
for(int p = 0;p<numProcess;p++){
printf("P%d waited %.3f sec\n", p, myProc[p].waitTime);
avgWaitTime+=myProc[p].waitTime;
}
//printf("Total wait time: %f secs", )
avgWaitTime = avgWaitTime/numProcess;
printf("Average Waiting Time = %.3f sec", avgWaitTime);
cout<<endl;
}
//SJF scheduler process, needing the process array struct, number of
// processes and the total amount of time.
void SJF(Process *myProc, int numProcess, int totalTime ){
int currentProcess = -1;
cout<<"Time ";
for(int i=0;i<numProcess;i++){
printf("P%d\t", i);
}
cout<<"\n---------------------------------------------------------------\n";
int smallest;
int smallIni = 100;;
for(int i=0;i<totalTime;i++){
printf(" %2d ", i);
//this for statement is only to find in the very beginning which
//is the shortest job when the arrival times are equal
for(int k=0; k<numProcess;k++){
if(myProc[k].status == 0){
if(myProc[k].arrivalTime == i){
if(myProc[k].burstTime<smallIni){
smallIni = myProc[k].burstTime;
//cout<<"smallest ini"<<smallIni<<endl;
}
}
}
}
for(int j=0;j<numProcess;j++){
//if process is not here or done
//printf("smallest : %d\n", smallest);
if(myProc[j].status == 0){
//if its not done
if(myProc[j].burstTime>0){
//if the arrival time is this specific time then ready it
if(myProc[j].arrivalTime == i){
//if theres nothing else then immediately goes to running
if(currentProcess == -1){
if(myProc[j].burstTime == smallIni){
myProc[j].status = 2;
currentProcess = j;
printf(".\t");
smallest = myProc[j].burstTime;
myProc[j].burstTime--;
}else{
myProc[j].status = 1;
//printf("Wait time: %d", myProc[j].waitTime);
myProc[j].waitTime++;
//printf("Wait time: %d", myProc[j].waitTime);
printf("+\t");
}
}else{
myProc[j].status = 1;
//printf("Wait time: %d", myProc[j].waitTime);
myProc[j].waitTime++;
//printf("Wait time: %d", myProc[j].waitTime);
printf("+\t");
}
}else{
printf(" \t");
}
}else{ //it is fully done
printf(" \t");
}
}else if(myProc[j].status == 1){
//if the process is ready state
//if there is no current process currently
if(currentProcess == -1){
//check if there is a process with a smaller amount of burstTime
if(myProc[j].burstTime==smallest){
myProc[j].status = 2;
currentProcess = j;
printf(".\t");
myProc[j].burstTime--;
}else{
myProc[j].waitTime++;
printf("+\t");
}
}else{
//if there is a current process running already
myProc[j].waitTime++;
printf("+\t");
}
//if the process is running
}else if(myProc[j].status == 2 ){
//if there is still fuel left then stay as current process
//and decrement the bursttime, otherwise change it to done
if(myProc[j].burstTime>1){
currentProcess = j;
printf(".\t");
myProc[j].burstTime--;
}else{
myProc[j].status = 0;
currentProcess = -1;
smallest = 100;
myProc[j].burstTime--;
printf(".\t");
}
}
}
for(int k=0; k<numProcess;k++){
if(myProc[k].status == 1){
if(myProc[k].burstTime<smallest){
smallest = myProc[k].burstTime;
}
}
}
printf("\n");
}
myProc[0].waitTime--;
cout<<"\n---------------------------------------------------------------\n";
double avgWaitTime=0;
for(int p = 0;p<numProcess;p++){
printf("P%d waited %.3f sec\n", p, myProc[p].waitTime);
avgWaitTime+=myProc[p].waitTime;
}
//printf("Total wait time: %f secs", )
avgWaitTime = avgWaitTime/numProcess;
printf("Average Waiting Time = %.3f sec", avgWaitTime);
cout<<endl;
}
int main( int argc, char ** argv)
{
string process;
int timeQuantum;
ifstream myFile;
int x;
int i = 0;
int arrival;
int burst;
int totalTime=0;
int numProcess = 0;
bool temp = true;
if( argc != 3 && argc != 4) {
printf("Usage: process textfile RR/SJF <time quantum>(optional)\n");
exit(-1);
}
if( argc == 3){
process = argv[2];
if(process == "RR"){
printf("Missing time quantum argument. Try again\n");
exit(-1);
}
if(process != "SJF"){
printf("Input RR or SJF for scheduler, try again\n");
exit(-1);
}
}
if(argc == 4){
process = argv[2];
timeQuantum = atoi(argv[3]);
if(process == "SJF"){
printf("No time quantum component for SJF. Try again\n");
exit(-1);
}
if(process != "RR"){
printf("Input RR or SJF for scheduler, try again\n");
exit(-1);
}
}
myFile.open(argv[1]);
if(!myFile){
printf("Unable to open file\n");
exit(1); //terminate
}
Process myProc[MAX_NUMBER];
while(myFile >> x){
//i use temp to switch between the two columns. Temp = arrivaltime
if(temp){
myProc[i].arrivalTime = x;
temp = !temp;
//printf("process %d has arrival time: %d\n", i, myProc[i].arrivalTime);
}else{
myProc[i].burstTime = x;
temp = !temp;
numProcess++;
totalTime +=x;
i++;
//printf("process %d has burst time: %d\n", i, myProc[i].burstTime);
}
myProc[i].status = 0; //does it twice per process, not efficient but i want it in while loop
myProc[i].waitTime = 0;
}
//printf("Wait time: %d", myProc[0].waitTime);
myProc[0].waitTime++;
//printf("Wait time: %d", myProc[0].waitTime);
totalTime +=myProc[0].arrivalTime;
//cout<<"Number of processes: "<<numProcess<<endl;
//for(int i=0;i<numProcess;i++){
// printf("Process %d has arrival time: %d burst time: %d\n", i,myProc[i].arrivalTime, myProc[i].burstTime);
//}
if(process == "SJF"){
SJF(myProc, numProcess, totalTime);
}else if(process == "RR"){
RR(myProc, numProcess, totalTime, timeQuantum);
}
myFile.close();
return 0;
}