in reply to Opening simultaneous scripts

Or you can use fork(). Why use system() when you can fork off your own kids. As such:

my $kids = 0; for ( 1..30 ) { if ( fork ) { waitpid(-1,0) && $kids++; } else { exec('scriptname') or die "Bad Things: $!; } } while ( $kids < 30 ) { waitpid(-1,0) && $kids++; sleep(1); }


Update: Yes, it's overkill, but you gotta have fun sometime!


   [ qi3ber ] I used to think that I knew it all, then I started to listen.

Replies are listed 'Best First'.
RE: Re: Opening simultaneous scripts
by merlyn (Sage) on Aug 22, 2000 at 21:34 UTC
    When you do that, you need to make sure you're reaping your task kids. Remember that pipe-opens and fork-opens also generate kids that may not be part of your "30 count".

    A better way is to fork your kids, noting their pid as keys in a hash. When you reap a kid, delete it from the hash. If it wasn't part of the hash, it won't affect the count. Then fork new kids (on the block? {grin}) until your hash has 30 keys again.

    I've done this for at least one of my columns, but after 106 by-lines, I can't remember which one anymore. {grin}

    -- Randal L. Schwartz, Perl hacker

      At a guess, one you can't post on your website yet because it is too recent. Anyways, I used a somewhat similar approach in Run commands in parallel in case what merlyn said is opaque to you.

      That way you won't have to wait to find out how to reap a kid. :-)