-
Notifications
You must be signed in to change notification settings - Fork 0
/
scheduling.c
303 lines (286 loc) · 7.68 KB
/
scheduling.c
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
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sched.h>
#include <sys/types.h>
#define _PROCESS_H_
#include <sys/wait.h>
#include <errno.h>
#include <unistd.h>
struct process{
char name[32];
int ready_time;
int last_exec_time;
int exec_time;
pid_t pid;
};
void time_unit(){
volatile unsigned long i;
for(int i=0;i<1000000UL;i++)
;
}
void specify_cpu(int pid, int core){
cpu_set_t my_set; //Define your cpu_set bit mask.
CPU_ZERO(&my_set); // Initialize it all to 0
CPU_SET(core, &my_set); // set the bit that represents core.
sched_setaffinity(pid, sizeof(my_set), &my_set);
return;
}
static inline int block_proc(int pid){//remember to check return value//趙允祥說這樣比較快
struct sched_param para;
para.sched_priority = 0;
int ret = sched_setscheduler(pid, SCHED_IDLE, ¶);
return ret;
}
static inline int wake_proc(int pid) {//remember to check return value
struct sched_param para;
para.sched_priority = 0;
int ret = sched_setscheduler(pid, SCHED_OTHER, ¶);
return ret;
}
/*struct timespec {
time_t tv_sec; // seconds
long tv_nsec; // nanoseconds
};*/
void child_proc(int total_time){
block_proc(getpid());
struct timespec start,end;
char my_message[256];
clock_gettime(CLOCK_REALTIME, &start);
for(int j=0;j<total_time;j++){
//if(j%100==0) printf("%d %d\n",getpid(),j);
time_unit();
}
clock_gettime(CLOCK_REALTIME, &end);
sprintf(my_message, "[project1] %d %lu.%09lu %lu.%09lu\n", getpid(), start.tv_sec, start.tv_nsec, end.tv_sec, end.tv_nsec);
syscall(334, my_message);
exit(0);
}
int FIFO(struct process* processes, int num_of_proc){
specify_cpu(getpid(), 0);
wake_proc(getpid());
int time_units_now=0;
int remain_proc=num_of_proc;
int running=-1;
int next=0;
while(1){
if(running!=-1&&processes[running].exec_time==0){ //someone is ending its work
waitpid(processes[running].pid, NULL, 0); //wait for child process to return
printf("%s %d\n", processes[running].name, processes[running].pid);
processes[running].pid=-1;
running=-1;
remain_proc--;
if(remain_proc==0)
exit(0);
}
for(int i=0;i<num_of_proc;i++){
if(processes[i].ready_time==time_units_now&&processes[i].pid==-1){
int pid=fork();
if (pid < 0) {
perror("fork");
return -1;
}
else if (pid==0){ //Child Process
child_proc(processes[i].exec_time);
}
specify_cpu(pid, 1);
processes[i].pid=pid;
}
}
if(running==-1&&processes[next].pid!=-1){ //no one is working, then assign ready process to cpu
wake_proc(processes[next].pid);
specify_cpu(processes[next].pid, 1);
running=next;
}
for(int i=running+1;running!=-1&&i<num_of_proc&&processes[i].pid!=-1;i++)
block_proc(processes[i].pid);
if(running!=-1){
wake_proc(processes[running].pid);
processes[running].exec_time--;
next=running+1;
}
time_unit();
time_units_now++;
}
return 0;
}
int RR(struct process* processes, int num_of_proc){
specify_cpu(getpid(), 0);
wake_proc(getpid());
int time_units_now=0;
int remain_proc=num_of_proc;
int running=-1;
int round_time=0;
int next=0;
int queue[30];
int que_start=0,que_end=0;//que_start=que_end => empty; que_start=que_end+1=>full
while(1){
for(int i=0;i<num_of_proc;i++){//create new process
if(processes[i].ready_time==time_units_now){
int pid;
pid=fork();
if (pid < 0) {
perror("fork");
return -1;
}
else if (pid==0){ //Child Process
child_proc(processes[i].exec_time);
} //prevent child process from getting parent's cpu
specify_cpu(pid, 1);
block_proc(pid);
processes[i].pid=pid;
queue[que_end]=i;
que_end=(que_end+1)%30;
}
}
if(running!=-1&&processes[running].exec_time==0){ //someone is ending its work
waitpid(processes[running].pid, NULL, 0); //wait for child process to return
printf("%s %d\n", processes[running].name, processes[running].pid);
processes[running].pid=-1;
running=-1;
remain_proc--;
que_start=(que_start+1)%30;
if(remain_proc==0)
exit(0);
}
else if(running!=-1 && round_time==0){
block_proc(processes[running].pid);
que_start=(que_start+1)%30;
queue[que_end]=running;
que_end=(que_end+1)%30;
running=-1;
}
//printf("%d %d %d\n",running,que_start,que_end);
if(running==-1&&que_start!=que_end){ //no one is working, then assign ready process to cpu
wake_proc(processes[queue[que_start]].pid);
round_time=0;
//printf("%d %d\n",(next+i)%num_of_proc,time_units_now);
specify_cpu(processes[queue[que_start]].pid, 1);
running=queue[que_start];
}
if(running!=-1){
wake_proc(processes[running].pid);
processes[running].exec_time--;
}
time_unit();
time_units_now++;
round_time=(round_time+1)%500;
}
return 0;
}
int SJF(struct process* processes, int num_of_proc){
specify_cpu(getpid(), 0);
wake_proc(getpid());
int time_units_now=0;
int remain_proc=num_of_proc;
int running=-1;
while(1){
for(int i=0;i<num_of_proc;i++){
if(processes[i].ready_time==time_units_now){
int pid;
pid=fork();
if (pid < 0) {
perror("fork");
return -1;
}
else if (pid==0){ //Child Process
child_proc(processes[i].exec_time);
//specify_cpu(getpid(), 1);
}
specify_cpu(pid, 1);
processes[i].pid=pid;
}
}
if(running!=-1&&processes[running].exec_time==0){ //someone is ending its work
waitpid(processes[running].pid, NULL, 0); //wait for child process to return
printf("%s %d\n", processes[running].name, processes[running].pid);
processes[running].pid=-1;
running=-1;
remain_proc--;
if(remain_proc==0)
exit(0);
}
if(running==-1){
int min=10000000;
for(int i=0;i<num_of_proc;i++){
if(processes[i].ready_time>time_units_now)
break;
if(processes[i].exec_time < min && processes[i].pid!=-1){
running=i;
min=processes[i].exec_time;
}
}
if(running!=-1){
wake_proc(processes[running].pid);
specify_cpu(processes[running].pid, 1);
}
}
else
wake_proc(processes[running].pid);
time_unit();
if(running!=-1){
processes[running].exec_time--;
}
time_units_now++;
}
return 0;
}
int PSJF(struct process* processes, int num_of_proc){
specify_cpu(getpid(), 0);
wake_proc(getpid());
int time_units_now=0;
int remain_proc=num_of_proc;
int running=-1;
while(1){
for(int i=0;i<num_of_proc;i++){
if(processes[i].ready_time==time_units_now){
int pid;
pid=fork();
if (pid < 0) {
perror("fork");
return -1;
}
else if (pid==0){ //Child Process
child_proc(processes[i].exec_time);
//specify_cpu(getpid(), 1);
}
specify_cpu(pid, 1);
processes[i].pid=pid;
}
}
if(running!=-1&&processes[running].exec_time==0){ //someone is ending its work
waitpid(processes[running].pid, NULL, 0); //wait for child process to return
printf("%s %d\n", processes[running].name, processes[running].pid);
processes[running].pid=-1;
running=-1;
remain_proc--;
if(remain_proc==0)
exit(0);
}
int min=10000000;
int old=running;
for(int i=0;i<num_of_proc;i++){
if(processes[i].ready_time>time_units_now)
break;
if(processes[i].exec_time < min && processes[i].pid!=-1){
running=i;
min=processes[i].exec_time;
//if(time_units_now%500==0)
// printf("%s,%d\n",processes[i].name,processes[i].exec_time);
}
}
if(old!=-1 && old!=running && processes[old].pid!=-1) block_proc(processes[old].pid);
if(running!=-1){
wake_proc(processes[running].pid);
specify_cpu(processes[running].pid, 1);
}
time_unit();
if(running!=-1){
processes[running].exec_time--;
}
time_units_now++;
}
return 0;
}