-
Notifications
You must be signed in to change notification settings - Fork 0
/
scheduler.h
331 lines (263 loc) · 9.58 KB
/
scheduler.h
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
#pragma once
#include "headers.h"
#include "stack.h"
#include "SJF.h"
#include "hpf.h"
#include "MLFQ.h"
#include "RR.h"
#include "memManager.h"
int previousTimeUsage; // Time when we last utilized the CPU
int idleTime = 0; // Time CPU was idle in, initially = 0
memList free_list[11];
int waitingTime = 0; // waiting time of processes, initially = 0
int numProc = 0; // number of processes, initially = 0
double avgWTA = 0; // average weighted turnaround time, initially = 0
scheduling_algo algo;
FILE *outputStats; // File pointer to the output file, where stats will be written
FILE *memoryState; // File pointer to the memory output file, where stats will be written
void schedulerIsForContextSwitch();
void schedulerAreDead(int SIGNUM);
void schedulerPreempt(process *proc);
void schedulerResume(process *proc);
void getQuantumSize(){
printf("Please enter quantum size: \n");
fflush(stdout);
scanf("%d",&RRquanta);
if (RRquanta < 1)
{
printf("Invalid input entered. Taking default value of 2 instead.\n");
fflush(stdout);
RRquanta = 2;
}
}
void childAreDead(int SIGNUM)
{
// getting finished process
int procStatus;
int procID = wait(&procStatus);
printf("delete process %d\n", procID);
fflush(stdout);
process *proc = stackGetProcess(procID);
algo.removeProcess(algo.type, proc); // removing process from algorithm queue
numProc++; // increasing number of processes
int turnaroundTime = getClk() - proc->arrival;
double weightedTurnaroundTime = turnaroundTime / (double)proc->runtime;
// double avgWeightedTurnaroundTime = avgWTA; // temporary holder
avgWTA = (weightedTurnaroundTime + (numProc - 1) * avgWTA) / numProc;
// Method of calucation: similar to that of calculating the GPA
// getting the total WTA by multiplying the number of processes (excluding the terminating one)
// by the avgWTA. Then we add the new WTA, and divide by the total number of processes.
waitingTime += proc->waitingtime;
previousTimeUsage = getClk(); // updating the utilization time
if (proc->remainingtime < 0)
proc->remainingtime = 0;
// Printing process info
DeallocateMemory(free_list,proc->memStart, proc->memSize);
fprintf(memoryState, "At time %d freed %d bytes from process %d from %d to %d\n",
getClk(), BestSlot(proc->memSize), proc->id, proc->memStart, proc->memStart + BestSlot(proc->memSize) - 1);
fflush(memoryState);
fprintf(outputStats, "At time %d process %d finished arr %d total %d remain %d wait %d TA %d WTA %.2f\n",
getClk(), proc->id, proc->arrival, proc->runtime, proc->remainingtime, proc->waitingtime, turnaroundTime, weightedTurnaroundTime);
fflush(outputStats);
stackRemove(proc);
curentProcess = NULL;
printf("delete process %d\n", procID);
fflush(stdout);
schedulerIsForContextSwitch();
}
bool schedulerInitialize(int algo_num, int *msgq_id)
{
initializeMemoryManager(free_list);
display(&free_list[10]);
// Opening the file where we will output the process updates
outputStats = fopen("scheduler.log", "w");
// Printing the opening statement
fprintf(outputStats, "#At time x process y state arr w total z remain y wait k\n");
fflush(outputStats);
memoryState = fopen("memory.log", "w");
fprintf(memoryState, "#At time x allocated y bytes for process z from i to j\n");
fflush(memoryState);
/* sigaction to handle SIGCHLD signal
whenever a process dies, it calls childAreDead
(sigaction since SIGCHLD cannot be handled directly)
*/
struct sigaction chld_handle;
chld_handle.sa_handler = childAreDead;
sigemptyset(&chld_handle.sa_mask);
chld_handle.sa_flags = SA_NOCLDSTOP;
if (sigaction(SIGCHLD, &chld_handle, NULL) == -1)
{
perror("sigaction");
exit(1);
}
signal(SIGINT, schedulerAreDead);
initClk();
previousTimeUsage = 0;
// Calling the initializer of each algorithm
// SJF
if (algo_num == 1)
{
SJFflag = 1;
SJFInitialize(&algo);
}
// HPF
else if (algo_num == 2)
{
HPFInitialize(&algo);
}
// RR
else if (algo_num == 3)
{
getQuantumSize();
RRInitialize(&algo);
}
// multilevel
else if (algo_num == 4)
{
MLFQ_init(&algo);
}
stackInitialize(200);
// Creating the message queue
key_t key_id = ftok("keyfile", 65);
int msgq_idTemp = msgget(key_id, 0666 | IPC_CREAT);
if (msgq_idTemp == -1)
{
printf("Error in create msgq_idTemp");
fflush(stdout);
return false;
}
*msgq_id = msgq_idTemp;
return true;
}
bool getMessage(int msgq_id, msgBuf *message)
{
// Doesn't wait for the process generator to send message
int rec_val = msgrcv(msgq_id, message, sizeof(process), 0, IPC_NOWAIT);
if (rec_val == -1)
{
/* if (errno != ENOMSG) {
perror("Error in message queue\n");
exit(-1);
} */
return false;
}
return true;
}
void schedulerPreempt(process *proc)
{
printf("Preempt \n");
fflush(stdout);
if (!proc)
return;
printf("Preempt 2 %d\n", proc->pid);
fflush(stdout);
// printf("Prempted process %d\n",curentProcess->id);
proc->status = 3; // Stopped process
proc->StartedBefore = 1; // Marking the process as ran before
kill(proc->pid, SIGTSTP); // Stopping the process
// Printing process info
fprintf(outputStats, "At time %d process %d stopped arr %d total %d remain %d wait %d\n",
getClk(), proc->id, proc->arrival, proc->runtime, proc->remainingtime, proc->waitingtime);
fflush(outputStats);
previousTimeUsage = getClk(); // starting from now, till another process utilizes the CPU
// that will be the idle time.
}
void schedulerResume(process *proc)
{
printf("resume\n");
fflush(stdout);
// Setting the current running process
curentProcess = proc;
if (!proc)
return;
printf("resume %d\n", proc->pid);
fflush(stdout);
proc->status = 2; // Marking the status as running
// Continue the process, SIGCONT to update waiting times once the process is resumed
kill(proc->pid, SIGCONT);
// Printing process info
// Remember:
// int StartedBefore; // 1 means it was started before, 0 means it has never been started before
bool FirstTime = false; // a flag that shows whether process is resuming or starting
// false means resuming
if (proc->StartedBefore == 0) // then it is now starting
{
FirstTime = true;
int addr = AllocateMemory(free_list, proc->memSize);
fprintf(memoryState, "At time %d allocated %d bytes for process %d from %d to %d\n",
getClk(), BestSlot(proc->memSize), proc->id, addr, addr+ BestSlot(proc->memSize) - 1);
proc->memStart = addr;
fflush(memoryState);
}
fprintf(outputStats, "At time %d process %d %s arr %d total %d remain %d wait %d\n",
getClk(), proc->id, (FirstTime) ? "started" : "resumed", proc->arrival, proc->runtime, proc->remainingtime, proc->waitingtime);
fflush(outputStats);
// Updating the idleTime for the scheduler
idleTime += getClk() - previousTimeUsage;
previousTimeUsage = getClk();
}
void schedulerIsForContextSwitch()
{
printf("context\n");
fflush(stdout);
bool check = algo.preempt(algo.type);
if (check)
{
schedulerPreempt(curentProcess);
process *next = algo.getNextProcess(algo.type);
schedulerResume(next);
}
}
void CreateProcess(msgBuf *msg_buffer)
{
int pid = fork();
// Child (process)
if (pid == 0)
{
char pRemainingTime[10];
sprintf(pRemainingTime, "%d", msg_buffer->proc.remainingtime);
execl("process.out", "process.out", pRemainingTime, (char *)NULL);
}
else
{
msg_buffer->proc.pid = pid;
// Making the process stopped (once it enters) and check after if ready
kill(pid, SIGTSTP);
// insert in the stack
process *stackProcessEntry = stackInsert(&msg_buffer->proc);
// Using the chosen algorithm on the created process
algo.addProcess(algo.type, stackProcessEntry);
printf("Created process %d\n", msg_buffer->proc.id);
fflush(stdout);
schedulerIsForContextSwitch();
}
}
void schedulerAreDead(int SIGNUM)
{
printf("schedulerAreDead \n");
// closing the opened file
fclose(outputStats);
printf("schedulerAreDead \n");
fflush(stdout);
// Calculating the utilization stats
outputStats = fopen("schedulerPerf.perf", "w");
printf("schedulerAreDead%d \n",idleTime );
fflush(stdout);
double utilization = 1 - ((double)idleTime / getClk()); // calculates the utilization percentage
double avgWaitingTime = (double)waitingTime / numProc;
// Printing the utilization stats in another file
fprintf(outputStats, "CPU utilization = %.2f%%\n", utilization * 100);
fprintf(outputStats, "Avg WTA = %.2f\n", avgWTA);
fprintf(outputStats, "Avg Waiting = %.2f\n", avgWaitingTime);
fflush(outputStats);
fclose(outputStats);
// TODO: free the rest of the stuff here
stackFree();
algo.free(algo.type); // free current algorithm
// Removing message queue
key_t key_id = ftok("keyfile", 65);
int msgq_id = msgget(key_id, 0666 | IPC_CREAT);
msgctl(msgq_id, IPC_RMID, (struct msqid_ds *)0);
// Installing the signal handler again to trap SIGINT again
signal(SIGINT, schedulerAreDead);
}