In addition, parents should wait for children before exiting

Yes, that is a good general practice. The most common problem of not doing that is that the shell that launched the command waits for the parent to exit, then displays the next prompt, then the child outputs a bit more, making a confusing display. Things get worse if a child might be reading from the same STDIN. And even if the parent process wasn't launched as a command from an interactive shell, it is often good to not have the parent exit before the children; for example, you often don't want to restart some service or daemon when some children from the last instance are still hanging around.

otherwise children either get killed or stop running and become zombie processes

But neither of those are valid justifications for that practice.

The parent exiting doesn't kill the child. The closest thing to that is that the login process exiting will send SIGHUP to all processes that share that controlling tty. (And your Perl script is pretty darn unlikely to be a login process.)

When a child process exits, it becomes a zombie process until its parent waits for it, or until the parent process exits (because then the child gets inherited by process 1 which scrupulously wait()s for any expired children). So preventing zombies is a good reason to wait() for children, but the one time that it doesn't matter is right before the parent process exits.

- tye        


In reply to Re^2: confusing fork/readline behaviour (wait) by tye
in thread confusing fork/readline behaviour by flipper

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.