Re: Wait for background processes to complete
- From: pgodfrin <pgodfrin@xxxxxxxxx>
- Date: Sun, 13 Jan 2008 21:10:00 -0800 (PST)
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
.
- Follow-Ups:
- Re: Wait for background processes to complete
- From: Ben Morrow
- Re: Wait for background processes to complete
- References:
- Wait for background processes to complete
- From: pgodfrin
- Re: Wait for background processes to complete
- From: Ben Morrow
- Wait for background processes to complete
- Prev by Date: Re: Wait for background processes to complete
- Next by Date: Re: Wait for background processes to complete
- Previous by thread: Re: Wait for background processes to complete
- Next by thread: Re: Wait for background processes to complete
- Index(es):
Relevant Pages
|
|