Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Re^2: confusing fork/readline behaviour (wait)

by tye (Sage)
on Aug 14, 2015 at 14:18 UTC ( #1138586=note: print w/replies, xml ) Need Help??

in reply to Re: confusing fork/readline behaviour
in thread confusing fork/readline behaviour

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        

  • Comment on Re^2: confusing fork/readline behaviour (wait)

Replies are listed 'Best First'.
Re^3: confusing fork/readline behaviour (wait)
by (anonymized user) (Curate) on Aug 14, 2015 at 18:55 UTC
    Yes I agree - the kill case is rather exceptional, so for clarity I'll strike through it. The case where the child doesn't zombify because the parent exits immediately is also exceptional, as you say.

    One world, one people

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1138586]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2020-02-18 02:05 GMT
Find Nodes?
    Voting Booth?
    What numbers are you going to focus on primarily in 2020?

    Results (74 votes). Check out past polls.