I was hoping for something as simple as this:
bool SomePortableLib::IsProcessWithPIDAlive(uint32_t PID);
Is the only way to implement something like this?
// Warning! untested.
bool IsProcessWithPIDAlive(uint32_t PID) {
#if defined(WIN32)
// Credit: [1]
HANDLE process = OpenProcess(SYNCHRONIZE, FALSE, pid);
DWORD ret = WaitForSingleObject(process, 0);
CloseHandle(process);
return ret == WAIT_TIMEOUT;
#elif defined(LINUX) || defined(QNX)
// Credit: [2]
struct stat sts;
if (stat("/proc/<pid>", &sts) == -1 && errno == ENOENT) {
return false;
}
return true;
#elif ...
[1] https://stackoverflow.com/a/1591371/1294207 [2] https://stackoverflow.com/a/9153189/1294207
Or is there a portable lib that does this? If not why not?
I noticed that boost
has a (non-official) process lib boost::process but it doesn't look to currently support this either.
Note: I am interested in a c++03
implementation of this, but welcome answers with c++11-17
as well.