I'm trying to create a daemon in python. I've found the following question, which has some good resources in it which I am currently following, but I'm curious as to why a double fork is necessary. I've scratched around google and found plenty of resources declaring that one is necessary, but not why.
Some mention that it is to prevent the daemon from acquiring a controlling terminal. How would it do this without the second fork? What are the repercussions?
According to "Advanced Programming in the Unix Environment", by Stephens and Rago, the second fork is more a recommendation, and it is done to guarantee that the daemon does not acquire a controlling terminal on System V-based systems.
Looking at the code referenced in the question, the justification is:
So it is to ensure that the daemon is re-parented onto init (just in case the process kicking off the daemon is long lived), and removes any chance of the daemon reacquiring a controlling tty. So if neither of these cases apply, then one fork should be sufficient. "Unix Network Programming - Stevens" has a good section on this.
It might be easier to understand in this way:
One reason is that the parent process can immediately wait_pid() for the child, and then forget about it. When then grand-child dies, it's parent is init, and it will wait() for it - and taking it out of the zombie state.
The result is that the parent process doesn't need to be aware of the forked children, and it also makes it possible to fork long running processes from libs etc.
I was trying to understand the double fork and stumbled upon this question here. After a lot of research this is what I figured out. Hopefully it will help clarify things better for anyone who has the same question.
In Unix every process belongs to a group which in turn belongs to a session. Here is the hierarchy…
Session (SID) → Process Group (PGID) → Process (PID)
The first process in the process group becomes the process group leader and the first process in the session becomes the session leader. Every session can have one TTY associated with it. Only a session leader can take control of a TTY. For a process to be truly daemonized (ran in the background) we should ensure that the session leader is killed so that there is no possibility of the session ever taking control of the TTY.
I ran Sander Marechal's python example daemon program from this site on my Ubuntu. Here are the results with my comments.
Note that the process is the session leader after
Decouple#1
, because it'sPID = SID
. It could still take control of a TTY.Note that
Fork#2
is no longer the session leaderPID != SID
. This process can never take control of a TTY. Truly daemonized.I personally find terminology fork-twice to be confusing. A better idiom might be fork-decouple-fork.
Additional links of interest:
Taken from Bad CTK:
"On some flavors of Unix, you are forced to do a double-fork on startup, in order to go into daemon mode. This is because single forking isn’t guaranteed to detach from the controlling terminal."