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

Re: Limiting the number of forks

by Abigail-II (Bishop)
on Oct 02, 2002 at 13:39 UTC ( [id://202263]=note: print w/replies, xml ) Need Help??


in reply to Limiting the number of forks

You could use the following subroutine. Call it with three arguments, the number of forks you want, the maximum number of childs you can have around simultaneously, and a code reference of code a child needs to perform:
sub mfork ($$&) { my ($count, $max, $code) = @_; foreach my $c (1 .. $count) { wait unless $c <= $max; die "Fork failed: $!\n" unless defined (my $pid = fork); exit $code -> ($c) unless $pid; } 1 until -1 == wait; }

Abigail

Replies are listed 'Best First'.
Re: Re: Limiting the number of forks
by robartes (Priest) on Oct 02, 2002 at 14:01 UTC
    Hi Abigail-II,

    Sorry to barge in to the middle of a discussion with a new question, but this code piqued my interest on your answer:

    exit $code -> ($c) unless $pid;

    In your discussion, you mentioned that $code should be a coderef. However, unless I'm mistaken (which is entirely possible) the above will not lead to the expected result in that case. I would rather expect something like:

    exit &$code unless $pid;

    I can make sense of your code if $code is in fact an object which implements $c methods (probably through autoloading).

    Could you throw some light on the darkness of my confusion?

    CU
    Robartes-

      You didn't try, did you?
      use strict; use warnings 'all'; sub mfork ($$&) { my ($count, $max, $code) = @_; foreach my $c (1 .. $count) { wait unless $c <= $max; die "Fork failed: $!\n" unless defined (my $pid = fork); exit $code -> ($c) unless $pid; } 1 until -1 == wait; } mfork 10, 3, sub { print "$$: " . localtime () . ": Starting\n"; select undef, undef, undef, 2 + rand 2; print "$$: " . localtime () . ": Exiting\n"; }; __END__ 972: Wed Oct 2 16:06:32 2002: Starting 973: Wed Oct 2 16:06:32 2002: Starting 974: Wed Oct 2 16:06:32 2002: Starting 973: Wed Oct 2 16:06:34 2002: Exiting 975: Wed Oct 2 16:06:34 2002: Starting 972: Wed Oct 2 16:06:35 2002: Exiting 976: Wed Oct 2 16:06:35 2002: Starting 974: Wed Oct 2 16:06:35 2002: Exiting 977: Wed Oct 2 16:06:35 2002: Starting 975: Wed Oct 2 16:06:38 2002: Exiting 978: Wed Oct 2 16:06:38 2002: Starting 977: Wed Oct 2 16:06:39 2002: Exiting 979: Wed Oct 2 16:06:39 2002: Starting 976: Wed Oct 2 16:06:39 2002: Exiting 980: Wed Oct 2 16:06:39 2002: Starting 978: Wed Oct 2 16:06:41 2002: Exiting 981: Wed Oct 2 16:06:41 2002: Starting 979: Wed Oct 2 16:06:41 2002: Exiting 980: Wed Oct 2 16:06:42 2002: Exiting 981: Wed Oct 2 16:06:45 2002: Exiting

      Please consult the perlref manual page for syntax details dealing with references.

      Abigail

        Robartes chastened

        I stand corrected. $coderef -> ($c) calls the referenced function with $c as argument. Thanks!

        CU
        Robartes-

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2024-04-25 10:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found