Re: Wait for background processes to complete




Quoth pgodfrin <pgodfrin@xxxxxxxxx>:

Here's what I want to do - run several commands in the background and
have the perl program wait for the commands to complete. Fork doesn't
do it, nor does wait nor waitpid.

Any thoughts?

Here's a sample program which starts the processes:

while (<*.txt>)
{
print "Copying $_ \n";
system("cp $_ $_.old &") ;

This string contains a shell metachar (&), so system will fork a shell
and wait for it. The shell will run cp in the background, and then exit,
at which point system will return. Unfortunately, the only process which
knew cp's pid was the shell, which has just exitted, so you can't wait
for that process at all (cp now has init as its parent, like any other
orphaned process).

You need to either implement the behaviour you want with fork, exec and
waitpid (it's a little complicated, but entirely possible) or use
IPC::Run, something like

use IPC::Run qw/run/;

my @cmds;

while (<*.txt>) {
print "Copying $_\n";
push @cmds, [cp => $_, "$_.old"];
}

run map { ($_, '&') } @cmds;

This is also safer than system STRING in the case where your filenames
have funny characters in them.

}
print "End of excercise\n";
exit;

Falling off the end is a perfectly valid way to end a Perl program. exit
is usually reserved for exceptional circumstances.

I mean if this were a shell program - this would work:

for x in `ls *.txt`
do
print "Copying $_ \n"
cp $_ $_.old &
done
wait

This works because the shell implements '&' directly, rather than using
a different shell, so it can remember the pids to wait for itself.

Ben

.



Relevant Pages

  • Re: Logging out the window manager
    ... it is when you exit a shell that is a login shell. ... > after the exec. ... commands without exec or an ampersand so the script will wait for the WM ...
    (Debian-User)
  • Re: $? exit status and makefile
    ... > I ran the commands below manually in a shell and the exit status gives ... > me the expected exit status. ... seperate shell. ... The minor problem is that you are running the command in the background. ...
    (comp.unix.shell)
  • Re: $? exit status and makefile
    ... >>I ran the commands below manually in a shell and the exit status gives ... >>me the expected exit status. ... > seperate shell. ... > The minor problem is that you are running the command in the background. ...
    (comp.unix.shell)
  • Re: breaking out of nested loops, pipelines, and coprocesses
    ... that is run is the "current" shell (of course, ... commands are forked). ... command in the pipeline and "exit" only exits the shell that ... runs the while loop (then cmd may get a sigpipe if it trys to ...
    (comp.unix.shell)
  • Re: Redirection issue
    ... 1- execute input commands from standard input, ... code to implement the redirection it does not work anymore. ... And it's not good shell behavior to echo commands anyway. ...
    (comp.lang.c)