Re: Wait for background processes to complete



On Jan 13, 9:51 pm, Ben Morrow <b...@xxxxxxxxxxxx> wrote:
Quoth pgodfrin <pgodf...@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

OK - would you have a good example of the fork-system-waitpid method -
not the same one that's in all the other posts or the camel book?

smiles,
pg
.



Relevant Pages

  • Re: How to get system error when calling a perl script.
    ... > The systemfunction causes string to be given to the shell ... The invoker waits until the shell has completed, ... > If waitpid() returns because the status of a child process ... > macros defined by wstat ...
    (comp.unix.solaris)
  • Re: Fork, exec - setsid?
    ... So I came up with the need to fork some processes away ... > of those or the spawned shell. ... The shell will have the pid that forkreturned to the parent. ... > How do I kill the forked away programms without knowing their PID? ...
    (comp.lang.perl.misc)
  • Re: [Umbrella-devel] Re: Getting full path from dentry in LSM hooks
    ... > attacker tries to fork a (root) shell, ... All this does is stop fork(). ... but most shellcodes I've seen ...
    (Linux-Kernel)
  • Re: Wait for background processes to complete
    ... have the perl program wait for the commands to complete. ... This starts a shell, which then starts cp in the background. ... the cp is *started*, the shell exits. ... to use fork and system or fork and exec. ...
    (comp.lang.perl.misc)
  • Trek OCLV question for Mike or another dealer with historical info
    ... I have a Trek from the late '97 through '99 version just prior to ... The original shell wasn't bad, ... bottom bracket, which, in some cases, caused a disbond between the ... That fork was pretty heavy by today's standards (though there were not ...
    (rec.bicycles.tech)