Skip to content
Advertisement

How to detect that a child process has crashed?

A child process can:

  • Exit normally (by exiting using exit(0),exit(22), exit(23)) — This is obviously specific to my application
  • Exit abnormally (code throws an exception, receives an unhandled signal, core dumps, etc…)

I am doing a fork/exec from a parent process and looping on waitpid, when I detect that child process has exited I would like to determine the reason it exited.

Currently I check WEXITSTATUS(status) (where status is returned by waitpid) to determine exit code.

Is there a way to reliably detect if child exited abnormally?

Advertisement

Answer

You can check for WIFSIGNALED(status). For testing this check out Test cases in C for WIFSIGNALED, WIFSTOPPED, WIFCONTINUED.

Of course you can also do a positive check for normal termination with WIFEXITED(status).

Advertisement