Avoid race condition when getting the PID of process