Hi Nora, On Thursday 24 Jun 2010 10:15:11 HACKER Nora wrote: > Hi list, > > 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; > } > > This behaves as expected, from the "simultaneous" and "background" point > of view - but my big problem is now that the message "done." is being > issued as soon as all the compression processes are ISSUED instead of > when they are FINISHED. > > Could anybody please point me into the right direction how to best solve > that issue? Maybe I should fork all the single compression processes to > get a pid for each, put them into an array and check with a loop whether > they still exist (process running)? Would there be another / easier / > more efficient way? >
Look into http://perldoc.perl.org/functions/wait.html and http://perldoc.perl.org/functions/waitpid.html . Regards, Shlomi Fish -- ----------------------------------------------------------------- Shlomi Fish http://www.shlomifish.org/ List of Portability Libraries - http://shlom.in/port-libs God considered inflicting XSLT as the tenth plague of Egypt, but then decided against it because he thought it would be too evil. Please reply to list if it's a mailing list post - http://shlom.in/reply . -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/