On Wed, 28 Aug 2002 07:30:15 -0500, [EMAIL PROTECTED] (Philip Montgomery) wrote:
>I am trying to figure out how to run process concurrently from Perl. I want the >parent to spawn off the children and wait until they are complete to continue. I can >get the child processes to run in serial, but I need them to run in parallel. > >The code I am using thus far is > >for ($x;$x<4;$x++) { > if ($pid=fork) { > } else { > system "mkdir dir$x"; > for ($i=1;$i<250;$i++) { > open3(OUTPUT, INPUT, ERRORS, cd dir$x;make clean; make all); > <code to process the output of the make commands and store into log files> > } >} > >Thanks for any help. Here is something written by Abigail on perlmonks.org. ################################################################ #!/usr/bin/perl =head1 Some times you have a need to fork of several children, but you want to limit the maximum number of children that are alive at one time. Here are two little subroutines that might help you, mfork and afork. They are very similar. They take three arguments, and differ in the first argument. For mfork, the first argument is a number, indicating how many children should be forked. For afork, the first argument is an array - a child will be forked for each array element. The second argument indicates the maximum number of children that may be alive at one time. The third argument is a code reference; this is the code that will be executed by the child. One argument will be given to this code fragment; for mfork it will be an increasing number, starting at one. Each next child gets the next number. For afork, the array element is passed. Note that this code will assume no other children will be spawned, and that $SIG {CHLD} hasn't been set to IGNORE. by Abigail =cut mfork (10,10,\&hello); sub hello{print "hello world\n";} ################################################### sub mfork ($$&) { my ($count, $max, $code) = @_; foreach my $c (1 .. $count) { wait unless $c <= $max; die "Fork failed: $!\n" unless defined (my $pid = fork); exit $code -> ($c) unless $pid; } 1 until -1 == wait; } ################################################## sub afork (\@$&) { my ($data, $max, $code) = @_; my $c = 0; foreach my $data (@$data) { wait unless ++ $c <= $max; die "Fork failed: $!\n" unless defined (my $pid = fork); exit $code -> ($data) unless $pid; } 1 until -1 == wait; } %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% and here is an afork example ###################################################### #!/usr/bin/perl =head1 For afork, the first argument is an array - a child will be forked for each array element. The second argument indicates the maximum number of children that may be alive at one time. The third argument is a code reference; this is the code that will be executed by the child. One argument will be given to this code fragment. For afork, the array element ispassed. Note that this code will assume no other children will be spawned,and that $SIG {CHLD} hasn't been set to IGNORE. by Abigail =cut if ($#ARGV < 0){@ARGV = qw( 1 2 3 4 5)} afork (\@ARGV,10,\&hello); sub hello{print "hello world\n";} ################################################## sub afork (\@$&) { my ($data, $max, $code) = @_; my $c = 0; foreach my $data (@$data) { wait unless ++ $c <= $max; die "Fork failed: $!\n" unless defined (my $pid = fork); exit $code -> ($data) unless $pid; } 1 until -1 == wait; } ##################################################### -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]