3 # tests for both real and emulated fork()
11 unless ($Config::Config{d_fork} or $Config::Config{d_pseudofork});
16 run_multiple_progs('', \*DATA);
18 my $shell = $ENV{SHELL} || '';
20 skip "This test can only be run under bash or zsh"
21 unless $shell =~ m{/(?:ba|z)sh$};
23 $shell -c 'ulimit -u 1 2>&1 && echo good'
26 skip "Can't set ulimit -u on this system: $probe"
27 unless $probe eq 'good';
30 $shell -c 'ulimit -u 1; exec $^X -e "
31 print((() = fork) == 1 ? q[ok] : q[not ok])
35 skip "fork() didn't fail, maybe you're running as root", 1
37 is($out, "ok", "bash/zsh-only test for 'fork' returning undef on failure");
46 if ($result = (kill 9, $cid)) {
50 print "not ok 2 $result\n";
52 sleep 1 if $^O eq 'MSWin32'; # avoid WinNT race bug
66 print "not " unless kill 'INT', $cid;
70 # XXX On Windows the default signal handler kills the
71 # XXX whole process, not just the thread (pseudo-process)
72 $SIG{INT} = sub { exit };
84 print "iteration $i start\n";
88 print "iteration $i parent\n";
91 print "iteration $i child\n";
95 print "pid $$ failed to fork\n";
98 while ($i++ < 3) { do { forkit(); }; }
125 ? (print("parent\n"),sleep(1))
126 : (print("child\n"),exit) ;
134 ? (print("parent\n"),exit)
135 : (print("child\n"),sleep(1)) ;
180 foreach my $c (1,2,3) {
189 while (wait() != -1) { print "waited\n" }
206 ? print($Config{osname} eq $^O)
207 : print($Config{osname} eq $^O) ;
216 ? do { require Config; print($Config::Config{osname} eq $^O); }
217 : do { require Config; print($Config::Config{osname} eq $^O); }
225 my $cwd = cwd(); # Make sure we load Win32.pm while "../lib" still works.
232 print cwd() =~ /\Q$dir/i ? "ok 1 parent" : "not ok 1 parent";
241 print cwd() =~ /\Q$dir/i ? "ok 1 child" : "not ok 1 child";
253 if ($^O eq 'MSWin32' || $^O eq 'NetWare') {
254 $getenv = qq[$^X -e "print \$ENV{TST}"];
257 $getenv = qq[$^X -e 'print \$ENV{TST}'];
262 print "parent before: " . `$getenv`;
264 print "parent after: " . `$getenv`;
267 print "child before: " . `$getenv`;
269 print "child after: " . `$getenv`;
282 print "parent got $?"
296 print "parent got $?"
314 parent died at - line 2.
315 child died at - line 5.
318 eval { die "parent died" };
322 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" };
340 parent died at (eval 2) line 1.
341 child died at (eval 2) line 1.
348 # XXX In emulated fork(), the child will not execute anything after
349 # the BEGIN block, due to difficulties in recreating the parse stacks
350 # and restarting yyparse() midstream in the child. This can potentially
351 # be overcome by treating what's after the BEGIN{} as a brand new parse.
357 sub pipe_to_fork ($$) {
360 pipe($child, $parent) or die;
362 die "fork() failed: $!" unless defined $pid;
363 close($pid ? $child : $parent);
367 if (pipe_to_fork('PARENT','CHILD')) {
369 print PARENT "pipe_to_fork\n";
374 while (<CHILD>) { print; }
379 sub pipe_from_fork ($$) {
382 pipe($parent, $child) or die;
384 die "fork() failed: $!" unless defined $pid;
385 close($pid ? $child : $parent);
389 if (pipe_from_fork('PARENT','CHILD')) {
391 while (<PARENT>) { print; }
396 print CHILD "pipe_from_fork\n";
407 print "forked first kid\n";
408 print "waitpid() returned ok\n" if waitpid($pid,0) == $pid;
411 print "first child\n";
415 print "forked second kid\n";
416 print "wait() returned ok\n" if wait() == $pid;
419 print "second child\n";
426 waitpid() returned ok
431 pipe(RDR,WTR) or die $!;
433 die "fork: $!" if !defined $pid;
436 print WTR "STRING_FROM_CHILD\n";
440 chomp(my $string_from_child = <RDR>);
442 print $string_from_child eq "STRING_FROM_CHILD", "\n";
448 # [perl #39145] Perl_dounwind() crashing with Win32's fork() emulation
449 sub { @_ = 3; fork ? die "1\n" : die "1\n" }->(2);
455 # [perl #72604] @DB::args stops working across Win32 fork
459 print "waitpid() returned ok\n" if waitpid($pid,0) == $pid;
464 print "child: called as [$c[3](", join(',',@DB::args), ")]\n";
471 child: called as [main::f(foo,bar)]
472 waitpid() returned ok
474 # Windows 2000: https://rt.cpan.org/Ticket/Display.html?id=66016#txn-908976
475 system $^X, "-e", "if (\$pid=fork){sleep 1;kill(9, \$pid)} else {sleep 5}";
480 # Windows 7: https://rt.cpan.org/Ticket/Display.html?id=66016#txn-908976
481 system $^X, "-e", "if (\$pid=fork){kill(9, \$pid)} else {sleep 5}";
486 # Windows fork() emulation: can we still waitpid() after signalling SIGTERM?
488 if (my $pid = fork) {
496 $SIG{TERM} = sub { print "2\n" };