My Profile Photo

Sheogorath's Blog

Reasons behind double forking

Today I learned that the reason for double forking of a process, when trying to create a deamonized process is due to the fact that creating processes of the processes that starts a new session can re-attach a terminal to that process. This is unwanted behaviour for a daemon, which being useful for things like a login-daemon. To prevent this from happening the first forked process creates a new session and then forks again, before killing itself and leaving its orphaned child process to the init-process. Now in its own session its no longer possible for the processes of the old session to attach to this process.

I came across this due to an article shared by JP Mens about “Understanding daemons”, which explains the process in depth. I can highly recommend that short read.