-
Notifications
You must be signed in to change notification settings - Fork 57
/
p-testforksimple.cc
36 lines (31 loc) · 1.05 KB
/
p-testforksimple.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
#include "u-lib.hh"
void process_main() {
pid_t initial_pid = sys_getpid();
assert(initial_pid > 0);
// Fork a total of three new copies, checking return values for simple
// issues.
pid_t p1 = sys_fork();
assert(p1 >= 0);
pid_t intermediate_pid = sys_getpid();
if (p1 == 0) {
assert(intermediate_pid != initial_pid);
} else {
assert(intermediate_pid == initial_pid);
assert(p1 != initial_pid);
}
pid_t p2 = sys_fork();
assert(p2 >= 0);
pid_t final_pid = sys_getpid();
if (p2 == 0) {
assert(final_pid != initial_pid && final_pid != intermediate_pid);
} else {
assert(final_pid == intermediate_pid);
assert(p2 != initial_pid && p2 != intermediate_pid && p2 != p1);
}
console_printf(CPOS(final_pid - 1, 0), 0x700,
"testforksimple %d [%d] %d [%d] %d succeeded.\n",
initial_pid, p1, intermediate_pid, p2, final_pid);
// This test runs before `sys_exit` is implemented, so we can’t use it.
while (true) {
}
}