Code implementation of daemon and daemon

1. Conceptual understanding:

A daemon is a special process running in the background, also known as a wizard process. It is a long-lived process, often started when the system is bootstrapped, and terminated only when the system is shut down. There is no controlling terminal, just running in the background. Linux has many daemons that perform daily business activities and are processes that are not controlled by the terminal.

2. The role of the daemon process:

Most servers in Linux are implemented as daemons. For example, Internet server inetd, Web server httpd, etc. At the same time, the daemon performs many system tasks. For example, the job planning process crond, the printing process lpd, etc.

The programming of the daemon itself is not complicated. What is complicated is that the implementation mechanisms of various versions of Unix are different, resulting in inconsistent programming rules for the daemon in different Unix environments.

3. Summary of daemon features:

  • Background process

  • Daemons must be isolated from the environment in which they run❶

  • The way the daemon is started is special❷


These environments include unclosed file descriptors, controlling terminals, session and process groups, working directories, and file creation masks, etc. These environments are usually inherited by the daemon process from the parent process (specifically the shell) that executes it.


It can be started from the startup script /etc/rc.d when the Linux system starts, can be started by the job planning process crond, and can also be executed by the user terminal (usually a shell).


Apart from these particularities, daemons are basically no different from ordinary processes. Therefore, writing a daemon process is actually transforming an ordinary process into a daemon process according to the characteristics of the daemon process mentioned above. It is easier if the reader has a deeper understanding of the process.

4. Write the daemon process:

The programming rules for daemons in different Unix environments are not consistent. Fortunately, the programming principles of daemons are actually the same, the difference lies in the specific implementation details. This principle is to meet the characteristics of the daemon.

The writing and steps of the daemon process:

1.fork子进程,而后父进程退出,此时子进程会被init进程接管。
2.修改子进程的工作目录、创建新进程组和新会话、修改umask。
3.子进程再次fork一个进程,这个进程可以称为孙子进程,而后子进程退出。
4.重定向孙子进程的标准输入流、标准输出流、标准错误流到/dev/null。

After completing the above 4 steps, the final grandchild process is called a daemon process.


#include <sys/types.h>
#include <signal.h>
#include <unistd.h>
#include <syslog.h>

#define MAXFD 64

void daemon_init(const char* pname, int facility)
{
  int i;
  pit_t pid;

  /*fork()一个子进程,并终止父进程*/
  if(pid=fork())
  {
    exit(0);
  }

  /*setsid()调用创建了一个新的进程组,调用进程成为该进程组的首进程。
    这样,就使该进程脱离原来的终端,成为了独立终端外的进程*/
  setsid();

  /*忽略SIGHUP信号,重新fork。
    这样使进程不再是进程组的首进程,可以防止在某些情况下进程意外的打开终端而重新与终端发生联系。*/
  signal(SIGHUP,SIG_IGN);
  if(pid=fork())       //fork()终止第一子进程
    exit(0);

  /*第二子进程*/
  daemon_proc = 1;

   /*改变工作目录,清楚文件掩码。
    改变工作目录主要是为了切断进程与原有文件系统的联系。
    并且保证无论从什么地方启动进程都能正常工作。清除文件掩码是为了消除进程自身掩码对其创建文件的影响。*/
  chdir("/");      //将工作目录设定位“/”
  umask(0);

  /* 关闭全部已打开的文件句柄。
    这是为了防止子进程继承在父进程中打开的文件而使这些文件始终保持打开从而产生某些冲突。*/
  for(i = 0; i<MAXFD; i++){
    close(i);
  }

  /*打开log系统*/
  openlog(pname, LOG_PID, facility);
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325847436&siteId=291194637