On Feb 1, 2008, at 5:41 PM, ara howard wrote:
On Feb 1, 2008, at 3:21 PM, Uwe K. wrote:
Can anybody explain to me why child processes continue to live after
their parent process has ended?
google ‘zombie process’
The OP was not really describing zombies but instead orphaned processes.
I have no doubt that Ara understands the differences but for other
folks:
A zombie process is a child process that has terminated but is being
ignored by its parent. The process hangs around in the zombie state
until the parent waits for it in order to ‘reap’ its termination status.
A process that gets orphaned when its parent dies will get inherited
by the init process, which will take care of reaping the termination
status of an adopted processes when/if it terminates. An orphaned
process is not notified that it has been orphaned but in theory it
could see that its parent process id has changed (Process.ppid) or
that its parent is init (process id 1).
So while a parent process needs to pay attention to the status of its
children, a child process doesn’t really have to know anything about
its parent.
Ara Howard wrote:
it’s up to you to make sure that children do not outlive their parent.
Ara, are you talking more about making sure that you are reaping
child processes? The idea that your child process will outlive you
is quite common, for example when a daemon is started. It will often
outlive whatever started it.
The typical way to explicitly orphan a process (so that it gets
adopted by init) is a double fork:
fork { fork { do_work }; exit }
The intermediary process forks a child process to do the work and
then exits. The original process waits for the intermediary and the
‘grandchild’ is orphaned and inherited by init. There are more
considerations though to make a true ‘daemon’ process.
A good source of information for this type of programming is Advanced
Programming in the Unix Environment, by Rich Stevens and Stephen Rago
(2nd Edition).
Gary W.