in reply to Re^3: forks for running multiple codes at same time
in thread forks for running multiple codes at same time

In the OP case, where the final intend is to run an external command, the optimum solution would probably be to fork a new process and exec the command.
  • Comment on Re^4: forks for running multiple codes at same time

Replies are listed 'Best First'.
Re^5: forks for running multiple codes at same time
by BrowserUk (Pope) on Feb 09, 2009 at 10:29 UTC

    forking, and then calling system to run the command, means he's forking twice for every command. Maybe 3 times depending what's in $command.

    You can achieve the goal without threads on win32 like this:

    #! perl -slw use strict; my @dates = 1 .. 10; my @pids; (my $cmd = <<'CMD' ) =~ s[\s+][ ]g; -wle"print qq[task $ARGV[0] starting]; sleep rand( 10 ); print qq[task $ARGV[ 0 ] finishing];" CMD while( my $date = pop @dates ) { push @pids, system 1, 'perl.exe', "$cmd $date"; if( @pids >= 5 ) { my $kid = wait; @pids = grep $_ != $kid, @pids; } } waitpid $_, 0 for @pids;

    Since you need to load a new executable, why not just start it in a new process rather than duplicating the current one first?


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Since you need to load a new executable, why not just start it in a new process rather than duplicating the current one first?

      Well, I was thinking about Unix systems where calling fork & exec is the standard (and sometimes only) way to start a new process. On Windows, you are right, it will be quite inefficient.

        I was thinking about Unix systems where calling fork & exec is the standard (and sometimes only) way to start a new process.

        Sorry. I should have added a smiley. I was taking a gentle dig (back) at *nix :)


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.