I have a child process specified by pid. This process could be:
- Running
- Defunct/Zombie (unreaped)
- Already reaped (and thus no longer exists)
I would like to kill this process and ensure no zombie remains. Currently my code is
kill(pid, SIGKILL);
int temp;
waitpid(pid, &temp, 0);
Would this work?
EDIT: The process specified by pid is a child of my program.
This looks fine so far, but I wonder why you would let case 3 happen. You should perform some bookkeeping, which of your child processes have terminated and are waiting to be reaped.
One way would be to install a handler for
SIGCHLD, setting a flag that awaitpidis in order. That way you guarantee that all pids are actually those of your child processes.