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