Ok I wrote you a quick example jeroe. I only had time to test it on a Xp
box, this should work on *nix as well since most of my coding is on *nix.
What you are looking for is a set of exec and waitpid call's to do the trick
for you. Basicly this is how its usually don with forking.

a: parents forks
b: child exec's
c: parent keeps track of childs

You might wanna expiriment with threads. It would not only give you beter
control, more flexibility, but also faster perforamce.

-->>> CUT

use POSIX ":sys_wait_h";
my $CHILD_SPAWNED=0;
my $CHILD_TO_CREATE=20;
my $MAX_CHILD=3;
$|=1;

while( $CHILD_SPAWNED < $CHILD_TO_CREATE ){

print "-------\t\t RUNING $CHILD_CREATED------------\n";

if( $CHILD_CREATED < $MAX_CHILD ){

 die "couldnt not spawn: $!" unless defined ( my $PID=fork() );

 $CHILD_CREATED++;
 $CHILD_SPAWNED++;

 unless( $PID ){
 $|=1;

 print "I am child $$...about to exec\n";

 { exec ('c:\perl\count.pl') }; print STDERR "could not exec: $!";

 }


}

# child never get's here

$status=waitpid(-1,WNOHANG);

if( $status != 0 ){ #some child has exited
 $CHILD_CREATED--;

 print "child $status has exited \n";
}

}


print "TOTAL CHILD SPAWNED IS $CHILD_SPAWNED\n";


<<<<<--------- PASTE


->>>>> CUT

#c:\perl\count.pl
while($i<100000){$i++};
print $i;

<<<------ PASTE




----- Original Message -----
From: "Jeroen Lodewijks" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Wednesday, March 19, 2003 3:03 PM
Subject: Fork ?


> Hi all,
>
> Not sure if this appropriate for the beginners list but there is no
> intermediate list :)
>
> I am writing a program in Perl to 'move' output from one process.
> The program looks in a database which contains lists of files.
>
> These files are either FTP'ed to mailed to recipients.
>
> I actually made this program using modules Mime::Lite and Net::FTP
> No sweat really.
>
> But when I FTP or mail, the program waits, so I want to make it multi
> process.
> And there is my problem. I tried fork() but it doesn't do what I want or I
> got it wrong.
> (maybe it's not the appropriate thing to use anyway)
>
> So my question is: how to start multi processing?
>
> Just imagine I have 'datapump', I gathered all data required to send it.
> What I want now is that a separate process is started to send the file
> (either by FTP or mail).
> The data pump continues and spawns up to 10 processes, if more it will
wait.
>
> Here is what I thought would work: (try program)
>
> #!/bin/env perl
>
> use strict;
>
> my $ADD = 0;
> my $WAIT = 1;
>
> {
> my $state = $ADD;
> my $processes = 1;
>
> sub ProcessHandler
> {
> my $request = @_;
>
> print "REQUEST = $request STATE = $state PRC =
> $processes\n";
>
> if ($state == $ADD) {
> my $pid = fork();
> die "Cannot fork: $!" unless defined($pid);
> if ($pid == 0) {
>   # Child process
> $processes++;
> if ($state > 10) {
> $state = $WAIT;
> }
> sleep (int(rand 20) + 1);
> print "child proc added PRC = $processes\n";
> }
> else {
> print "MAIN process PRC $processes\n";
> }
> }
> elsif ($state == $WAIT) {
> print "WAIT\n";
> wait;
> $state = $ADD;
> $processes--;
> print "child proc removed PRC = $processes\n";
> }
> }
> }
>
> my $i;
> for ($i = 0;; $i < 20; $i++) {
> print "HI! $i\n";
> ProcessHandler($i);
> };
>
>
> It doesn't do what I want at all. It spawns way too many processes.
> (and I get errors in win 2000).
>
> Basically, I want to spawn a process which does something (call a
> subroutine)
> The main process should just continue pumping data which is spawned up to
10
> processes.
> Else it should wait until 1 of the 10 processes finishes.
>
> Any code examples out there??
>
> PS The target machine is a Unix box.
>
> Thanx,
>
> Jeroen
>
>
> --
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to