
October 9th, 2002, 11:14 PM
|
|
Contributing User
|
|
Join Date: Sep 2002
Posts: 36
Time spent in forums: < 1 sec
Reputation Power: 6
|
|
|
Fork()ing NET::FTP - taboo?
Hey guys,
Latly I've spent some time trying to get to know fork() a little better. It's somewhat challenging, it would seem that all the docs on the net about it are a little overly complicated, here is what I've got and if you think it's lame or bad sample, bear with me, that's one of my first attemps on fork(), I borrowed most of the code from samples here and there.
It would seem that it works just fine, untill I call on NET::FTP, I mean i tried sticking just some code pieces inside and it works, as soon as I put something in that involves objects(??), like DBI or FTP it stops working, I wonder if anybody could help me out.
Thanks in advance,
Code:
#! /usr/bin/perl
use NET::FTP;
use constant CLIENTS => 3;
use constant DEBUG => 1;
my @kids=();
my $pid=$$;
my $parentpid=0;
sub Forker {
my $clients=shift;
my $i=0;
while ($i++ < $clients) {
my $newpid = fork();
if (! defined $newpid) { #hosed
die "fork() error: $!\n";
}
elsif ($newpid == 0) { #child
$parentpid = $pid;
$pid = $$;
@kids = (); #don't inhert the kids
last;
}
else { #parent (defined $newpid)
push(@kids, $newpid);
}
}
}
sub SharedInit {
}
sub Work {
print "Connecting...\n";
if(!($f=Net::FTP->new("127.0.0.1", Port => 21, Debug => 0, Timeout => 5))) {print "FTP Conenction Failed\n"}
$f->quit;
print "<br>Connected Sucessfully.\n";
}
sub Reaper {
while (my $kid = shift(@kids)) {
my $reaped = waitpid($kid,0);
unless ($reaped == $kid) {
}
}
}
SharedInit();
Forker(CLIENTS);
if ($parentpid) {
Work();
}
else { #the original parent only does cleanup duty
Reaper();
}
if ($parentpid) {exit(0);}
else {exit(0);}
die;
__END__
use POSIX ":sys_wait_h";
do {
$kid = waitpid(-1,&WNOHANG);
} until $kid == -1;
|