A
A
Anton2017-10-02 10:02:48
linux
Anton, 2017-10-02 10:02:48

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) )
{
   ...
}

Inside, everything works as follows: fork()pid is called and remembered. In the child process, it is called execvp()and then called _exit();
Called isRunning()internally kill(pid, 0).
All this works, the question arises when the path variable is zero, that is, no other executable file should be executed. But for some reason it kill(pid, 0)returns 0, that is, the process is running, how can this be?
Maybe it will be clearer like this:
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;
}

Conclusion:
Child process: pid: 3542 
Child process: ppid: 3533 
Child process: res: -1 
kill returned 0

Conclusion 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

1 answer(s)
T
tsarevfs, 2017-10-02
@Riki-tiki-tavi

It looks like you got a zombie process. The parent process must read the return code using wait/waitpid.
https://ru.stackoverflow.com/a/484142/221454

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question