-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscheduler.c
More file actions
executable file
·131 lines (123 loc) · 2.85 KB
/
scheduler.c
File metadata and controls
executable file
·131 lines (123 loc) · 2.85 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
#define _GNU_SOURCE
#include "scheduler.h"
#include <stdlib.h>
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sched.h>
#include <sys/syscall.h>
#include <sys/types.h>
int cmpbyready(const void *a, const void *b) {
return ((proc *)a)->readyTime - ((proc *)b)->readyTime;
}
typedef struct Q{
int p;
struct Q *next;
}q;
q *head = NULL, *tail = NULL;
int isEmpty(){
if(head == NULL)
return 1;
return 0;
}
q * create(int n){
q *new = (q*)malloc(sizeof(q));
new->p = n;
new->next = NULL;
return new;
}
void enq(int n){
if(head == NULL){
head = tail = create(n);
}
else{
tail->next = create(n);
tail = tail->next;
}
return;
}
int deq(){
q *temp = head->next;
int p = head->p;
free(head);
head = temp;
return p;
}
void scheduler(proc *procarr, int procnum, int policy){
assignCPU(getpid(), 0);
wakeup(getpid());
qsort(procarr, procnum, sizeof(proc), cmpbyready);
int runningproc = -1, time = 0, waitingprocnum = 0, lastswitchtime = -1;
while(1){
for(;waitingprocnum < procnum && procarr[waitingprocnum].readyTime == time; waitingprocnum ++){
procarr[waitingprocnum].pid = execproc(procarr + waitingprocnum);
assignCPU(procarr[waitingprocnum].pid, 1);
block(procarr[waitingprocnum].pid);
printf("%s %d\n", procarr[waitingprocnum].name, procarr[waitingprocnum].pid);
fflush(stdout);
enq(waitingprocnum);
}
if(runningproc == -1 || procarr[runningproc].execTime <= 0){
if(runningproc != -1){
waitpid(procarr[runningproc].pid, NULL, 0);
}
runningproc = -1;
if(policy == FIFO || policy == RR){
if(!isEmpty()){
runningproc = deq();
lastswitchtime = time;
}
else
runningproc = -1;
}
else{
int min = 2147483647;
for(int i = 0; i < waitingprocnum; i ++){
if(procarr[i].execTime > 0 && procarr[i].execTime < min){
min = procarr[i].execTime;
runningproc = i;
}
}
}
if(runningproc == -1 && waitingprocnum == procnum){
break;
}
else if(runningproc != -1){
wakeup(procarr[runningproc].pid);
}
}
else if(policy == PSJF){
int newrunning = runningproc;
for(int i = 0 ; i < waitingprocnum; i ++){
if(procarr[i].execTime > 0 && procarr[newrunning].execTime > procarr[i].execTime)
newrunning = i;
}
if(newrunning != runningproc){
block(procarr[runningproc].pid);
runningproc = newrunning;
wakeup(procarr[runningproc].pid);
}
}
else if(policy == RR){
if(time - lastswitchtime == RRcycle){
block(procarr[runningproc].pid);
enq(runningproc);
if(!isEmpty()){
runningproc = deq();
}
else
runningproc = -1;
lastswitchtime = time;
if(runningproc != -1)
wakeup(procarr[runningproc].pid);
}
}
if(runningproc != -1)
procarr[runningproc].execTime --;
//fprintf(stderr, "main in %d\n", time);
unit();
time ++;
}
return;
}