3 # tests for both real and emulated fork()
11 unless ($Config::Config{d_fork} or $Config::Config{d_pseudofork});
13 if $^O eq 'MSWin32' && is_miniperl;
18 run_multiple_progs('', \*DATA);
20 my $shell = $ENV{SHELL} || '';
22 skip "This test can only be run under bash or zsh"
23 unless $shell =~ m{/(?:ba|z)sh$};
25 $shell -c 'ulimit -u 1 2>/dev/null && echo good'
28 skip "Can't set ulimit -u on this system: $probe"
29 unless $probe eq 'good';
32 $shell -c 'ulimit -u 1; exec $^X -e "
33 print((() = fork) == 1 ? q[ok] : q[not ok])
37 skip "fork() didn't fail, maybe you're running as root", 1
39 is($out, "ok", "bash/zsh-only test for 'fork' returning undef on failure");
48 if ($result = (kill 9, $cid)) {
52 print "not ok 2 $result\n";
54 sleep 1 if $^O eq 'MSWin32'; # avoid WinNT race bug
68 print "not " unless kill 'INT', $cid;
72 # XXX On Windows the default signal handler kills the
73 # XXX whole process, not just the thread (pseudo-process)
74 $SIG{INT} = sub { exit };
86 print "iteration $i start\n";
90 print "iteration $i parent\n";
93 print "iteration $i child\n";
97 print "pid $$ failed to fork\n";
100 while ($i++ < 3) { do { forkit(); }; }
127 ? (print("parent\n"),sleep(1))
128 : (print("child\n"),exit) ;
136 ? (print("parent\n"),exit)
137 : (print("child\n"),sleep(1)) ;
182 foreach my $c (1,2,3) {
191 while (wait() != -1) { print "waited\n" }
208 ? print($Config{osname} eq $^O)
209 : print($Config{osname} eq $^O) ;
218 ? do { require Config; print($Config::Config{osname} eq $^O); }
219 : do { require Config; print($Config::Config{osname} eq $^O); }
227 my $cwd = cwd(); # Make sure we load Win32.pm while "../lib" still works.
234 print cwd() =~ /\Q$dir/i ? "ok 1 parent" : "not ok 1 parent";
243 print cwd() =~ /\Q$dir/i ? "ok 1 child" : "not ok 1 child";
255 if ($^O eq 'MSWin32' || $^O eq 'NetWare') {
256 $getenv = qq[$^X -e "print \$ENV{TST}"];
259 $getenv = qq[$^X -e 'print \$ENV{TST}'];
264 print "parent before: " . `$getenv`;
266 print "parent after: " . `$getenv`;
269 print "child before: " . `$getenv`;
271 print "child after: " . `$getenv`;
284 print "parent got $?"
296 if ($^O =~ /android/) {
297 $echo = q{sh -c 'echo $@' -- };
301 print "parent got $?"
319 parent died at - line 2.
320 child died at - line 5.
323 eval { die "parent died" };
327 eval { die "child died" };
332 parent died at - line 2.
333 child died at - line 6.
335 if (eval q{$pid = fork}) {
336 eval q{ die "parent died" };
340 eval q{ die "child died" };
345 parent died at (eval 2) line 1.
346 child died at (eval 2) line 1.
353 # XXX In emulated fork(), the child will not execute anything after
354 # the BEGIN block, due to difficulties in recreating the parse stacks
355 # and restarting yyparse() midstream in the child. This can potentially
356 # be overcome by treating what's after the BEGIN{} as a brand new parse.
362 sub pipe_to_fork ($$) {
365 pipe($child, $parent) or die;
367 die "fork() failed: $!" unless defined $pid;
368 close($pid ? $child : $parent);
372 if (pipe_to_fork('PARENT','CHILD')) {
374 print PARENT "pipe_to_fork\n";
379 while (<CHILD>) { print; }
384 sub pipe_from_fork ($$) {
387 pipe($parent, $child) or die;
389 die "fork() failed: $!" unless defined $pid;
390 close($pid ? $child : $parent);
394 if (pipe_from_fork('PARENT','CHILD')) {
396 while (<PARENT>) { print; }
401 print CHILD "pipe_from_fork\n";
412 print "forked first kid\n";
413 print "waitpid() returned ok\n" if waitpid($pid,0) == $pid;
416 print "first child\n";
420 print "forked second kid\n";
421 print "wait() returned ok\n" if wait() == $pid;
424 print "second child\n";
431 waitpid() returned ok
436 pipe(RDR,WTR) or die $!;
438 die "fork: $!" if !defined $pid;
441 print WTR "STRING_FROM_CHILD\n";
445 chomp(my $string_from_child = <RDR>);
447 print $string_from_child eq "STRING_FROM_CHILD", "\n";
453 # [perl #39145] Perl_dounwind() crashing with Win32's fork() emulation
454 sub { @_ = 3; fork ? die "1\n" : die "1\n" }->(2);
460 # [perl #72604] @DB::args stops working across Win32 fork
464 print "waitpid() returned ok\n" if waitpid($pid,0) == $pid;
469 print "child: called as [$c[3](", join(',',@DB::args), ")]\n";
476 child: called as [main::f(foo,bar)]
477 waitpid() returned ok
479 # Windows 2000: https://rt.cpan.org/Ticket/Display.html?id=66016#txn-908976
480 system $^X, "-e", "if (\$pid=fork){sleep 1;kill(9, \$pid)} else {sleep 5}";
485 # Windows 7: https://rt.cpan.org/Ticket/Display.html?id=66016#txn-908976
486 system $^X, "-e", "if (\$pid=fork){kill(9, \$pid)} else {sleep 5}";
491 # Windows fork() emulation: can we still waitpid() after signalling SIGTERM?
493 if (my $pid = fork) {
501 $SIG{TERM} = sub { print "2\n" };
511 # this used to SEGV. RT # 121721
515 if (my $pid = fork) {
525 # ${^GLOBAL_PHASE} at the end of a pseudo-fork
526 if (my $pid = fork) {
529 eval 'END { print "${^GLOBAL_PHASE}\n" }';