Galen has asked for the wisdom of the Perl Monks concerning the following question:
The problem is that even though the logical order of my code says to: 1) create the batch, then 2) do something with this batch, then 3) create another batch, it isn't working in that order. Instead, I watch as 12 child processes go out and do their thing. Even though part of the duty of each process is to print data and that data has not yet been returned to me, the loop figures it's done and proceeds to the next batch. I don't get it.
Here's an example - shouldn't this snippet send off 5 processes which sleep for 15 seconds, then print 5 values, after which another 15 seconds passes and another 5 values are printed, then 15 seconds and the last two are printed? Instead, it sleeps for 15 seconds and prints all 12 values.
#!/usr/bin/perl # batch.pl use IO::Select; use IO::Pipe; my @list = ("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l"); my $children = 5; my $counter = 0; my $size = @list; foreach (@list) { $counter++; if (($counter%$children) && ($counter != $size)) { push(@temp,$_); } else { push(@temp,$_); @batch = @temp; @temp = @blank; foreach (@batch) { my $pipe = IO::Pipe->new(); my $pid = fork; die "Bad Fork $!" unless defined $pid; if ($pid) { $pipe->reader(); # $select->add( $pipe ); } else { $pipe->writer(); $pipe->autoflush(1); &dosomething; exit; } } } } sub dosomething { sleep 15; print "$_\n"; }
By the way, I tried using waitpid() with no success. Perhaps I was using it incorrectly. Is waitpid() the way to go, or is my logic just totally wrong?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: how to limit child processes?
by AgentM (Curate) on May 02, 2001 at 01:46 UTC | |
by merlyn (Sage) on May 02, 2001 at 02:59 UTC | |
|
Re: how to limit child processes?
by mikfire (Deacon) on May 02, 2001 at 07:51 UTC |