Skip to content
Prev 55732 / 63424 Next

Detecting whether a process exists or not by its PID?

On 08/31/2018 01:18 AM, Henrik Bengtsson wrote:
kill(sig=0) is specified by POSIX but indeed as you say there is a race 
condition due to PID-reuse.? In principle, detecting that a worker 
process is still alive cannot be done correctly outside base R. At 
user-level I would probably consider some watchdog, e.g. the parallel 
tasks would be repeatedly touching a file.

In base R, one can do this correctly for forked processes via 
mcparallel/mccollect, not for PSOCK cluster workers which are based on 
system() (and I understand it would be a useful feature)

 > j <- mcparallel(Sys.sleep(1000))
 > mccollect(j, wait=FALSE)
NULL

# kill the child process

 > mccollect(j, wait=FALSE)
$`1542`
NULL

More details indeed in ?mcparallel. The key part is that the job must be 
started as non-detached and as soon as mccollect() collects is, 
mccollect() must never be called on it again.

Tomas