2

I wanted to spawn a child process from my application such that their lifetimes are tied i.e. when my application is closed, the child process is also closed. To achieve the same I used boost::process::child::terminate().

The problem with using terminate() is that the spawned child process doesn't know that it is exiting and thus can't take necessary steps to ensure that the exit is clean (close open file handles, release resources etc.).

Without using interprocess communication, is there a way to detect in the child process that it is being terminated or communicate to child this somehow?

I have tried using the following handlers to catch termination at child end, but without any success:

std::set_terminate(terminateHandler);
std::set_unexpected(unexpectedHandler);
SetUnhandledExceptionFilter(sehExceptionHandler);

_set_purecall_handler(pureCallHandler);
_set_new_handler(newMemoryExceptionHandler);
_set_invalid_parameter_handler(invalidParameterHandler);

_set_abort_behavior(_CALL_REPORTFAULT, _CALL_REPORTFAULT);

signal(SIGABRT, abortHandler);
signal(SIGINT, interruptionHandler);

signal(SIGTERM, terminationRequestHandler);

Any thoughts?

Azeem
  • 11,148
  • 4
  • 27
  • 40
Arun
  • 3,138
  • 4
  • 30
  • 41
  • 1
    "Without using interprocess communication" and "communicate to child this somehow" are conflicting. You should only choose one. Have you sent `SIGTERM` signal to child to make `SIGTERM` handler work? – user7860670 Aug 07 '17 at 11:12
  • As of now I am using boost::process::child::terminate(). I was hoping that boost internally is sending the appropriate signal for terminating the process – Arun Aug 07 '17 at 11:33
  • 1
    Signals terminology is exceptionally misleading. Terminating processes will send `SIGKILL` by calling `kill` method and will just terminate it. While `SIGTERM` though is also sent using `kill` is actually just a request for process to exit that you can handle. – user7860670 Aug 07 '17 at 11:46
  • 1
    There is no portable way to do that (windows knows to different ways), which is why it's not part of boost.process. But kill(SIGTERM) should do the job on posix. – Klemens Morgenstern Aug 13 '17 at 21:24

0 Answers0