1#!/usr/bin/perl 2$| = 1; 3 4# Note that because fork loses test count we do not use Test::More 5 6use strict; 7 8BEGIN { 9 require Config; 10 my $can_fork = $Config::Config{d_fork} || 11 (($^O eq 'MSWin32' || $^O eq 'NetWare') and 12 $Config::Config{useithreads} and 13 $Config::Config{ccflags} =~ /-DPERL_IMPLICIT_SYS/ 14 ); 15 if ( $can_fork ) { 16 print "1..8\n"; 17 } else { 18 print "1..0 # Skip No fork available\n"; 19 exit; 20 } 21} 22 23use File::Temp; 24 25# OO interface 26 27my $file = File::Temp->new(); 28 29myok( 1, -f $file->filename, "OO File exists" ); 30 31my $children = 2; 32for my $i (1 .. $children) { 33 my $pid = fork; 34 die "Can't fork: $!" unless defined $pid; 35 if ($pid) { 36 # parent process 37 next; 38 } else { 39 # in a child we can't keep the count properly so we do it manually 40 # make sure that child 1 dies first 41 srand(); 42 my $time = (($i-1) * 5) +int(rand(5)); 43 print "# child $i sleeping for $time seconds\n"; 44 sleep($time); 45 my $count = $i + 1; 46 myok( $count, -f $file->filename(), "OO file present in child $i" ); 47 print "# child $i exiting\n"; 48 exit; 49 } 50} 51 52while ($children) { 53 wait; 54 $children--; 55} 56 57 58 59myok( 4, -f $file->filename(), "OO File exists in parent" ); 60 61# non-OO interface 62 63my ($fh, $filename) = File::Temp::tempfile( UNLINK => 1 ); 64 65myok( 5, -f $filename, "non-OO File exists" ); 66 67$children = 2; 68for my $i (1 .. $children) { 69 my $pid = fork; 70 die "Can't fork: $!" unless defined $pid; 71 if ($pid) { 72 # parent process 73 next; 74 } else { 75 srand(); 76 my $time = (($i-1) * 5) +int(rand(5)); 77 print "# child $i sleeping for $time seconds\n"; 78 sleep($time); 79 my $count = 5 + $i; 80 myok( $count, -f $filename, "non-OO File present in child $i" ); 81 print "# child $i exiting\n"; 82 exit; 83 } 84} 85 86while ($children) { 87 wait; 88 $children--; 89} 90myok(8, -f $filename, "non-OO File exists in parent" ); 91 92 93# Local ok sub handles explicit number 94sub myok { 95 my ($count, $test, $msg) = @_; 96 97 if ($test) { 98 print "ok $count - $msg\n"; 99 } else { 100 print "not ok $count - $msg\n"; 101 } 102 return $test; 103} 104