3 # tests for both real and emulated fork()
7 unshift @INC, '../lib';
8 require Config; import Config;
9 unless ($Config{'d_fork'}
10 or ($^O eq 'MSWin32' and $Config{useithreads}
11 and $Config{ccflags} =~ /-DPERL_IMPLICIT_SYS/))
13 print "1..0 # Skip: no fork\n";
16 $ENV{PERL5LIB} = "../lib";
20 print "1..0 # Skip: fork/status problems on MPE/iX\n";
27 @prgs = split "\n########\n", <DATA>;
28 print "1..", scalar @prgs, "\n";
30 $tmpfile = "forktmp000";
31 1 while -f ++$tmpfile;
32 END { close TEST; unlink $tmpfile if $tmpfile; }
34 $CAT = (($^O eq 'MSWin32') ? '.\perl -e "print <>"' : 'cat');
41 my($prog,$expected) = split(/\nEXPECT\n/, $_);
42 $expected =~ s/\n+$//;
43 # results can be in any order, so sort 'em
44 my @expected = sort split /\n/, $expected;
45 open TEST, ">$tmpfile" or die "Cannot open $tmpfile: $!";
46 print TEST $prog, "\n";
47 close TEST or die "Cannot close $tmpfile: $!";
49 if ($^O eq 'MSWin32') {
50 $results = `.\\perl -I../lib $switch $tmpfile 2>&1`;
53 $results = `./perl $switch $tmpfile 2>&1`;
57 $results =~ s/at\s+forktmp\d+\s+line/at - line/g;
58 $results =~ s/of\s+forktmp\d+\s+aborted/of - aborted/g;
59 # bison says 'parse error' instead of 'syntax error',
60 # various yaccs may or may not capitalize 'syntax'.
61 $results =~ s/^(syntax|parse) error/syntax error/mig;
62 $results =~ s/^\n*Process terminated by SIG\w+\n?//mg
64 my @results = sort split /\n/, $results;
65 if ( "@results" ne "@expected" ) {
66 print STDERR "PROG: $switch\n$prog\n";
67 print STDERR "EXPECTED:\n$expected\n";
68 print STDERR "GOT:\n$results\n";
71 print "ok ", ++$i, "\n";
78 if ($result = (kill 9, $cid)) {
82 print "not ok 2 $result\n";
84 sleep 1 if $^O eq 'MSWin32'; # avoid WinNT race bug
96 print "iteration $i start\n";
100 print "iteration $i parent\n";
103 print "iteration $i child\n";
107 print "pid $$ failed to fork\n";
110 while ($i++ < 3) { do { forkit(); }; }
136 ? (print("parent\n"),sleep(1))
137 : (print("child\n"),exit) ;
144 ? (print("parent\n"),exit)
145 : (print("child\n"),sleep(1)) ;
191 ? print($Config{osname} eq $^O)
192 : print($Config{osname} eq $^O) ;
200 ? do { require Config; print($Config::Config{osname} eq $^O); }
201 : do { require Config; print($Config::Config{osname} eq $^O); }
214 print cwd() =~ /\Q$dir/i ? "ok 1 parent" : "not ok 1 parent";
223 print cwd() =~ /\Q$dir/i ? "ok 1 child" : "not ok 1 child";
234 if ($^O eq 'MSWin32') {
235 $getenv = qq[$^X -e "print \$ENV{TST}"];
238 $getenv = qq[$^X -e 'print \$ENV{TST}'];
243 print "parent before: " . `$getenv`;
245 print "parent after: " . `$getenv`;
248 print "child before: " . `$getenv`;
250 print "child after: " . `$getenv`;
262 print "parent got $?"
275 print "parent got $?"
291 parent died at - line 2.
292 child died at - line 5.
295 eval { die "parent died" };
299 eval { die "child died" };
303 parent died at - line 2.
304 child died at - line 6.
306 if (eval q{$pid = fork}) {
307 eval q{ die "parent died" };
311 eval q{ die "child died" };
315 parent died at (eval 2) line 1.
316 child died at (eval 2) line 1.
323 # XXX In emulated fork(), the child will not execute anything after
324 # the BEGIN block, due to difficulties in recreating the parse stacks
325 # and restarting yyparse() midstream in the child. This can potentially
326 # be overcome by treating what's after the BEGIN{} as a brand new parse.
331 sub pipe_to_fork ($$) {
334 pipe($child, $parent) or die;
336 die "fork() failed: $!" unless defined $pid;
337 close($pid ? $child : $parent);
341 if (pipe_to_fork('PARENT','CHILD')) {
343 print PARENT "pipe_to_fork\n";
348 while (<CHILD>) { print; }
353 sub pipe_from_fork ($$) {
356 pipe($parent, $child) or die;
358 die "fork() failed: $!" unless defined $pid;
359 close($pid ? $child : $parent);
363 if (pipe_from_fork('PARENT','CHILD')) {
365 while (<PARENT>) { print; }
370 print CHILD "pipe_from_fork\n";
380 print "forked first kid\n";
381 print "waitpid() returned ok\n" if waitpid($pid,0) == $pid;
384 print "first child\n";
388 print "forked second kid\n";
389 print "wait() returned ok\n" if wait() == $pid;
392 print "second child\n";
398 waitpid() returned ok