#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2002
    Posts
    36
    Rep Power
    13

    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;
  2. #2
  3. No Profile Picture
    Bare knuckle tiger fighter
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2002
    Location
    MK the UK
    Posts
    44
    Rep Power
    13
    Could try that one on perlmonks.com, they seem to have a endless supply of arcane knowledge.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2002
    Posts
    36
    Rep Power
    13
    I am sorry for bumping this up, however I feel that this is appropriate, is has been a while since i asked, however I found solution and if someone else have the same problem they may learn for my experience in this particular matter.

    My problem was not with fork() as it turns out, rather with windows and they way activestate fork()s

    Once moved to unix platform which fork() was originally designed for all my problems disappeared and everything works, allthough I dealt away with above code snippit, if you have the same problem, just kick win in the nuts and go with something things were made for

IMN logo majestic logo threadwatch logo seochat tools logo