-
Notifications
You must be signed in to change notification settings - Fork 1
/
round_robin.c
77 lines (62 loc) · 1.57 KB
/
round_robin.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
#include "types.h"
#include "stat.h"
#include "user.h"
#include "pstat.h"
#define check(exp, msg) if(exp) {} else {\
printf(1, "%s:%d check (" #exp ") failed: %s\n", __FILE__, __LINE__, msg);\
exit();}
#define DDEBUG 1
#ifdef DDEBUG
# define DEBUG_PRINT(x) printf x
#else
# define DEBUG_PRINT(x) do {} while (0)
#endif
int workload(int n) {
int i, j = 0;
for (i = 0; i < n; i++) {
j += i * j + 1;
}
return j;
}
int
main(int argc, char *argv[])
{
struct pstat st;
int time_slices[] = {0, 32, 16, 8};
check(getpinfo(&st) == 0, "getpinfo");
// Push this thread to the bottom
workload(80000000);
int i, j, k;
// Launch the 4 processes
for (i = 0; i < 4; i++) {
int c_pid = fork();
// Child
if (c_pid == 0) {
workload(800000000);
exit();
}
}
// Checking every 4 time-slice for 8 times
for (i = 0; i < 8; i++) {
sleep(100);
check(getpinfo(&st) == 0, "getpinfo");
for (j = 0; j < NPROC; j++) {
if (st.inuse[j] && st.pid[j] > 3) {
DEBUG_PRINT((1, "pid: %d\n", st.pid[j]));
for (k = 3; k >= 0; k--) {
DEBUG_PRINT((1, "\t level %d ticks used %d -- state : %d\n", k, st.ticks[j][k], st.state[j]));
//DEBUG_PRINT((1, "\t level %d waited for ticks %d\n", k, st.wait_ticks[j][k]));
if (k > 0 && st.state[j] != 5) {
check(st.ticks[j][k] % time_slices[k] == 0, "All processes have used up the timer ticks in level 3, 2 and 1");
}
}
}
}
}
for (i = 0; i < 4; i++) {
// sleep(100);
wait();
}
printf(1, "TEST PASSED");
exit();
}