Separated writing PID from acquiring lock to facilitate forking