Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

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.


Comment on Re: spawning multiple child processes
Download Code

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://176226]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (7)
As of 2015-10-07 18:29 GMT
Find Nodes?
    Voting Booth?

    Does Humor Belong in Programming?

    Results (197 votes), past polls