-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimpleScheduler.c
More file actions
424 lines (386 loc) · 12.2 KB
/
simpleScheduler.c
File metadata and controls
424 lines (386 loc) · 12.2 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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
//header files
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <signal.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <semaphore.h>
//definitions
#define MAX_SIZE 50
#define MAX_HISTORY 75
#define MAX_SUBMIT 25
//struct to store process info
struct Process{
int pid, priority;
bool submit,queue,completed; // flags
// submit: process have been submitted
// queue: process is in the scheduler's queue
// completed: indicates if process have been completed
char command[MAX_SIZE + 1]; //+1 to accomodate \n or \0
struct timeval start;
unsigned long execution_time, wait_time, vruntime;
};
//history struct used ot store the history of process executions
struct history_struct {
int history_count,ncpu,tslice;
sem_t mutex;
struct Process history[MAX_HISTORY];
};
// struct for queue data structure
struct queue{
int head,tail,capacity,curr;
struct Process **table;
};
// struct for priority queue data structure
struct pqueue{
int size,capacity;
struct Process **heap;
};
//function declarations
void scheduler(int ncpu, int tslice);
static void my_handler(int signum);
void terminate();
void start_time(struct timeval *start);
unsigned long end_time(struct timeval *start);
bool queue_empty(struct queue *q);
int next_head(struct queue *q);
int next_tail(struct queue *q);
bool queue_full(struct queue *q);
void enqueue(struct queue *q, struct Process *proc);
void dequeue(struct queue *q);
bool pqueue_empty(struct pqueue *pq);
bool pqueue_full(struct pqueue *pq);
void swap(struct Process* a, struct Process* b);
void heapifyUp(struct pqueue* pq, int index);
void heapifyDown(struct pqueue* pq, int index); //min-heapify
void penqueue(struct pqueue *pq, struct Process *proc); //min-heap-insert
struct Process* pdequeue(struct pqueue *pq); //min-heap-extract-min
//global variables
int shm_fd;
bool term = false;
struct history_struct *process_table;
struct queue *running_q;
struct pqueue *ready_q;
int main(){
//signal part to handle ctrl c (from lecture 7)
struct sigaction sig;
if (memset(&sig, 0, sizeof(sig)) == 0){
perror("memset");
exit(1);
}
sig.sa_handler = my_handler;
if (sigaction(SIGINT, &sig, NULL) == -1){
perror("sigaction");
exit(1);
}
//accessing the shm in read-write mode
shm_fd = shm_open("shm", O_RDWR, 0666);
if (shm_fd == -1){
perror("shm_open");
exit(1);
}
process_table = mmap(NULL, sizeof(struct history_struct), PROT_READ|PROT_WRITE, MAP_SHARED, shm_fd,0);
if (process_table == MAP_FAILED){
perror("mmap");
exit(1);
}
int ncpu = process_table->ncpu;
int tslice = process_table->tslice;
//initialising ready priority queue
ready_q = (struct pqueue *) (malloc(sizeof(struct pqueue)));
if (ready_q == NULL){
perror("malloc");
exit(1);
}
ready_q->size = 0;
ready_q->capacity = MAX_SUBMIT;
ready_q->heap = (struct Process **) malloc(ready_q->capacity * sizeof(struct Process));
if (ready_q->heap == NULL){
perror("malloc");
exit(1);
}
for (int i=0; i<ready_q->capacity; i++){
ready_q->heap[i] = (struct Process *)malloc(sizeof(struct Process));
if (ready_q->heap[i] == NULL){
perror("malloc");
exit(1);
}
}
//initialising running queue
running_q = (struct queue *) (malloc(sizeof(struct queue)));
if (running_q == NULL){
perror("malloc");
exit(1);
}
running_q->head = running_q->tail = running_q->curr = 0;
running_q->capacity = ncpu+1;
running_q->table = (struct Process **) malloc(running_q->capacity * sizeof(struct Process));
if (running_q->table == NULL){
perror("malloc");
exit(1);
}
for (int i=0; i<running_q->capacity; i++){
running_q->table[i] = (struct Process *)malloc(sizeof(struct Process));
if (running_q->table[i] == NULL){
perror("malloc");
exit(1);
}
}
// initialising a semaphore
if (sem_init(&process_table->mutex, 1, 1) == -1){
perror("sem_init");
exit(1);
}
//creating daemon process
if(daemon(1, 1)){
perror("daemon");
exit(1);
}
scheduler(ncpu, tslice);
//cleanup for mallocs
for (int i=running_q->capacity-1; i<0; i--) {
free(running_q->table[i]);
}
free(running_q->table);
free(running_q);
for (int i=ready_q->capacity-1; i<0; i--){
free(ready_q->heap[i]);
}
free(ready_q->heap);
free(ready_q);
// destroying the semaphore
if (sem_destroy(&process_table->mutex) == -1){
perror("shm_destroy");
exit(1);
}
// unmapping shared memory segment followed by a "close" call
if (munmap(process_table, sizeof(struct history_struct)) < 0){
printf("Error unmapping\n");
perror("munmap");
exit(1);
}
if (close(shm_fd) == -1){
perror("close");
exit(1);
}
return 0;
}
// scheduler function for scheduling and managing processes
void scheduler(int ncpu, int tslice){
while(true){
unsigned int remaining_sleep = sleep(tslice / 1000);
if (remaining_sleep > 0){
printf("Sleep was interrupted after %u seconds\n", remaining_sleep);
exit(1);
}
if (sem_wait(&process_table->mutex) == -1){
perror("sem_wait");
exit(1);
}
//this if-block ensures that scheduler terminates after natural termination of all processes
if (term && queue_empty(running_q) && pqueue_empty(ready_q)){
terminate();
}
//adding process to ready queue if they have submit keyword
for (int i=0; i<process_table->history_count; i++){
if (process_table->history[i].submit==true && process_table->history[i].completed==false && process_table->history[i].queue==false){
if (ready_q->size+ncpu < ready_q->capacity-1){
process_table->history[i].queue=true;
penqueue(ready_q, &process_table->history[i]);
}
else{
break;
}
}
}
//checking running queue and pausing the processes if they haven't terminated
if (!queue_empty(running_q)){
for (int i=0; i<ncpu; i++){
if (!queue_empty(running_q)){
if (!running_q->table[running_q->head]->completed){
penqueue(ready_q, running_q->table[running_q->head]);
running_q->table[running_q->head]->execution_time += end_time(&running_q->table[running_q->head]->start);
running_q->table[running_q->head]->vruntime += running_q->table[running_q->head]->execution_time *running_q->table[running_q->head]->priority;
start_time(&running_q->table[running_q->head]->start);
if (kill(running_q->table[running_q->head]->pid, SIGSTOP) == -1){
perror("kill");
exit(1);
}
dequeue(running_q);
}
else{
dequeue(running_q);
}
}
}
}
//adding processes to running queue and resume their execution
if (!pqueue_empty(ready_q)){
for (int i=0; i<ncpu; i++){
if (!pqueue_empty(ready_q)){
struct Process *proc = pdequeue(ready_q);
proc->wait_time += end_time(&proc->start);
start_time(&proc->start);
if (kill(proc->pid, SIGCONT) == -1){
perror("kill");
exit(1);
}
enqueue(running_q, proc);
}
}
}
if (sem_post(&process_table->mutex) == -1){
perror("sem_post");
exit(1);
}
}
}
//signal handler
static void my_handler(int signum){
// handling SIGINT signal for termination
if(signum == SIGINT){
term = true;
}
}
//function to terminate scheduler
void terminate(){
printf("\nCaught SIGINT signal for termination\n");
printf("Terminating simple scheduler...\n");
//cleanups for malloc
for (int i=running_q->capacity-1; i<0; i--) {
free(running_q->table[i]);
}
free(running_q->table);
free(running_q);
for (int i=ready_q->capacity-1; i<0; i--){
free(ready_q->heap[i]);
}
free(ready_q->heap);
free(ready_q);
// destroying the semaphore
if (sem_destroy(&process_table->mutex) == -1){
perror("shm_destroy");
exit(1);
}
// unmapping shared memory segment followed by a "close" call
if (munmap(process_table, sizeof(struct history_struct)) < 0){
printf("Error unmapping\n");
perror("munmap");
exit(1);
}
if (close(shm_fd) == -1){
perror("close");
exit(1);
}
exit(0);
}
//function to note start time
void start_time(struct timeval *start){
gettimeofday(start, 0);
}
//function to get time duration since start time
unsigned long end_time(struct timeval *start){
struct timeval end;
unsigned long t;
gettimeofday(&end, 0);
t = ((end.tv_sec*1000000) + end.tv_usec) - ((start->tv_sec*1000000) + start->tv_usec);
return t/1000;
}
//queue methods
bool queue_empty(struct queue *q){
return q->head == q->tail;
}
int next_head(struct queue *q){
if (q->head == q->capacity-1){
return 0;
}
return q->head+1;
}
int next_tail(struct queue *q){
if (q->tail == q->capacity-1){
return 0;
}
return q->tail+1;
}
bool queue_full(struct queue *q){
return next_tail(q) == q->head;
}
void enqueue(struct queue *q, struct Process *proc){
if (queue_full(q)){
printf("queue overflow, upper cap of 20 jobs at once\n");
return;
}
q->curr++;
q->table[q->tail] = proc;
q->tail = next_tail(q);
}
void dequeue(struct queue *q){
if (queue_empty(q)){
printf("queue underflow\n");
return;
}
q->curr--;
q->head = next_head(q);
}
//pqueue methods
bool pqueue_empty(struct pqueue *pq){
return pq->size == 0;
}
bool pqueue_full(struct pqueue *pq){
return pq->size == pq->capacity;
}
void swap(struct Process* a, struct Process* b){
struct Process temp = *a;
*a = *b;
*b = temp;
}
void heapifyUp(struct pqueue* pq, int index){
while (index>0){
int parent = (index-1)/2;
if (pq->heap[index]->vruntime < pq->heap[parent]->vruntime){
swap(pq->heap[index], pq->heap[parent]);
index = parent;
}
else{
break;
}
}
}
void heapifyDown(struct pqueue* pq, int index){
int leftChild = 2*index + 1;
int rightChild = 2*index + 2;
int smallest = index;
if (leftChild<pq->size && pq->heap[leftChild]->vruntime < pq->heap[smallest]->vruntime){
smallest = leftChild;
}
if (rightChild<pq->size && pq->heap[rightChild]->vruntime < pq->heap[smallest]->vruntime){
smallest = rightChild;
}
if (smallest != index){
swap(pq->heap[index], pq->heap[smallest]);
heapifyDown(pq, smallest);
}
}
void penqueue(struct pqueue *pq, struct Process *proc){
if (pq->size < pq->capacity){
pq->heap[pq->size] = proc;
heapifyUp(pq, pq->size);
pq->size++;
}
}
struct Process* pdequeue(struct pqueue *pq){
if (pq->size>0){
struct Process* removed = pq->heap[0];
pq->heap[0] = pq->heap[pq->size - 1];
pq->size--;
heapifyDown(pq, 0);
return removed;
}
return NULL;
}