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";
22 @prgs = split "\n########\n", <DATA>;
23 print "1..", scalar @prgs, "\n";
25 $tmpfile = "forktmp000";
26 1 while -f ++$tmpfile;
27 END { unlink $tmpfile if $tmpfile; }
29 $CAT = (($^O eq 'MSWin32') ? '.\perl -e "print <>"' : 'cat');
36 my($prog,$expected) = split(/\nEXPECT\n/, $_);
37 $expected =~ s/\n+$//;
38 # results can be in any order, so sort 'em
39 my @expected = sort split /\n/, $expected;
40 open TEST, ">$tmpfile" or die "Cannot open $tmpfile: $!";
41 print TEST $prog, "\n";
42 close TEST or die "Cannot close $tmpfile: $!";
44 if ($^O eq 'MSWin32') {
45 $results = `.\\perl -I../lib $switch $tmpfile 2>&1`;
48 $results = `./perl $switch $tmpfile 2>&1`;
52 $results =~ s/at\s+forktmp\d+\s+line/at - line/g;
53 $results =~ s/of\s+forktmp\d+\s+aborted/of - aborted/g;
54 # bison says 'parse error' instead of 'syntax error',
55 # various yaccs may or may not capitalize 'syntax'.
56 $results =~ s/^(syntax|parse) error/syntax error/mig;
57 my @results = sort split /\n/, $results;
58 if ( "@results" ne "@expected" ) {
59 print STDERR "PROG: $switch\n$prog\n";
60 print STDERR "EXPECTED:\n$expected\n";
61 print STDERR "GOT:\n$results\n";
64 print "ok ", ++$i, "\n";
71 if ($result = (kill 9, $cid)) {
75 print "not ok 2 $result\n";
77 sleep 1 if $^O eq 'MSWin32'; # avoid WinNT race bug
89 print "iteration $i start\n";
93 print "iteration $i parent\n";
96 print "iteration $i child\n";
100 print "pid $$ failed to fork\n";
103 while ($i++ < 3) { do { forkit(); }; }
129 ? (print("parent\n"),sleep(1))
130 : (print("child\n"),exit) ;
137 ? (print("parent\n"),exit)
138 : (print("child\n"),sleep(1)) ;
184 ? print($Config{osname} eq $^O)
185 : print($Config{osname} eq $^O) ;
193 ? do { require Config; print($Config::Config{osname} eq $^O); }
194 : do { require Config; print($Config::Config{osname} eq $^O); }
207 print cwd() =~ /\Q$dir/i ? "ok 1 parent" : "not ok 1 parent";
216 print cwd() =~ /\Q$dir/i ? "ok 1 child" : "not ok 1 child";
227 if ($^O eq 'MSWin32') {
228 $getenv = qq[$^X -e "print \$ENV{TST}"];
231 $getenv = qq[$^X -e 'print \$ENV{TST}'];
236 print "parent before: " . `$getenv`;
238 print "parent after: " . `$getenv`;
241 print "child before: " . `$getenv`;
243 print "child after: " . `$getenv`;
255 print "parent got $?"
268 print "parent got $?"
284 parent died at - line 2.
285 child died at - line 5.
288 eval { die "parent died" };
292 eval { die "child died" };
296 parent died at - line 2.
297 child died at - line 6.
299 if (eval q{$pid = fork}) {
300 eval q{ die "parent died" };
304 eval q{ die "child died" };
308 parent died at (eval 2) line 1.
309 child died at (eval 2) line 1.
316 # XXX In emulated fork(), the child will not execute anything after
317 # the BEGIN block, due to difficulties in recreating the parse stacks
318 # and restarting yyparse() midstream in the child. This can potentially
319 # be overcome by treating what's after the BEGIN{} as a brand new parse.
324 sub pipe_to_fork ($$) {
327 pipe($child, $parent) or die;
329 die "fork() failed: $!" unless defined $pid;
330 close($pid ? $child : $parent);
334 if (pipe_to_fork('PARENT','CHILD')) {
336 print PARENT "pipe_to_fork\n";
341 while (<CHILD>) { print; }
346 sub pipe_from_fork ($$) {
349 pipe($parent, $child) or die;
351 die "fork() failed: $!" unless defined $pid;
352 close($pid ? $child : $parent);
356 if (pipe_from_fork('PARENT','CHILD')) {
358 while (<PARENT>) { print; }
363 print CHILD "pipe_from_fork\n";