Debian Codegraph @

test/fork.awk

raw
1 @load "fork"
2
3 BEGIN {
4 fn = ("fork.tmp." PROCINFO["pid"])
5 switch (pid = fork()) {
6 case -1:
7 printf "Error: fork failed with ERRNO %s\n", ERRNO
8 exit 1
9 case 0:
10 # child
11 printf "pid %s ppid %s\n", PROCINFO["pid"], PROCINFO["ppid"] > fn
12 exit 0
13 default:
14 # parent
15 erc = 1
16 if ((rc = wait()) < 0)
17 printf "Error: wait failed with ERRNO %s\n", ERRNO
18 else if (rc != pid)
19 printf "Error: wait returned %s instead of child pid %s\n", rc, pid
20 else if ((getline x < fn) != 1)
21 printf "Error: getline failed on temp file %s\n", fn
22 else {
23 close(fn) # required on non-POSIX systems
24 expected = ("pid " pid " ppid " PROCINFO["pid"])
25 if (x != expected)
26 printf "Error: child data (%s) != expected (%s)\n", x, expected
27 else if ((rc = system("rm " fn)) != 0)
28 printf "Error removing temp file %s with ERRNO %s\n", fn, ERRNO
29 else
30 erc = 0
31 }
32 exit erc
33 }
34 }