Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

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


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

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (8)
As of 2017-12-13 20:59 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (378 votes). Check out past polls.