Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^2: forks for running multiple codes at same time

by Eyck (Priest)
on Feb 09, 2009 at 07:37 UTC ( #742366=note: print w/ replies, xml ) Need Help??


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

This is untrue.

This is only true for Win32, where the fork() call doesn't even really exists (it's poorly faked using threads), on every other platform fork() costs almost the same, and provides many valuable additional benefits (like better memory protection, better management etc etc)

Not to mention, he's spawning only 5 (count'em - five) processes, what would be cummulative gain on that, even on win32?


Comment on Re^2: forks for running multiple codes at same time
Re^3: forks for running multiple codes at same time
by weismat (Friar) on Feb 09, 2009 at 07:54 UTC
    I agree that there is no difference if you would create as many threads as forked processes.
    The difference is that you need to call fork once for every planned task - whereas in a thread model you can share data and thus you would start threads only 5 times depending on the number of workers as the workers can get the data from a queue instead of a function parameter.
      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.

        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.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (8)
As of 2014-09-19 11:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

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











    Results (136 votes), past polls