1

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.

Fantastic Mr Fox
  • 32,495
  • 27
  • 95
  • 175
  • There's nothing in the C++ standard regarding processes and pids. As such, by definition, there is no "portable" way to implement this kind of functionality. – Sam Varshavchik Dec 14 '17 at 01:24
  • How portable do you need it to be? There may not be a way of doing this on all systems ever, but if not that, perhaps one that covers a few major OS's would be good enough? – David Z Dec 14 '17 at 01:25
  • @SamVarshavchik I figured that. But i thought that someone would have implemented something stable for this at some point. It is weird to me that something as simple as checking a process exists (since separate processes exist on essentially every OS) doesn't have a standard way. Does everyone always implement their own lib for this? – Fantastic Mr Fox Dec 14 '17 at 01:26
  • @DavidZ For my specific purpose, i need it on Windows, QNX and Nucleus. Nucleus is an obscure enough OS that i will probably have to implement this myself anyway, but i was interested in why this doesn't exist. – Fantastic Mr Fox Dec 14 '17 at 01:27
  • Tihs doesn't exist for the same reason why pink elephants don't exist: nobody has bothered to buy enough pink paint, and spend some time painting an elephant pink. – Sam Varshavchik Dec 14 '17 at 01:29
  • @SamVarshavchik If you think that is the answer then add it. Its a pain though given how old and mature processes are ... – Fantastic Mr Fox Dec 14 '17 at 01:31
  • Where are you getting your PIDs from? – 1201ProgramAlarm Dec 14 '17 at 01:36
  • @1201ProgramAlarm Passed from the parent program, or sent through a pipe, or looked up in a file. Not sure what the relevance is? – Fantastic Mr Fox Dec 14 '17 at 01:40
  • My thinking was that if you're creating the process, you'd have an object with a handle or PID or something in it. The existing process libraries seem to have this concept in mind. The use case for "is this process that I did not create still running?" is not a common one. Looks like you've got the start for creating your own library/add-on. – 1201ProgramAlarm Dec 14 '17 at 01:47
  • @1201ProgramAlarm In my specific case I am looking if my parent has died and am committing suicide if it has. I know this could be achieved with better methods, like a heartbeat or similar. But it brought up the general question for me of simply checking if a PID is alive or not. – Fantastic Mr Fox Dec 14 '17 at 01:49
  • @FantasticMrFox The problem with checking whether the parent process still exists is that there is race where the parent could die, a new process is created with the same PID, and then the child process checks for the existence of a process the parent PID. Better to use something like a pipe, and detect when it is closed because the parent exited. – janm Dec 14 '17 at 06:01

0 Answers0