-
Notifications
You must be signed in to change notification settings - Fork 0
/
test4.c
78 lines (68 loc) · 1.78 KB
/
test4.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
#include "types.h"
#include "user.h"
int stdout = 1;
#define MAX 1000000
// The priority test, the first job will get the highest
// priority, as it will complete first.
void
policytest(void)
{
int pid;
int pmax = 200;
int nmax = 1230; // calculate first 200 prime number (the 200th is 1223 )
int i, j, n;
int nchild = 3;
int cproc[nchild];
int prime[pmax];
for(i=0;i<pmax;i++)
prime[i] = 0;
for(i=0;i<nchild;i++)
cproc[i] = 0;
// Set to Round-Robin scheduling
if(salgo(0) == 0)
printf(stdout, "Switch scheduling strategy to Round-Robin.\n");
else
printf(stdout, "Switch Round-Robin failed.\n");
for(j = 0; j < nchild; j++){
pid = fork();
if(pid > 0){
cproc[j] = pid;
continue;
}
if(pid < 0){
printf(stdout, "fork failed\n");
exit();
}
// give first child process the highest priority
// 40 tickets assigned. Default tickets is same as parent
if(cproc[0] == 0)
nice(-20);
// Calculate Prime Number
for(n = 2 ; n < nmax; n++){
for (i = 0; i < pmax ; i++){
if(prime[i] == 0){
printf(stdout, "PID(%d) (%d tickets) [%d] -> %d\n", getpid(), 41 - (nice(0) - 99), i + 1, n);
prime[i] = n;
}
if(n % prime[i] == 0)
break;
}
}
if(pid == 0){
exit();
}
}
while(nchild--){
wait();
}
// Set back to lottery ticket
if(salgo(1) == 1)
printf(stdout, "Switch scheduling strategy to Loterry scheduling.\n");
else
printf(stdout, "Switch Loterry scheduling failed.\n");
printf(stdout, "Switch strategy test ok\n\n");
}
int main(void){
policytest(); // Switching scheduling policy test.
exit();
}