Checking if a process has terminated?
I am confused here.
What I want to do is checking if a child process has terminated or if it is still running.
Basic, right? Well, it seems not so much.
First research seems to indicate that using waitpid can achieve this. It is important to note that I do not want to wait until a process has exited, but merely check if it has (and remove zombie processes).
The manual says that by checking the status with WIFEXITED, I should know if a process has exited by means of a return statement or exit function (at least to a degree).
Now, this confuses me since this...
(background = true)
waitpid(child, &status, (background ? WNOHANG : 0));
printf("WIFEXITED: %d, WEXITSTATUS: %d, WIFSIGNALED: %d, WTERMSIG: %d, WCOREDUMP: %d, WIFSTOPPED: %d, WSTOPSIG: %d, WIFCONTINUED: %d",
WIFEXITED(status), WEXITSTATUS(status), WIFSIGNALED(status), WTERMSIG(status), 0/*WCOREDUMP(status)*/, WIFSTOPPED(status),
...doesn't seem to work. WIFEXITED seems to be set to 1, even though the process is still running.
I can easily verify this by running ps.
So I thought about using waitid. Unfortunately, for some reason (perhaps the system I am using is outdated?), the siginfo_t structure and P_PID constant are undeclared in the headers (well, I tried including them, but gcc is giving me errors). So that didn't work either.
So color me confused. How do I figure out if a child has terminated and remove it as a zombie process? It doesn't matter if it exited normally or some abnormal means. Both should be detected.
Any information would be welcome.