> -----Original Message----- > From: Ho, Tony [mailto:[EMAIL PROTECTED]] > Sent: Thursday, May 30, 2002 8:09 AM > To: '[EMAIL PROTECTED]' > Subject: Launching Perl Code in Parallel > > > Hi Guys > I was wondering if you could help me. > > I have a perl program, get_ice_cream_descriptions.pl, that accepts 2 > arguments > > For example, > > get_ice_cream_descriptions.pl 1 10 > > However, I would like to get ice cream descriptions for other > ranges at the > same time, such as 11 - 20, 21 - 30, 31 - 40, 41 - 50 etc > > I could write an external file that contains the following : > > get_ice_cream_descriptions.pl 1 10 & > get_ice_cream_descriptions.pl 11 20 & > get_ice_cream_descriptions.pl 21 30 & > get_ice_cream_descriptions.pl 31 40 & > get_ice_cream_descriptions.pl 41 50 & > > This way I could execute the perl code in parallel and > eventually get the > descriptions. > > Instead of using the above method, I would prefer to write > some code in > get_ice_cream_descriptions.pl accept different number ranges > and launch > itself. > I have been looking at the fork() command. > I believe fork() only creates a copy of the original process > but I wasn't > quite sure how I could pass the different number ranges into > the perl code > before it forks itself.
Since the child is a copy of the parent, the value of all variables in the child will be the same as in the parent, so you can just do something like this: # parent loops through ranges, creating # a child to handle each range for my $range (@ranges) { defined(my $pid = fork) or die "Couldn't fork: $!"; unless($pid) { # I'm the child... process_range($range); exit; # child must exit here! } } -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]