HACKER Nora wrote:
Hi list,

Hello,

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:

From your description you are doing too much. The following code accomplishes the task:

system 'compress *.dbf';

Where system() forks off a process in the background and then waits for it to finish before continuing with your program.

my $pid = fork;

You don't verify that fork() succeeded.

if ( $pid ) {
     wait;

wait() waits for *a* child process, not necessarily *this* child process.

     print "done.\n";
} else {

Are we here because fork() failed or because this is the child process?

     print "starting.\n";
     system "compress *.dbf&";

system() forks off a process which starts a shell and then the shell forks off a process which runs 'compress a.dbf b.dbf c.dbf d.dbf e.dbf etc.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.

Again, you have the same problems as with the previous code.

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.

That is because wait() will only wait for *one* child process.

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?

perldoc perlipc



John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction.                   -- Albert Einstein

--
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