Saturday, March 9, 2013

daemon

Daemon is a process running at background without a control terminal.  The name is derived from physicist James Maxwell's demon (a thought experiement for 2nd law of thermodynamics).  Daemons are also superbeing in Greek's mythology.  Daemons are not god (kernel) but perform tasks supplementing gods.

A daemon has 2 requirements: (1) it must run as a child of init, (2) it must not have a control terminal.  In linux, a process performs the following steps to become a daemon:

1. Call fork() to spawn a child process
2. In the parent, call exit().  The parent, which is the process group leader terminates.  The child process is not the process group leader and thus able to perform setsid() to create a session.  This ensure the child process will be associate with a control terminal create any any other porcess in the same process group.
3. Change working directory to root.  This allows administrator to unmount non-root filesystems or otherwise will be held by daemons.
4. Close all file descriptor to release all files.  Open only standard in, out and err.  Redirect them to /dev/null

No comments: