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 $?"
294 if ($^O =~ /android/) {
295 $echo = q{sh -c 'echo $@' -- };
299 print "parent got $?"
317 parent died at - line 2.
318 child died at - line 5.
321 eval { die "parent died" };
325 eval { die "child died" };
330 parent died at - line 2.
331 child died at - line 6.
333 if (eval q{$pid = fork}) {
334 eval q{ die "parent died" };
338 eval q{ die "child died" };
343 parent died at (eval 2) line 1.
344 child died at (eval 2) line 1.
351 # XXX In emulated fork(), the child will not execute anything after
352 # the BEGIN block, due to difficulties in recreating the parse stacks
353 # and restarting yyparse() midstream in the child. This can potentially
354 # be overcome by treating what's after the BEGIN{} as a brand new parse.
360 sub pipe_to_fork ($$) {
363 pipe($child, $parent) or die;
365 die "fork() failed: $!" unless defined $pid;
366 close($pid ? $child : $parent);
370 if (pipe_to_fork('PARENT','CHILD')) {
372 print PARENT "pipe_to_fork\n";
377 while (<CHILD>) { print; }
382 sub pipe_from_fork ($$) {
385 pipe($parent, $child) or die;
387 die "fork() failed: $!" unless defined $pid;
388 close($pid ? $child : $parent);
392 if (pipe_from_fork('PARENT','CHILD')) {
394 while (<PARENT>) { print; }
399 print CHILD "pipe_from_fork\n";
410 print "forked first kid\n";
411 print "waitpid() returned ok\n" if waitpid($pid,0) == $pid;
414 print "first child\n";
418 print "forked second kid\n";
419 print "wait() returned ok\n" if wait() == $pid;
422 print "second child\n";
429 waitpid() returned ok
434 pipe(RDR,WTR) or die $!;
436 die "fork: $!" if !defined $pid;
439 print WTR "STRING_FROM_CHILD\n";
443 chomp(my $string_from_child = <RDR>);
445 print $string_from_child eq "STRING_FROM_CHILD", "\n";
451 # [perl #39145] Perl_dounwind() crashing with Win32's fork() emulation
452 sub { @_ = 3; fork ? die "1\n" : die "1\n" }->(2);
458 # [perl #72604] @DB::args stops working across Win32 fork
462 print "waitpid() returned ok\n" if waitpid($pid,0) == $pid;
467 print "child: called as [$c[3](", join(',',@DB::args), ")]\n";
474 child: called as [main::f(foo,bar)]
475 waitpid() returned ok
477 # Windows 2000: https://rt.cpan.org/Ticket/Display.html?id=66016#txn-908976
478 system $^X, "-e", "if (\$pid=fork){sleep 1;kill(9, \$pid)} else {sleep 5}";
483 # Windows 7: https://rt.cpan.org/Ticket/Display.html?id=66016#txn-908976
484 system $^X, "-e", "if (\$pid=fork){kill(9, \$pid)} else {sleep 5}";
489 # Windows fork() emulation: can we still waitpid() after signalling SIGTERM?
491 if (my $pid = fork) {
499 $SIG{TERM} = sub { print "2\n" };
509 # this used to SEGV. RT # 121721
513 if (my $pid = fork) {