-
zombie analysis
Hey,
first i will post my code, then i say what happens when i start the program.
Code:
int fork_service(service_t *s){
int pid;
pid = fork();
if (pid == -1){
perror("ERRfork_service: ");
return -1;
}
if (pid == 0){
// child
int ret;
if ((ret = execvp(s->path, s->flags)) == -1){
perror("ERRfork_service: "); //should never get here
//need to free allocations?
exit(EXIT_FAILURE);
}
}
//parent;
return pid;
}
ok i run this and look at the process table:
#ps -ax
...
9228 pts/5 S+ 0:00 ./a.out
9232 pts/5 Z+ 0:00 [thttpd] <defunct>
9233 ? Ss 0:00 /usr/sbin/thttpd -C /etc/thttpd/thttpd.conf
...
and
#ps -ejH says that the zombie is a child of a.out, and the running thttpd process is a direct child of init.
say if i am right with this:
when thttpd starts it immediately forks a copy of its own, then the "original" thttpd exits, (...and becomes a zombie), and "init" inherits the thttpd child.
now i am out of control of killing the process that i started at some time in the program, which originally was my plan...
is the thttpd behavior like an often used strategy or so?
thanks for any comments,
felix
-
It remains a zombie until you call one of these functions (from the parent process)
#include <sys/types.h>
#include <sys/wait.h>
pid_t wait(int *status);
pid_t waitpid(pid_t pid, int *status, int options);
-
thanks salem, i know that i need to call wait() ... but this is not the point of interest.
just to mention, i resolve the child zombie thing by calling sigaction() and setting SA_NOCLDWAIT (only for 2.6 kernels)
What i really find to be my problem is that I lose control of my child process.
I need to find a way to kill my child and ALL of its descendants, even if my child has already exited and thus all its children got adopted by 'init'
I read something about this might be possible using process groups.