4

I'm looking for a way for basic management of processes in a cross platform way in C++, but I can't use external libraries (like boost::process). The scenario is this - i'm calling a child process with std::system, now in case the parent process is interrupted with a signal i need to kill the child process before quitting, to avoid leaving it as a zombie. I've read up on signal handling, but i'm not sure how to kill a process created with std::system in the handle function. Is there another/better way to do this?

Vlad Keel
  • 372
  • 2
  • 13
  • If the parent dies, the child becomes an orphan, not a zombie. A zombie is a dead child that isn't reaped. – Kerrek SB Apr 21 '16 at 11:04
  • The solutions to this are system dependent. From your description, you appear to be on a unix (e.g linux, BSD, etc) or similar. Is it POSIX? If so, you'll have a set of platform-related functions you can use to find/kill processes (albeit, some aspects of posix are loosely specified, so implementations vary between systems). If you have a mix of target systems (say, posix and non-posix) you'll definitely need different solutions for different targets. There is no solution in standard C++ other than using third-party libraries (which, in turn, take care of the platform dependencies) – Peter Apr 21 '16 at 11:14

2 Answers2

1

C++ has recently got semantics for threading, but it does not (yet ?) have semantics for processes. Therefore, it doesn't provide any facility for killing a process running on your system.

The only viable approach is therefore to let the parent process send some kind of signal to the child process, and make the child process terminate itself upon signal reception. There are multiple ways of doing this signaling (e.g., through OS signals, through standard IPC primitives, through networking) but standard C++ doesn't provide any portable mechanism for most of them. Therefore, you will end up by choosing to use an external library (e.g., Boost, Qt, etc.) or to restrict your implementation to a set of supported systems (e.g., POSIX OSs, in which case you could even direclty invoke kill).

Claudio
  • 10,614
  • 4
  • 31
  • 71
1

Process management is specific for every platform. The classic way (If you don't want use external libraries) is write code for every platform and separate it with compile-time preprocessor conditions:

// For both OS X and iOS
#ifdef __APPLE__
   // includes or code
#endif

// For Windows:
#ifdef __MINGW32__
   // includes or code
#endif    

// For Linux:
#ifdef __linux__
  // includes or code
#endif

Here's more solutions: How to identify platform/compiler from preprocessor macros?

Community
  • 1
  • 1
rootatdarkstar
  • 1,486
  • 2
  • 15
  • 26