0

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()??

alk
  • 69,737
  • 10
  • 105
  • 255

1 Answers1

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