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

Situation, I have a parent process that forks a number of childeren.
Each child create it's own dbi-connection to the DB.

This all works fine except when I kill the parent, the childeren aren't killed.
The childeren receive the ppid 1.

This gives me some problems, when I restart the parent, new childeren are forked but the old one still exists.

What is the best way to solve this problem.

Replies are listed 'Best First'.
Re: dbi and fork
by busunsl (Vicar) on Aug 28, 2001 at 11:30 UTC
    When you fork off a child, you get the pid of it.
    One way would be to send each child a signal, though signal handling is reported to be not very stable.

    Another way could be to kill the whole process family when the parent is killed.

    Yet another way is to establish some communication (pipes, sockets) between parent and children.

    Next way could be to use a framework like poe.

Re: dbi and fork
by jlongino (Parson) on Aug 28, 2001 at 18:27 UTC
    Another way is to have each child monitor the parent:

    $ppid = getppid;

    and then terminate when the parent does:

    while (getppid == $ppid) { # process until parent dies }

    If the code and the comments disagree, then both are probably wrong. -- Norm Schryer

Re: dbi and fork
by perrin (Chancellor) on Aug 28, 2001 at 19:09 UTC
    There's some nice code for a forking server that monitors the children and cleans up after itself in The Perl Cookbook.