Why do we need to fork()
twice to create a daemon of a python server? Can't the same be done using a single fork()
without subsequently setting it as a session leader by using os.setsid()
??
Asked
Active
Viewed 271 times
0

alk
- 69,737
- 10
- 105
- 255
-
It's not really Python specific and is answered in http://stackoverflow.com/questions/10932592/why-fork-twice – NPE Jan 18 '15 at 13:14
-
Why not just fork once and then exit the parent? – Sandeep Hari Hara Bhat Jan 18 '15 at 13:21
-
Did you read the answers to the linked question? – NPE Jan 18 '15 at 13:30
1 Answers
1
It's done to ensure that the PID!=SID which ensures that the process cannotacquire tty(i.e. the terminal) to effectively be called a daemon process.
A single fork results in the child having same SID as it's parent which means that it can acquire the tty if it's parent had such a permission or rights. So just exiting the parent may not help the cause.
The best method to avoid this is to setsid to the first child and fork it and exit the first child too.

alk
- 69,737
- 10
- 105
- 255