-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathlottery1.c
97 lines (83 loc) · 1.92 KB
/
lottery1.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
#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.
// This test also makes sure that processes with a priority of 0 (lowest possible) still gets to run.
#define X 10000
int
main(int argc, char *argv[])
{
printf(1, "lottery test 1\n");
// fork into 4 processes with different niceness values.
int pid;
pid = fork();
if (pid == 0) {
int count = 0;
int laps = 0;
while(1) {
// since prints are expensive, only print after X iterations
count++;
if (count > X) {
laps++;
count = 0;
printf(1, "BEST\n");
}
// keep priority between 17 and 20
if (nice(0) <= 17)
nice(5); // increment nice by 5
}
}
pid = fork();
if (pid == 0) {
int count = 0;
int laps = 0;
while(1) {
// since prints are expensive, only print after X iterations
count++;
if (count > X) {
laps++;
count = 0;
printf(1, "HIGH\n");
}
// keep priority between 12 and 15
if (nice(0) <= 12)
nice(5); // increment nice by 5
}
}
pid = fork();
if (pid == 0) {
int count = 0;
int laps = 0;
while(1) {
// since prints are expensive, only print after X iterations
count++;
if (count > X) {
laps++;
count = 0;
printf(1, "LOW\n");
}
// keep priority between 7 and 10
if (nice(0) <= 7)
nice(5); // increment nice by 5
}
}
pid = fork();
if (pid == 0) {
int count = 0;
int laps = 0;
while(1) {
// since prints are expensive, only print after X iterations
count++;
if (count > X) {
laps++;
count = 0;
printf(1, "WORST\n");
}
// keep priority between 2 and 5
if (nice(0) <= 2)
nice(5); // increment nice by 5
}
}
while(1) {}
exit();
}