We have encountered daemons in several places already. A daemon is a process that runs in the background, owned by the init process and not connected to a controlling Terminal. The steps to create a daemon are as follows:
- Call fork to create a new process, after which the parent should exit, thus creating an orphan which will be re-parented to init.
- The child process calls setsid(2), creating a new session and process group of which it is the sole member. The exact details do not matter here; you can simply consider this a way of isolating the process from any controlling terminal.
- Change the working directory to the root directory.
- Close all file descriptors and redirect stdin, stdout, and stderr (descriptors 0, 1, and 2) to /dev/null ...