Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Re: spawning multiple child processes

by kvale (Monsignor)
on Jun 21, 2002 at 05:11 UTC ( #176226=note: print w/replies, xml ) Need Help??

in reply to spawning multiple child processes

You can definitely create child processes in Perl programs. The most common way is to do this with fork:
sub bear_child { my $pid; my $sigset; # block signal for fork $sigset = POSIX::SigSet->new(SIGINT); sigprocmask( SIG_BLOCK, $sigset) or die "Can't block SIGINT for fork: $!\n"; # fork a subprocess die "fork: $!" unless defined ($pid = fork); if ($pid) { # Parent records the child's birth and returns. sigprocmask( SIG_UNBLOCK, $sigset) or die "Can't unblock SIGINT for fork: $!\n"; $children{$pid} = 1; $children++; return; } else { # Child process must *not* return from this subroutine. $SIG{INT} = 'DEFAULT'; # make SIGINT kill us as it did befo +re # unblock signals sigprocmask( SIG_UNBLOCK, $sigset) or die "Can't unblock SIGINT for fork: $!\n"; # do kid sutff # ... print "child $$ doing stuff\n"; # cleanup print "child $$ is done processing\n"; exit; # make sure sub never returns } }
So here, the fork makes a clone of the parent process; both processes then test the $pid and take different branches according to whether they are the parent or the child.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://176226]
and all is calm...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2018-02-24 22:22 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (311 votes). Check out past polls.