Answer the question
In order to leave comments, you need to log in
How to check if a child process is running?
You need to start the process and in the parent process check whether it started. I want to do it with the POCO library something like this:
poco::ProcessHandle handle = poco::Process::launch(path, arg, workingDirectory);
if ( !poco::Process::isRunning(handle) )
{
...
}
fork()
pid is called and remembered. In the child process, it is called execvp()
and then called _exit()
; isRunning()
internally kill(pid, 0)
. kill(pid, 0)
returns 0, that is, the process is running, how can this be? int main(int argc, char** argv)
{
int pid = fork();
if ( pid == 0)
{
int pid = getpid();
int ppid = getppid();
printf("Child process: pid: %d \n", pid);
printf("Child process: ppid: %d \n", ppid);
int res = execvp("", NULL);
printf("Child process: res: %d \n", res);
_exit(72);
}
sleep(3);
if ( kill(pid, 0) == 0 )
{
printf("kill returned 0 \n");
}
return 0;
}
Child process: pid: 3542
Child process: ppid: 3533
Child process: res: -1
kill returned 0
ps -ef | grep 3533
milai 3533 3531 0 15:55 pts/6 00:00:00 /home/milai/project/test/build/testApp
milai 3542 3533 0 15:55 pts/6 00:00:00 [testApp] <defunct>
milai 3561 2579 0 15:57 pts/4 00:00:00 grep --color=auto 3533
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question