Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: controlling child processes

by bluto (Curate)
on Apr 04, 2006 at 23:03 UTC ( #541238=note: print w/replies, xml ) Need Help??


in reply to controlling child processes

It is easy to get these problems wrong, so I second using Parallel::ForkManager. In your solution you need to keep looping not only if there is more work to spawn, but also if there are children to reap. Update: That's confusing to read -- it just means you'll need to reap 3 children at the end.

I like to use a hash to maintain the children. Here's an untested solution, probably with bugs ...

use strict; use warnings; $SIG{CHLD} = 'IGNORE'; my $max_procs = 3; # don't use strings when you want numbers my @work = qw(fw1 fw2 fw3 fw4 fw5); my %kids; while (@work or keys %kids) { if (@work and $max_procs > keys %kids) { my $fw = shift @work; my $pid = fork; die "Can not fork!" if ! defined $pid; if ($pid) { $kids{$pid} = $fw; } else { exec "remotelogfile $fw logfile > /var/$fw.log"; die; } } else { my $pid = waitpid(-1, 0); delete $kids{$pid}; } }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://541238]
help
Chatterbox?
[Discipulus]: next time you can add `SARGV-1` and if still not impressed pass a 'rm -f..' as last arg
[Eily]: :)
[Eily]: or I might be tempted to send him an Acme::EyeDropsed file next time
[Eily]: Acme::Bleach is tempting, but it will look like I'm just using the one module that does precisely exactly what he wants

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (8)
As of 2017-04-26 15:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I'm a fool:











    Results (483 votes). Check out past polls.