Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

The unix documentation I have read says you can call waitpid with a negitive pid to wait on an entire process group. However, the perl docs for the function of the same name don't mention this.

The following program tests this out:

use strict; if (my $pid = fork) { # parent setpgrp($pid, $pid); waitpid($pid, 0); while (waitpid(-$pid, 0) != -1) {} exit; } else { # child if (!(my $pid = fork)) { # grandchild sleep(1) while 1; } }
Unfortunately it doesn't seem to work on my system (perl 5.6, Tru64). The parent process exits, without waiting for the grandchild process. Is there another way of waiting on a process group?

Replies are listed 'Best First'.
Re: waiting on a process group
by kschwab (Vicar) on Jul 02, 2001 at 02:40 UTC
    The grandchild process' parent is exiting, which causes the grandchild to be inherited by the init() process, and hence, no longer in the grandparent's process group.

    This "double fork" technique is even used this way on purpose in some code. ( for systems that lack setpgrp )

    It's probably easier to demonstrate the process group waiting by spawning multiple children...

Re: waiting on a process group
by Anonymous Monk on Jul 02, 2001 at 02:57 UTC
    The unix documentation I have read says you can call waitpid with a negitive pid to wait on an entire process group.

    My documentation says something slightly different, it says that a negative pid waits on a child process in the process group, not an entire process group just your children. You should be able to wait on your children with -$pid but not your grandchildren.