waiting on recurively forked children

B

bernd.web

Hi all,

I am trying to wait like the bash '"wait" on all my forked processes.
A recursive routine that forked off children is called from main.
Using `ps` polling I managed to accomplish this, but how to use "wait"
to wait for (great)grandchildren in the original parent without
waiting during the recursion?
I would not like to wait for completion of children in the recursion
but only in the main.
However, I have not been able to figure out how to do this.
I did have a look at:
http://perldoc.perl.org/perlipc.html#Signals
http://perldoc.perl.org/perlfaq8.html


Has anyone ideas?

do_fork(1);
#continue when all children, and the children of the children etc...
are finished
print "All sub processes completed\n";

sub do_fork {
#fork off children
my $depth = shift;
if ($depth < 10) {
for(my $i = 0; $i < 4; $i++) {
unless ( fork ) {
#do something......
do_fork($depth + 1);
exit;
}
}

Kind regards,
Bernd
 
B

Brian McCauley

Hi all,

I am trying to wait like the bash '"wait" on all my forked processes
A recursive routine that forked off children is called from main.
Using `ps` polling I managed to accomplish this, but how to use "wait"
to wait for (great)grandchildren in the original parent without
waiting during the recursion?
I would not like to wait for completion of children in the recursion
but only in the main.
However, I have not been able to figure out how to do this.
I did have a look at:

I do not believe it is possible as such. Orphaned grandchildren are
adopted by process 1. You can't be informed of their demise.

This has nothing to do with Perl.

Perhaps you should use a FIFO. If each descendant inherits a copy of
the writing end the you can simply wait for EOF on the reading end.

Actually there may be some tricks with process groups or some-such,
but like I said this is not related to Perl.
 
X

xhoster

Hi all,

I am trying to wait like the bash '"wait" on all my forked processes.

On my bash, wait doesn't do what you describe. It doesn't wait for
orphaned grand-children, or at least not in general.

A recursive routine that forked off children is called from main.
Using `ps` polling I managed to accomplish this, but how to use "wait"
to wait for (great)grandchildren in the original parent without
waiting during the recursion?
I would not like to wait for completion of children in the recursion
but only in the main.
Why?

However, I have not been able to figure out how to do this.
I did have a look at:
http://perldoc.perl.org/perlipc.html#Signals
http://perldoc.perl.org/perlfaq8.html

Has anyone ideas?

You could have the children take out shared locks on one file. The
uberparent waits to get an exclusive lock. When it obtains it, all the
children must be gone. Or you could make all the children share the write
end of a pipe or socket. When the read end in the uberparent becomes
readable (eof), all the children have exited.

Xho

--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top