Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

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 quiet...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (10)
As of 2018-06-19 09:20 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (111 votes). Check out past polls.