-
Notifications
You must be signed in to change notification settings - Fork 0
/
lotterytest2.c
104 lines (97 loc) · 2.46 KB
/
lotterytest2.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
#include "types.h"
#include "user.h"
// test the lottery ticket scheduler
// This test makes sure that the process with the highest priority gets to run the most.
int
main(int argc, char *argv[])
{
printf(1, "lottery test 2\n");
// fork into 6 processes with different niceness values.
int k = 0;
int pid;
double z = 0;
pid = fork();
if (pid == 0) {
while(1) {
// set priority to 18
if (nice(0) <= 17)
nice(1); // increment nice by 1
if (nice(0) == 18) {
k++;
z += 3.14 * 89.9; // occupy cpu time
if(k%1000000 == 0)
printf(1, "Process 1 -Priority=%d -tickets= %d -ticks=%d \n",nice(0),20-nice(0)+1,k);
}
}
}
pid = fork();
if (pid == 0) {
while(1) {
// set priority to 18
if (nice(0) <= 17)
nice(1); // increment nice by 1
if (nice(0) == 18) {
k++;
z += 3.14 * 89.9; // occupy cpu time
if(k%1000000 == 0)
printf(1, "Process 2 -Priority=%d -tickets= %d -ticks=%d \n",nice(0),20-nice(0)+1,k);
}
}
}
pid = fork();
if (pid == 0) {
while(1) {
// set priority to 18
if (nice(0) <= 17)
nice(1); // increment nice by 1
if (nice(0) == 18) {
k++;
z += 3.14 * 89.9; // occupy cpu time
if(k%1000000 == 0)
printf(1, "Process 3 -Priority=%d -tickets= %d -ticks=%d \n",nice(0),20-nice(0)+1,k);
}
}
}
pid = fork();
if (pid == 0) {
while(1) {
// set priority to 16
if (nice(0) <= 15)
nice(1); // increment nice by 1
if (nice(0) == 16) {
k++;
z += 3.14 * 89.9; // occupy cpu time
if(k%1000000 == 0)
printf(1, "Process 4 -Priority=%d -tickets= %d -ticks=%d \n",nice(0),20-nice(0)+1,k);
}
}
}
pid = fork();
if (pid == 0) {
while(1) {
// set priority to 11
if (nice(0) <= 10)
nice(1); // increment nice by 1
if (nice(0) == 11) {
k++;
z += 3.14 * 89.9; // occupy cpu time
if(k%1000000 == 0)
printf(1, "Process 5 -Priority=%d -tickets= %d -ticks=%d \n",nice(0),20-nice(0)+1,k);
}
}
}
else {
while(1) {
// set priority to 6
if (nice(0) <= 5)
nice(1); // increment nice by 1
if (nice(0) == 6) {
k++;
z += 3.14 * 89.9; // occupy cpu time
if(k%1000000 == 0)
printf(1, "Process 6 -Priority=%d -tickets= %d -ticks=%d \n",nice(0),20-nice(0)+1,k);
}
}
}
exit();
}