Question

Scenario: I'm developing a daemon monitoring application which starts and stops daemons on by request (TCP-IP). To achieve that, after forking, I'm calling setsid() in order to "detach" the new process from the parent (the monitoring process).

When I try to kill one of the processes the monitoring application created it ends up defuncted.

Here's my code:

int retval;
char* arg_list[] = {
  NULL      /* The argument list must end with a NULL.  */
};
retval = fork();
if (retval == 0)
{
    umask(0);
    setsid();
    execv(LBSDPATH, arg_list);
    exit(0);
}

As I understand, defunct/zombie process means that the parent haven't joined with the process yet. don't setsid() solve it ?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top