-
Notifications
You must be signed in to change notification settings - Fork 58
/
p-testwaitpid.cc
99 lines (82 loc) · 2.85 KB
/
p-testwaitpid.cc
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
#include "u-lib.hh"
static const int order[] = {
2, 7, 6, 4, 1, 8, 5, 3
};
static void make_children(pid_t* children) {
for (size_t i = 0; i != arraysize(order); ++i) {
pid_t p = sys_fork();
if (p == 0) {
sys_msleep(order[i] * 100);
sys_exit(order[i]);
}
assert_gt(p, 0);
children[i] = p;
}
}
void process_main() {
console_printf("waitpid(0, W_NOHANG) tests...\n");
pid_t children[arraysize(order)];
make_children(children);
for (size_t i = 0; i != arraysize(order); ++i) {
pid_t ch;
int status = 0;
while ((ch = sys_waitpid(0, &status, W_NOHANG)) == E_AGAIN) {
sys_yield();
}
assert_gt(ch, 0);
size_t idx = 0;
while (idx != arraysize(order) && children[idx] != ch) {
++idx;
}
assert(idx < arraysize(order));
children[idx] = 0;
console_printf("%d @%lu: exit status %d\n", ch, idx, status);
assert_eq(order[idx], status);
}
assert_eq(sys_waitpid(0, nullptr, W_NOHANG), E_CHILD);
console_printf("waitpid(0, W_NOHANG) tests succeed.\n");
console_printf("waitpid(pid, W_NOHANG) tests...\n");
make_children(children);
for (size_t i = 0; i != arraysize(order); ++i) {
pid_t ch;
int status = 0;
while ((ch = sys_waitpid(children[i], &status, W_NOHANG)) == E_AGAIN) {
sys_yield();
}
assert_eq(ch, children[i]);
console_printf("%d @%lu: exit status %d\n", ch, i, status);
assert_eq(order[i], status);
}
assert_eq(sys_waitpid(0, nullptr, W_NOHANG), E_CHILD);
console_printf("waitpid(pid, W_NOHANG) tests succeed.\n");
console_printf("waitpid(0) blocking tests...\n");
make_children(children);
for (size_t i = 0; i != arraysize(order); ++i) {
int status = 0;
pid_t ch = sys_waitpid(0, &status);
assert_gt(ch, 0);
size_t idx = 0;
while (idx != arraysize(order) && children[idx] != ch) {
++idx;
}
assert(idx < arraysize(order));
children[idx] = 0;
console_printf("%d @%lu: exit status %d\n", ch, idx, status);
assert_eq(order[idx], status);
}
assert_eq(sys_waitpid(0), E_CHILD);
console_printf("waitpid(0) blocking tests succeed.\n");
console_printf("waitpid(pid) blocking tests...\n");
make_children(children);
for (size_t i = 0; i != arraysize(order); ++i) {
int status = 0;
pid_t ch = sys_waitpid(children[i], &status);
assert_eq(ch, children[i]);
console_printf("%d @%lu: exit status %d\n", ch, i, status);
assert_eq(order[i], status);
}
assert_eq(sys_waitpid(0), E_CHILD);
console_printf("waitpid(pid) blocking tests succeed.\n");
console_printf(COLOR_SUCCESS, "testwaitpid succeeded!\n");
sys_exit(0);
}