isProcessAlive function

bool isProcessAlive(
  1. int pid
)

Whether pid currently refers to a live process on this host.

Use this for PIDs read from disk (pidfiles, supervisor records, foreign subtree members). For subprocesses you spawned, prefer holding the Process instance and awaiting Process.exitCode - no PID-recycling race, reactive on exit.

POSIX uses kill(pid, 0). Windows waits zero ms on the process handle, since a handle outlives its process. Another user's process reads dead.

Implementation

bool isProcessAlive(int pid) {
  if (Platform.isWindows) {
    return _withProcessHandle(pid, (handle) {
          final win32.Win32Result(value: signalled) = win32.WaitForSingleObject(
            handle,
            0,
          );
          return signalled == win32.WAIT_TIMEOUT;
        }, access: win32.PROCESS_SYNCHRONIZE) ??
        false;
  }
  return _libcKill(pid, 0) == 0;
}