fork.t revision 7c478bd95313f5f23a4c958a745db2134aa03244
#!./perl
# tests for both real and emulated fork()
BEGIN {
chdir 't' if -d 't';
and $Config{ccflags} =~ /-DPERL_IMPLICIT_SYS/
# and !defined $Config{'useperlio'}
))
{
print "1..0 # Skip: no fork\n";
exit 0;
}
}
exit 0;
}
$|=1;
undef $/;
$tmpfile = "forktmp000";
1 while -f ++$tmpfile;
$CAT = (($^O eq 'MSWin32') ? '.\perl -e "print <>"' : (($^O eq 'NetWare') ? 'perl -e "print <>"' : 'cat'));
for (@prgs){
my $switch;
if (s/^\s*(-\w.*)//){
$switch = $1;
}
$expected =~ s/\n+$//;
# results can be in any order, so sort 'em
my $results;
}
}
else {
}
$status = $?;
$results =~ s/\n+$//;
# bison says 'parse error' instead of 'syntax error',
# various yaccs may or may not capitalize 'syntax'.
print "not ";
}
print "ok ", ++$i, "\n";
}
$| = 1;
sleep 1;
print "ok 2\n";
}
else {
}
}
else {
print "ok 1\n";
sleep 10;
}
ok 1
ok 2
########
$| = 1;
sub forkit {
print "iteration $i start\n";
my $x = fork;
if ($x) {
print "iteration $i parent\n";
}
else {
print "iteration $i child\n";
}
}
else {
print "pid $$ failed to fork\n";
}
}
########
$| = 1;
fork()
: (print("child\n"),exit) ;
########
$| = 1;
fork()
? (print("parent\n"),exit)
########
$| = 1;
@a = (1..3);
for (@a) {
print "parent $_\n";
$_ = "[$_]";
}
else {
print "child $_\n";
$_ = "-$_-";
}
}
print "@a\n";
parent 1
child 1
parent 2
child 2
parent 2
child 2
parent 3
child 3
parent 3
child 3
parent 3
child 3
parent 3
child 3
[1] [2] [3]
-1- [2] [3]
[1] -2- [3]
[1] [2] -3-
-1- -2- [3]
-1- [2] -3-
[1] -2- -3-
-1- -2- -3-
########
$| = 1;
foreach my $c (1,2,3) {
print "parent $c\n";
}
else {
print "child $c\n";
exit;
}
}
while (wait() != -1) { print "waited\n" }
child 1
child 2
child 3
parent 1
parent 2
parent 3
########
use Config;
$| = 1;
$\ = "\n";
fork()
1
1
########
$| = 1;
$\ = "\n";
fork()
1
1
########
$| = 1;
use Cwd;
$\ = "\n";
my $dir;
chdir $dir;
chdir "..";
}
else {
sleep 2;
chdir $dir;
chdir "..";
}
########
$| = 1;
$\ = "\n";
my $getenv;
}
else {
}
sleep 1;
}
else {
}
########
$| = 1;
$\ = "\n";
print "parent got $?"
}
else {
exit(42);
}
########
$| = 1;
$\ = "\n";
print "parent got $?"
}
else {
}
########
}
else {
}
########
print $@;
}
else {
print $@;
}
########
print $@;
}
else {
print $@;
}
########
BEGIN {
$| = 1;
print "inner\n";
}
# XXX In emulated fork(), the child will not execute anything after
# the BEGIN block, due to difficulties in recreating the parse stacks
# and restarting yyparse() midstream in the child. This can potentially
# be overcome by treating what's after the BEGIN{} as a brand new parse.
#print "outer\n"
########
my $parent = shift;
my $child = shift;
$pid;
}
# parent
}
else {
# child
exit;
}
my $parent = shift;
my $child = shift;
$pid;
}
# parent
}
else {
# child
exit;
}
########
$|=1;
print "forked first kid\n";
}
else {
print "first child\n";
exit(0);
}
print "forked second kid\n";
}
else {
print "second child\n";
exit(0);
}
########
my $rand_child = rand;
} else {
my $rand_parent = rand;
}
1