3 # tests for both real and emulated fork()
8 require Config; import Config;
9 unless ($Config{'d_fork'} or $Config{'d_pseudofork'}) {
10 print "1..0 # Skip: no fork\n";
13 $ENV{PERL5LIB} = "../lib";
17 print "1..0 # Skip: fork/status problems on MPE/iX\n";
24 @prgs = split "\n########\n", <DATA>;
25 print "1..", scalar @prgs, "\n";
27 $tmpfile = "forktmp000";
28 1 while -f ++$tmpfile;
29 END { close TEST; unlink $tmpfile if $tmpfile; }
31 $CAT = (($^O eq 'MSWin32') ? '.\perl -e "print <>"' : (($^O eq 'NetWare') ? 'perl -e "print <>"' : 'cat'));
38 my($prog,$expected) = split(/\nEXPECT\n/, $_);
39 $expected =~ s/\n+$//;
40 # results can be in any order, so sort 'em
41 my @expected = sort split /\n/, $expected;
42 open TEST, ">$tmpfile" or die "Cannot open $tmpfile: $!";
43 print TEST $prog, "\n";
44 close TEST or die "Cannot close $tmpfile: $!";
46 if ($^O eq 'MSWin32') {
47 $results = `.\\perl -I../lib $switch $tmpfile 2>&1`;
49 elsif ($^O eq 'NetWare') {
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
97 print "not " unless kill 'INT', $cid;
101 # XXX On Windows the default signal handler kills the
102 # XXX whole process, not just the thread (pseudo-process)
103 $SIG{INT} = sub { exit };
114 print "iteration $i start\n";
118 print "iteration $i parent\n";
121 print "iteration $i child\n";
125 print "pid $$ failed to fork\n";
128 while ($i++ < 3) { do { forkit(); }; }
154 ? (print("parent\n"),sleep(1))
155 : (print("child\n"),exit) ;
162 ? (print("parent\n"),exit)
163 : (print("child\n"),sleep(1)) ;
206 foreach my $c (1,2,3) {
215 while (wait() != -1) { print "waited\n" }
231 ? print($Config{osname} eq $^O)
232 : print($Config{osname} eq $^O) ;
240 ? do { require Config; print($Config::Config{osname} eq $^O); }
241 : do { require Config; print($Config::Config{osname} eq $^O); }
254 print cwd() =~ /\Q$dir/i ? "ok 1 parent" : "not ok 1 parent";
263 print cwd() =~ /\Q$dir/i ? "ok 1 child" : "not ok 1 child";
274 if ($^O eq 'MSWin32' || $^O eq 'NetWare') {
275 $getenv = qq[$^X -e "print \$ENV{TST}"];
278 $getenv = qq[$^X -e 'print \$ENV{TST}'];
283 print "parent before: " . `$getenv`;
285 print "parent after: " . `$getenv`;
288 print "child before: " . `$getenv`;
290 print "child after: " . `$getenv`;
302 print "parent got $?"
315 print "parent got $?"
331 parent died at - line 2.
332 child died at - line 5.
335 eval { die "parent died" };
339 eval { die "child died" };
343 parent died at - line 2.
344 child died at - line 6.
346 if (eval q{$pid = fork}) {
347 eval q{ die "parent died" };
351 eval q{ die "child died" };
355 parent died at (eval 2) line 1.
356 child died at (eval 2) line 1.
363 # XXX In emulated fork(), the child will not execute anything after
364 # the BEGIN block, due to difficulties in recreating the parse stacks
365 # and restarting yyparse() midstream in the child. This can potentially
366 # be overcome by treating what's after the BEGIN{} as a brand new parse.
371 sub pipe_to_fork ($$) {
374 pipe($child, $parent) or die;
376 die "fork() failed: $!" unless defined $pid;
377 close($pid ? $child : $parent);
381 if (pipe_to_fork('PARENT','CHILD')) {
383 print PARENT "pipe_to_fork\n";
388 while (<CHILD>) { print; }
393 sub pipe_from_fork ($$) {
396 pipe($parent, $child) or die;
398 die "fork() failed: $!" unless defined $pid;
399 close($pid ? $child : $parent);
403 if (pipe_from_fork('PARENT','CHILD')) {
405 while (<PARENT>) { print; }
410 print CHILD "pipe_from_fork\n";
420 print "forked first kid\n";
421 print "waitpid() returned ok\n" if waitpid($pid,0) == $pid;
424 print "first child\n";
428 print "forked second kid\n";
429 print "wait() returned ok\n" if wait() == $pid;
432 print "second child\n";
438 waitpid() returned ok
443 pipe(RDR,WTR) or die $!;
445 die "fork: $!" if !defined $pid;
447 my $rand_child = rand;
449 print WTR $rand_child, "\n";
452 my $rand_parent = rand;
454 chomp(my $rand_child = <RDR>);
456 print $rand_child ne $rand_parent, "\n";
461 # [perl #39145] Perl_dounwind() crashing with Win32's fork() emulation
462 sub { @_ = 3; fork ? die "1\n" : die "1\n" }->(2);