Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Re: forking through a subroutine

by leons (Pilgrim)
on Apr 10, 2001 at 12:41 UTC ( #71285=note: print w/ replies, xml ) Need Help??


in reply to Re: forking through a subroutine
in thread forking through a subroutine

Hi,

I'm writing something similar as well at this moment which
might come in handy.

You can pass a reference to a sub and it's parameters to the
spawnChild sub, which will provide you with it's PID and
a filehandler to the child.

It's all still very basic and nothing fancy, that's because I'm
doing lots of learning and testing right now ... but it works ;-)

#!/usr/bin/perl -w use strict; # # Declaration of Subroutines # sub spawnChild(@); # # Main # my ($pid,$fh)=spawnChild(\&Count,10); print "$pid\n"; # # Subroutines # sub spawnChild(@) { my $childSub=shift; my @Parameters=@_; my ($Cnt,$pid)=0; do { $pid=open FH,'-|'; unless (defined $pid) { warn "Cannot fork: $!\n"; die "Could not fork\n" if $Cnt++ > 5; sleep 10; } } until defined $pid; if ($pid) { #Parent return($pid,*{FH}); } else { #Child; &$childSub(@Parameters); exit(0); } } sub Count($) { foreach(1..shift) { print "$_\n"; sleep(1); } }

Bye, Leon


Comment on Re: Re: forking through a subroutine
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (16)
As of 2014-09-23 12:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (221 votes), past polls