HACKER Nora <nora.hac...@stgkk.at> asked:
> I am currently having an issue with background processes. I already
> searched the web and found a way how to principally send a process into
> background, wait for the execution to finish and then go on with the
> main program:
> 
> my $pid = fork;
> if ( $pid ) {
>     wait;
>     print "done.\n";
> } else {
>     print "starting.\n";
>     system "compress *.dbf &";
>     exit;
> }
> 
> The "done." is being issued at the correct point of time, namely when
> really all files have been compressed. Unfortunately, this only
> compresses one file after the other and therefore lacks performance. So
> I tried to send multiple processes into background and have them to be
> executed simultaneously. I tried the following:
> 
> my $pid = fork;
> if ( $pid ) {
>         wait;
>         print "done.\n";
> } else {
>         print "starting.\n";
>         foreach ( glob "*.dbf" ) {
>                 system "compress $_ &";
>         exit;
> }

Have you had a look at the perlipc manpage yet? I'd say it's mandatory reading 
for what you're trying to achieve.

Apart from that, spawning multiple "cruncher" processes makes sense - up to a 
point. Obviously you don't want to spawn more processes than the number of CPU 
cores in your system because then they would be in contention for CPU.

Fortunately, there's already at least one module on CPAN that does most of the 
hard work for you: 
http://search.cpan.org/~dlux/Parallel-ForkManager-0.7.5/ForkManager.pm

HTH,
Thomas

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to