3 # tests for both real and emulated fork()
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/
12 # and !defined $Config{'useperlio'}
15 print "1..0 # Skip: no fork\n";
18 $ENV{PERL5LIB} = "../lib";
22 print "1..0 # Skip: fork/status problems on MPE/iX\n";
29 @prgs = split "\n########\n", <DATA>;
30 print "1..", scalar @prgs, "\n";
32 $tmpfile = "forktmp000";
33 1 while -f ++$tmpfile;
34 END { close TEST; unlink $tmpfile if $tmpfile; }
36 $CAT = (($^O eq 'MSWin32') ? '.\perl -e "print <>"' : 'cat');
43 my($prog,$expected) = split(/\nEXPECT\n/, $_);
44 $expected =~ s/\n+$//;
45 # results can be in any order, so sort 'em
46 my @expected = sort split /\n/, $expected;
47 open TEST, ">$tmpfile" or die "Cannot open $tmpfile: $!";
48 print TEST $prog, "\n";
49 close TEST or die "Cannot close $tmpfile: $!";
51 if ($^O eq 'MSWin32') {
52 $results = `.\\perl -I../lib $switch $tmpfile 2>&1`;
55 $results = `./perl $switch $tmpfile 2>&1`;
59 $results =~ s/at\s+forktmp\d+\s+line/at - line/g;
60 $results =~ s/of\s+forktmp\d+\s+aborted/of - aborted/g;
61 # bison says 'parse error' instead of 'syntax error',
62 # various yaccs may or may not capitalize 'syntax'.
63 $results =~ s/^(syntax|parse) error/syntax error/mig;
64 $results =~ s/^\n*Process terminated by SIG\w+\n?//mg
66 my @results = sort split /\n/, $results;
67 if ( "@results" ne "@expected" ) {
68 print STDERR "PROG: $switch\n$prog\n";
69 print STDERR "EXPECTED:\n$expected\n";
70 print STDERR "GOT:\n$results\n";
73 print "ok ", ++$i, "\n";
80 if ($result = (kill 9, $cid)) {
84 print "not ok 2 $result\n";
86 sleep 1 if $^O eq 'MSWin32'; # avoid WinNT race bug
98 print "iteration $i start\n";
102 print "iteration $i parent\n";
105 print "iteration $i child\n";
109 print "pid $$ failed to fork\n";
112 while ($i++ < 3) { do { forkit(); }; }
138 ? (print("parent\n"),sleep(1))
139 : (print("child\n"),exit) ;
146 ? (print("parent\n"),exit)
147 : (print("child\n"),sleep(1)) ;
190 foreach my $c (1,2,3) {
199 while (wait() != -1) { print "waited\n" }
215 ? print($Config{osname} eq $^O)
216 : print($Config{osname} eq $^O) ;
224 ? do { require Config; print($Config::Config{osname} eq $^O); }
225 : do { require Config; print($Config::Config{osname} eq $^O); }
238 print cwd() =~ /\Q$dir/i ? "ok 1 parent" : "not ok 1 parent";
247 print cwd() =~ /\Q$dir/i ? "ok 1 child" : "not ok 1 child";
258 if ($^O eq 'MSWin32') {
259 $getenv = qq[$^X -e "print \$ENV{TST}"];
262 $getenv = qq[$^X -e 'print \$ENV{TST}'];
267 print "parent before: " . `$getenv`;
269 print "parent after: " . `$getenv`;
272 print "child before: " . `$getenv`;
274 print "child after: " . `$getenv`;
286 print "parent got $?"
299 print "parent got $?"
315 parent died at - line 2.
316 child died at - line 5.
319 eval { die "parent died" };
323 eval { die "child died" };
327 parent died at - line 2.
328 child died at - line 6.
330 if (eval q{$pid = fork}) {
331 eval q{ die "parent died" };
335 eval q{ die "child died" };
339 parent died at (eval 2) line 1.
340 child died at (eval 2) line 1.
347 # XXX In emulated fork(), the child will not execute anything after
348 # the BEGIN block, due to difficulties in recreating the parse stacks
349 # and restarting yyparse() midstream in the child. This can potentially
350 # be overcome by treating what's after the BEGIN{} as a brand new parse.
355 sub pipe_to_fork ($$) {
358 pipe($child, $parent) or die;
360 die "fork() failed: $!" unless defined $pid;
361 close($pid ? $child : $parent);
365 if (pipe_to_fork('PARENT','CHILD')) {
367 print PARENT "pipe_to_fork\n";
372 while (<CHILD>) { print; }
377 sub pipe_from_fork ($$) {
380 pipe($parent, $child) or die;
382 die "fork() failed: $!" unless defined $pid;
383 close($pid ? $child : $parent);
387 if (pipe_from_fork('PARENT','CHILD')) {
389 while (<PARENT>) { print; }
394 print CHILD "pipe_from_fork\n";
404 print "forked first kid\n";
405 print "waitpid() returned ok\n" if waitpid($pid,0) == $pid;
408 print "first child\n";
412 print "forked second kid\n";
413 print "wait() returned ok\n" if wait() == $pid;
416 print "second child\n";
422 waitpid() returned ok