2

boost/process.hpp provides a nice mechanism to spawn and manage processes.

It provides a child.terminate() method to send SIGKILL to a child.

How would I alternatively send SIGINT or SIGTERM to a child process?

CraigDavid
  • 1,046
  • 1
  • 12
  • 26
  • 2
    If you are on a Posix system: `kill(child.id(), SIGTERM);`. I don't think there is a portable way to do it though. – Ted Lyngmo Mar 02 '22 at 15:30

1 Answers1

2

Looks like you can do:

#include <boost/process/child.hpp>

pid_t pid = my_child.id ();
kill (pid, SIGINT);

The documentation states that id is a private member function, but in practise it seems not to be.

There's also:

native_handle_t native_handle() const;

But what that actually returns isn't documented. On Windows it's most likely a process handle, but on *nix there's no such thing of course.

... And Ted beat me to it :)

Paul Sanders
  • 24,133
  • 4
  • 26
  • 48
  • :-) I just found `Windows\System32\TASKKILL.EXE /PID ` that may be of use on Windows machines. I have no idea how it does it in code though. – Ted Lyngmo Mar 02 '22 at 15:36
  • @TedLyngmo It does? In the page I linked to, I found it lurking under 'child private member functions'. But it makes no sense for it to be private, so maybe that's just an oversight. Edit: Oh sorry, just read your updated comment. We are in agreement then. – Paul Sanders Mar 02 '22 at 15:40
  • I was looking [here](https://www.boost.org/doc/libs/release/doc/html/boost/process/child.html) and there it only has `public:` members (but an erroneous comment about "private") – Ted Lyngmo Mar 02 '22 at 15:41
  • @TedLyngmo Windows does have the concept of process IDs. `OpenProcess` takes a process ID and returns a process handle. Once you have that, you can kill it (if you have sufficient privileges - you need to be running 'elevated` I believe). – Paul Sanders Mar 02 '22 at 15:48
  • Yes, but `TASKKILL.EXE` does not kill it unconditionally (with `TerminateProcess`) unless `/F`is used. It does "something" to signal the process to close itself (perhaps it uses `SendMessage`) so the program goes through the normal shutdown procedure. "Do you want to save..." etc. – Ted Lyngmo Mar 02 '22 at 15:51
  • 1
    Wow! Thanks @TedLyngmo and PaulSanders. Using your feedback I was able to fix a nasty bug. I learned that kill(0, SIGTERM), would send SIGTERM to all processes in the same group as the master process. Boost couldn't kill the children, because they were ignoring SIGKILL. – CraigDavid Mar 02 '22 at 15:55
  • 2
    @WilderField You can also do `kill(-child.id(), SIGTERM);` to send the signal to all processes in the same process group as `child` which is probably more correct since any process can detach itself from the parent process and start a process group of its own if it wishes. – Ted Lyngmo Mar 02 '22 at 16:02
  • 1
    @TedLyngmo Re `TASKKILL`: Ah yes, I forgot about that. Without `/F` it probably just calls `PostMessage (hSomeWindowThatImNotSureHowItIsDerived, WM_QUIT, ...);` (don't wanna hang the sending app!) Once upon a time, I read one of Raymond Chen's blog posts about how Task Manager does it, but now I can't find it. – Paul Sanders Mar 02 '22 at 16:06