Home > Net >  Kill process by pid with boost process
Kill process by pid with boost process

Time:10-10

I do want to kill a process I have a pid of, so my code is the following:

pid_t pid = 28880;
boost::process::child process { pid };
std::cout << "Running: " << process.running() << "\n";
process.terminate();

I noticed though that running() always returns false (no matter what pid I take) and based on the source code then terminate isn't even called.

Digging a little bit deeper it seem to be the linux function waitpid is called. And it always returns -1 (which means some error has occured, rather than 0, which would mean: Yes the process is still running).

WIFSIGNALED return 1 and WTERMSIG returns 5.

Am I doing something wrong here?

CodePudding user response:

Boost process is a library for the execution (and manipulation) of child processes. Yours is not (necesarily) a child process, and not created by boost.

Indeed running() doesn't work unless the process is attached. Using that constructor by definition results in a detached process.

This leaves the interesting question why this constructor exists, but let's focus on the question.

Detached processes cannot be joined or terminated.

The terminate() call is a no-op.

I would suggest writing the logic yourself - the code is not that complicated (POSIX enter image description here

  • Related