Lots of consting
[p5sagit/p5-mst-13.2.git] / t / op / getppid.t
CommitLineData
098f0b12 1#!./perl
2
3# Test that getppid() follows UNIX semantics: when the parent process
4# dies, the child is reparented to the init process (pid 1).
5
6BEGIN {
7 chdir 't' if -d 't';
8 @INC = qw(../lib);
9}
10
11use strict;
12use Config;
13
14BEGIN {
15 for my $syscall (qw(pipe fork waitpid getppid)) {
16 if (!$Config{"d_$syscall"}) {
17 print "1..0 # Skip: no $syscall\n";
18 exit;
19 }
20 }
21 print "1..3\n";
22}
23
24pipe my ($r, $w) or die "pipe: $!\n";
25my $pid = fork; defined $pid or die "fork: $!\n";
26
27if ($pid) {
28 # parent
29 close $w;
30 waitpid($pid, 0) == $pid or die "waitpid: $!\n";
31 print <$r>;
32}
33else {
34 # child
35 close $r;
36 my $pid2 = fork; defined $pid2 or die "fork: $!\n";
37 if ($pid2) {
38 close $w;
39 sleep 1;
40 }
41 else {
42 # grandchild
43 my $ppid1 = getppid();
44 print $w "not " if $ppid1 <= 1;
45 print $w "ok 1 # ppid1=$ppid1\n";
46 sleep 2;
47 my $ppid2 = getppid();
48 print $w "not " if $ppid1 == $ppid2;
49 print $w "ok 2 # ppid2=$ppid2, ppid1!=ppid2\n";
50 print $w "not " if $ppid2 != 1;
51 print $w "ok 3 # ppid2=1\n";
52 }
53 exit 0;
54}