Closed gaborcsardi closed 2 months ago
This is how htop does it: https://github.com/hishamhm/htop/commit/5ee6715843902da1aa40782a9d00f41ca855a2ee
Nowadays this does not work because macOS does not let you read the status of most processes, unless you are the root user.
An alternative would be to call out to /bin/ps
, which is SUID root:
❯ pp <- ps_pids()
❯ system.time(st <- system2(stdout = TRUE, stderr = FALSE, "/bin/ps", c("ax", "-o", "pid,stat", "-p", paste(pp, collapse = ","))))
user system elapsed
0.004 0.018 0.064
❯ writeLines(head(st))
PID STAT
1 Ss
1125 Ss
1127 Ss
1129 Ss
1130 Ss
We might do that in the future if there is a need for it, but for now I am going to close this issue.
Slightly faster for a single pid:
❯ system.time(system2(stdout = TRUE, stderr = FALSE, "/bin/ps", c("-o", "pid,stat", "-p", Sys.getpid())))
user system elapsed
0.001 0.003 0.021
It is only running or stopped or zombie. To get a sleeping status, we would need to iterate over the threads and check their status.