27

In order to create a service (daemon) we fork the parent and make it to exit while making the child to be the session leader by calling setsid(). Moreover why do we use setsid()? Our orphan process is taken care of by init(though in not all cases) process.

Why do we use setsid()? Is there any relation between setsid() and handling SIGHUP signal?.

Manuel Jordan
  • 1,728
  • 2
  • 16
  • 40
Kalu
  • 469

1 Answers1

19

We use setsid() because if we just kill the parent the child will be killed too, the setsid():

creates a new session if the calling process is not a process group leader. The calling process is the leader of the new session, the process group leader of the new process group, and has no controlling terminal.

The parent is the first process group leader, killing it - or killing the session/terminal - kills the group, which is why we switch the leader (and create a new session while we are at it).

Manuel Jordan
  • 1,728
  • 2
  • 16
  • 40
ZN13
  • 695
  • 1
    Thst true but does it have any realtion with SIGHUP signal as I kow it is send when the controlling terminal is lost – Kalu Nov 04 '15 at 08:00
  • 9
    The parent gets a SIGHUP when the controlling terminal is closed. The child won't get a SIGHUP because it's in a new session. – ZN13 Nov 04 '15 at 08:09