in reply to Keep Two Processes Going
Instead of that I usually try to have a pipe into the processes and poll/select on them. Most programs don't close the standard handles, so you can actually use these. In this case I use STDIN.
The following code demonstrates the idea. Modify it to fit whatever your constraints are.
#! /usr/bin/perl -w use 5.008; use strict; use IO::Select; # program and args my @work = qw(sleep 3); my $wanted = 2; my $s = IO::Select->new; my $have = 0; while ($wanted) { while ($have < $wanted) { open(my $fh, "|-", @work) || die "Could not fork: $!"; $s->add($fh); $have++; } for ($s->can_read()) { $s->remove($_); $have--; close $_; die "Unexpected returncode $?" if $?; # Real code may change $wanted here } }
|
|---|