Debian Codegraph @

test/fork2.awk

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