Proc::Branch - Creating Multiple Child Processes and Merging
use Proc::Branch; my $b = Proc::Branch->new( branch => 4 ); # 1 parent and 3 children my $procid = $b->proc; # serial number of the process print "I am processor $procid.\n"; if ( $procid == 0 ) { print "I am the parent.\n"; for ( 1 .. 3 ) { my $pid = $b->pid($_); # PID of the children print "I have child $pid\n"; } } $b->merge; # merging the branched processes $b = undef; # same as above
This module branches the current process into multiple processes when the object is created. Internally, perl function "fork" is used.
new is the constructor method. It has arguments shown below.
new
$b = Proc::Branch( # default values 'branch' => 2, # number of branches including the parent process 'sleep' => 0, # sleep time between forking 'debug' => 0, # turn on to see detailed messages 'auto_merge' => 1, # When the object is destroyed, it merges. # When auto_merge is turned off, # 'merge' should be called somewhere. );
proc returns serial number of the processes. Parent process is 0.
proc
pid returns process ID of the child with the serial_number. When it is called by a child process, undef is returned.
pid
undef
Branched processes are merged. If auto_merge mode, you can simply destroy the object to call this method.
auto_merge
Proc::Fork, Proc::Simple
In Suk Joung, <jmarch@hanmail.net>
Copyright (C) 2009 by In Suk Joung
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.5 or, at your option, any later version of Perl 5 you may have available.
To install Proc::Branch, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Proc::Branch
CPAN shell
perl -MCPAN -e shell install Proc::Branch
For more information on module installation, please visit the detailed CPAN module installation guide.