gct
gct

Reputation: 14583

What does it take to _guarantee_ that child processes are killed before parent exits in linux?

I do a fair bit of work on streaming real time data and I've found that a process model for isolating different bits of my system from each other works very well. It's often very convenient/terse to be able to popen a child process and read data from it in a streaming fashion for the life of a program. However, if a child process isn't properly wait()-ed on before the parent ends, you can pretty easily end up with zombies or orphans.

What I'm looking for is basically what it would take to get RAII-style guarantees on child processes being properly cleaned up. Ideally without regard to how the parent exits, be it in response to a signal, calling exit(), throwing an exception, whatever. I'd like to sleep easy knowing I can't ever generate a zombie or orphaned process.

Upvotes: 3

Views: 235

Answers (1)

user3629249
user3629249

Reputation: 16540

in your code, when you know the child pid, probably as the returned value from a call to fork() the code can wait for the child to die via:

waitpid( pid, &status, 0);

which could be followed by the appropriate macro invocations to extract the returned status from the child, if the parent process is actually interested in that status.

Note: not waiting for the child process to die can result in creating zombie processes. Which usually take a re-boot to eliminate

Upvotes: 1

Related Questions