Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I have a question in forking a process. My application forks couple of processes as per the configuration file, say 3.

After sometime depending on the load, I would like to add 3 more processes. There are some things to consider, like: attach the parent pid of this new child to those as earlier and to have these child processes do the same functionality as the previous childs do.

My question to the Perl Monks is, Is there any simple way to do this? Thanks,

Replies are listed 'Best First'.
Re: Forking a new Process
by Zaxo (Archbishop) on Nov 06, 2001 at 07:39 UTC

    Parallel::ForkManager can do what you want.

    use Parallel::ForkManager; my $max = 3; my %cids = (); my $forker = new Parallel::ForkManager($max); while (<REQUEST>) { if (pressure() > 0) { $max = $forker->set_max_procs( $max + 3) ; } elsif {pressure() < 0) { $max = 1 + scalar( keys %cids); $forker->set_max_procs( $max); } if (my $pid = $forker->start()) { ++$cids{$pid}; next; } act_on($_); $forker->finish(); } $forker->wait_on_children();
    With adjustments and subs to fit your purpose.

    After Compline,
    Zaxo

Re: Forking a new Process
by sevensven (Pilgrim) on Nov 06, 2001 at 06:16 UTC

    Well, assuming you dont kill the parent process, why dont you let the parent process do all the forking ?

    It's better if you have only one process doing it, otherwise, two children could decide independently that 3 new process where needed and you would realy have 6 new process (and so on).

    If you realy crave for children of parents that want to be children of their grandparent, I don't think there's anything like that in the C api, so there should be neither in Perl's fork.

    May the modules from Poe (perl application kernel with event driven threads) and it's modules can give you a hand.

    HTH