in reply to Help with waitpid and forking ?

Here is some code for you to have a look at. It forks 5 kids. One to process each of the dirs in @dirs. It then waits for them to finish and dies.

So to answer your specific questions

1) you can fork lots of processes - each has a overhead in starting so sometimes more is less and sometimes less is more if you get my drift. The longer each kid will take to run the more worthwhile it is to fork them off.

2) Either code it so you get the number of processes you want or see Parallel::ForkManager vs global variables for a bit of info on this module (it allows you to set an upper limit on processes)

3) You wait on kids as shown below:

#!/usr/bin/perl -w use strict; use POSIX ":sys_wait_h"; $|++; # flush buffers my @dirs = qw ( dir1 dir2 dir3 dir4 dir5 ); my @pids; for my $dir (@dirs) { my $pid = fork(); die "Fork failed" unless defined $pid; push @pids, $pid; next if $pid; # parent returns to loop # child starts here do_dir($dir); exit; # kill child here after it has done work } print "Child pids are:\n"; print "$_\n" for @pids; # wait for all kids to finish my $kids; do{ $kids = waitpid(-1, &WNOHANG); }until $kids == -1; print "My children are all dead!\n"; print "My life is not worth living....\n"; die "I can go on no longer\nGoodbye cruel CPU\n"; sub do_dir { my $dirname = shift; # do dir stuff # we will exit on return sleep ( 2+ rand 5); # simulate processing print "Processed $dirname\n"; }

cheers

tachyon

s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Replies are listed 'Best First'.
Re: Re: Help with waitpid and forking ?
by VicBalta (Scribe) on Sep 05, 2001 at 22:01 UTC

    Hi

    Thanks for all the help tachyon. I have having some problems running the code you sampled. When it is done with the do_dir and it hits the exit. It exits the whole program. But what I just figured out that the problem only happens in when i run it with perl builder. When i ran it from a command line it ran fine. I am running win 2k. does any one know why this happened?

    Thanks

      In case you have not noticed Perl Builder inserts itself between your code and Perl. As a result code runs about 10-100 times slower in the IDE than from the command line, and sometimes weird stuff happens. I always double check on the command line if I think my code is OK but it chokes in PB2. Often the code runs fine so something in the PB2/perl.exe interface is breaking it. Most code runs fine in PB2 but forking does not. I get the same result as you in PB2. There is however nothing wrong with my code as you have found when you run it from the command line :-)

      The lastest version 2c is better that 2b or 2a. The {ahmm} *extended life demo versions* are all unstable and more buggy than the registered product.

      cheers

      tachyon

      s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

      * cracks