A daemon process is usually defined as a background process that
does not belong to a terminal session. Many system services are performed
by daemons; network services, printing etc.
Simply invoking a program in the background isn't really adequate for these
long-running programs; that does not correctly detach the process from the
terminal session that started it. Also, the conventional way of starting
daemons is simply to issue the command manually or from an rc script; the
daemon is expected to put itself into the background.
Here are the steps to become a daemon:
fork()so the parent can exit, this returns control to the
setsid(), fails if you're a process group leader.
setsid()to become a process group and session group
fork()again so the parent, (the session group leader), can exit.
chdir("/")to ensure that our process doesn't keep any directory
umask(0)so that we have complete control over the permissions of
close()fds 0, 1, and 2. This releases the standard in, out, and
sysconf()to determine the limit
_SC_OPEN_MAXtells you the maximun open files/process. Then in a
Almost none of this is necessary (or advisable) if your daemon is being
inetd. In that case, stdin, stdout and stderr are all
set up for you to refer to the network connection, and the
fork()s and session manipulation should not be done (to
inetd). Only the
umask() steps remain as useful.
English to Visayan Cebuano Dictionary
Suggest a Site
Visayan Cebuano to English Dictionary