Introduction
I am writing a C monitoring program that executes a fork() and exec() loop. However, I need to check whether the child process has completed or not, without blocking the main process, that is, the monitoring program. Something like that:
Main process
pid_t child_pid = fork(); if (child_pid == 0) exec(bar); while (1) { if (the child process has finished) foo(); else bar(); }
What i tried
Given the fact that I have a child pid, I tried the following:
Making a kill call with signal 0 and checking errno :
if (kill(child_pid, 0) == -1 || errno == ESRCH) , which, I think, is not a good way to track the status of a child process, given that it is not safe from the race conditions. Moreover, it did not work, or at least it seemed so.
Check with stat(2) whether proc/child_pid . All the above negative arguments are true, and in this case, plus that this method is slower.
waitpid(2) . Unfortunately, it blocks the main process.
Question
Is there any other way to get such information? Or maybe I missed something from the solutions that I have already tried?
c process fork monitoring
0x450
source share