The parent needs to call wait()
or waitpid()
to have the child process be "freed" out of the defunct/zombie state.
This could be done in a parent's thread, as well as in a parent's signal handler servering the SIGCHLD
signal received upon a child's death.
Update
Refering the need to have a child reparented to init
to let it live as a damon as stated in a comment by Mellowcandle:
Just fork()
twice. That is: the first fork()
ed process just fork()
s off another one.
Let the secondly fork()
ed process do the exec*()
, and let the first fork()ed
process end.
When then first fork()
ed process ends, its child (the secondly fork()
ed process and with it it successor: the exec*()
'ed one) is inherited by init
which then handles the SIGCHLD
/the death of this child.