$ENV{REMOTE_USER} has asked for the wisdom of the Perl Monks concerning the following question:

When you are forking processes, is there an easy way for a child to check up on the status of it's parent process? Something equivelant to the reverse of the SIG{CHLD} handler used with waitpid().

In my child's code, I'm reading data from a socket using accept() and of course that is blocking (hence the reason why I'm forking the code). If my parent dies, I have no way of checking it (like using ps) until I get more data (which might be a very long time).

Replies are listed 'Best First'.
(chromatic) Re: Checking up on your parents
by chromatic (Archbishop) on Jul 18, 2000 at 03:12 UTC
    The only option I see right now is storing the parent pid ($$) in a variable before forking, and then in the child, moving it into something like $parent_pid. You can then check for it with ps. There might even be some parameter for waitpid that helps.

    You could also use alarm around your select call, and abort if there's no response within 60 seconds.

    Beyond that... I'm really not sure. These are just guesses, and anyone who knows better is welcome to jump in.

Re: Checking up on your parents
by tye (Sage) on Jul 18, 2000 at 05:32 UTC

    You don't have to save $$, just use getppid() and wait for it to go to 1 (assuming a fairly Unix-like system).

    waitpid only works on children.

    The only way I can think that might get the OS to send an asynchronous event (aka signal) to the child when the parent dies is to make the parent the head of a process group. But I don't have my process group docs handy (sorry).

    If you use select, then you don't need to use alarm. But either of these is probably a good choice.

    Also, you can also try to get the parent to trap that it is about to exit and notify the child any way you like. You can use END and signal handlers in the parent for this.

    For extreme cases, you can have a grandparent who's only purpose is to wait for the parent to die. But combining the above options can give you immediate notification for orderly parent death and reasonably quick detection for unorderly parent death.

Re: Checking up on your parents
by $ENV{REMOTE_USER} (Novice) on Jul 18, 2000 at 08:18 UTC
    If you use select, then you don't need to use alarm. But either of these is probably a good choice.

    Yeah, the other reason why I'm forking is so I don't have to use select. It seems kludgy to me for some reason.

    Also, you can also try to get the parent to trap that is about to exit and notify the child any way you like. You can use END and signal handlers in the parent for this.

    Yeah, but you can't trap a kill -9. I have END and SIG{INT} and SIG{TERM} handlers that toast the kid's but I just hate leaving loose ends.

    I suppose I'm just being picky.