How to check if a forked process is running from program c
I have the pid of a forked process. Now, from my c code (running on Linux), I have to periodically check if this process is still running or exiting. I don't want to use blocking calls like wait()
or waitpid()
. You need (preferably) a non-blocking system call that just checks if this pid works and returns the status of the child.
What's the best and easiest way to do this?
source to share
kill(pid, 0);
This will succeed (return 0) if the PID exists. Of course, it could have existed because the original process ended and something new took its place ... it's up to you if it matters.
Instead, you can register a handler for SIGCHLD
. It will not be affected by the PID that can be reworked.
source to share