3 # tests for both real and emulated fork()
8 $ENV{PERL5LIB} = "../lib";
12 unless ($Config::Config{d_fork} or $Config::Config{d_pseudofork});
15 skip_all('fork/status problems on MPE/iX')
20 run_multiple_progs('', \*DATA);
28 if ($result = (kill 9, $cid)) {
32 print "not ok 2 $result\n";
34 sleep 1 if $^O eq 'MSWin32'; # avoid WinNT race bug
48 print "not " unless kill 'INT', $cid;
52 # XXX On Windows the default signal handler kills the
53 # XXX whole process, not just the thread (pseudo-process)
54 $SIG{INT} = sub { exit };
66 print "iteration $i start\n";
70 print "iteration $i parent\n";
73 print "iteration $i child\n";
77 print "pid $$ failed to fork\n";
80 while ($i++ < 3) { do { forkit(); }; }
107 ? (print("parent\n"),sleep(1))
108 : (print("child\n"),exit) ;
116 ? (print("parent\n"),exit)
117 : (print("child\n"),sleep(1)) ;
162 foreach my $c (1,2,3) {
171 while (wait() != -1) { print "waited\n" }
188 ? print($Config{osname} eq $^O)
189 : print($Config{osname} eq $^O) ;
198 ? do { require Config; print($Config::Config{osname} eq $^O); }
199 : do { require Config; print($Config::Config{osname} eq $^O); }
207 my $cwd = cwd(); # Make sure we load Win32.pm while "../lib" still works.
214 print cwd() =~ /\Q$dir/i ? "ok 1 parent" : "not ok 1 parent";
223 print cwd() =~ /\Q$dir/i ? "ok 1 child" : "not ok 1 child";
235 if ($^O eq 'MSWin32' || $^O eq 'NetWare') {
236 $getenv = qq[$^X -e "print \$ENV{TST}"];
239 $getenv = qq[$^X -e 'print \$ENV{TST}'];
244 print "parent before: " . `$getenv`;
246 print "parent after: " . `$getenv`;
249 print "child before: " . `$getenv`;
251 print "child after: " . `$getenv`;
264 print "parent got $?"
278 print "parent got $?"
296 parent died at - line 2.
297 child died at - line 5.
300 eval { die "parent died" };
304 eval { die "child died" };
309 parent died at - line 2.
310 child died at - line 6.
312 if (eval q{$pid = fork}) {
313 eval q{ die "parent died" };
317 eval q{ die "child died" };
322 parent died at (eval 2) line 1.
323 child died at (eval 2) line 1.
330 # XXX In emulated fork(), the child will not execute anything after
331 # the BEGIN block, due to difficulties in recreating the parse stacks
332 # and restarting yyparse() midstream in the child. This can potentially
333 # be overcome by treating what's after the BEGIN{} as a brand new parse.
339 sub pipe_to_fork ($$) {
342 pipe($child, $parent) or die;
344 die "fork() failed: $!" unless defined $pid;
345 close($pid ? $child : $parent);
349 if (pipe_to_fork('PARENT','CHILD')) {
351 print PARENT "pipe_to_fork\n";
356 while (<CHILD>) { print; }
361 sub pipe_from_fork ($$) {
364 pipe($parent, $child) or die;
366 die "fork() failed: $!" unless defined $pid;
367 close($pid ? $child : $parent);
371 if (pipe_from_fork('PARENT','CHILD')) {
373 while (<PARENT>) { print; }
378 print CHILD "pipe_from_fork\n";
389 print "forked first kid\n";
390 print "waitpid() returned ok\n" if waitpid($pid,0) == $pid;
393 print "first child\n";
397 print "forked second kid\n";
398 print "wait() returned ok\n" if wait() == $pid;
401 print "second child\n";
408 waitpid() returned ok
413 pipe(RDR,WTR) or die $!;
415 die "fork: $!" if !defined $pid;
418 print WTR "STRING_FROM_CHILD\n";
422 chomp(my $string_from_child = <RDR>);
424 print $string_from_child eq "STRING_FROM_CHILD", "\n";
430 # [perl #39145] Perl_dounwind() crashing with Win32's fork() emulation
431 sub { @_ = 3; fork ? die "1\n" : die "1\n" }->(2);
437 # [perl #72604] @DB::args stops working across Win32 fork
441 print "waitpid() returned ok\n" if waitpid($pid,0) == $pid;
446 print "child: called as [$c[3](", join(',',@DB::args), ")]\n";
453 child: called as [main::f(foo,bar)]
454 waitpid() returned ok
456 # Windows 2000: https://rt.cpan.org/Ticket/Display.html?id=66016#txn-908976
457 system $^X, "-e", "if (\$pid=fork){sleep 1;kill(9, \$pid)} else {sleep 5}";
462 # Windows 7: https://rt.cpan.org/Ticket/Display.html?id=66016#txn-908976
463 system $^X, "-e", "if (\$pid=fork){kill(9, \$pid)} else {sleep 5}";