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

fork and Linux/Windows portability

by thelycaeum (Initiate)
on Oct 01, 2012 at 19:58 UTC ( #996731=perlquestion: print w/ replies, xml ) Need Help??
thelycaeum has asked for the wisdom of the Perl Monks concerning the following question:

Is it possible to write a perl fork script in such a way that it is seamlessly portable between windows and linux? The idea is the following: I wrote a perl script that produces the input for another program, calls the program 500 to 10000 times to do a calculation using the input, redirects the programs command line output to an array using something like
@foo = `bar $input_file_name`;
and then calculates the average and the standard deviation of the output stored in the array. This process could be speeded up by forking the external program call (obviously, I guess) which is the only thing that takes a while. What module could I use to achieve a speed up under Linux and Windows as well? I could write it in such a way that it recognizes windows and switches to the presently implemented serial processing I guess, but this would be somewhat unsatisfying.

Comment on fork and Linux/Windows portability
Download Code
Re: fork and Linux/Windows portability
by BrowserUk (Pope) on Oct 01, 2012 at 21:22 UTC

    This should run without change, anywhere there is a threaded perl:

    #! perl -slw use strict; use List::Util qw[ sum ]; use threads; sub joinable { threads->list( threads::joinable ) }; sub running { threads->list( threads::running ) }; my @files = map glob, @ARGV; our $T //= 4; my @results; for my $file ( @files ) { async { return `wc -l "$file"`; }; next if running() < 4; push @results, $_->join =~ m[(\d+)] for joinable; sleep 1 while running >= 4; } sleep 1 while running; push @results, $_->join =~ m[(\d+)] for joinable; print "The average number of lines in all the files is:", sum( @results ) / @results; __END__ C:\test>996731 a*.* The average number of lines in all the files is:77346.75

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    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.

    RIP Neil Armstrong

Re: fork and Linux/Windows portability
by rpnoble419 (Pilgrim) on Oct 01, 2012 at 23:12 UTC

    Depending on how your linux server and the version of Perl on it is setup, I'd suggest that you make a copy of Strawberry Perl and put it in the same directory path as on the Linux box. Especially of the version of Perl under Linux is below 5.12. This means having a c:\usr\bin\perl folder (or what ever the path to you Perl is under Linux) on your windows box. This way you guarantee that the script works on both platforms.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (8)
As of 2014-10-23 05:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (124 votes), past polls