Daemon ----- Introduction and Implementation

1 Overview

Daemon is running and is not subject to any process control terminal in the background. There are many typical Unix operating system daemon (their number varies according to need or 20-50), which runs in the background to perform various administrative tasks.
The daemon user terminal is independent of all because, in the case of a daemon is started from the terminal, which may be used with a terminal other users. For example, the user exits after starting the daemon from a terminal, then another person also log on to the terminal. The latter does not want the user during use of the terminal, any error messages received daemon. Likewise, any signal (such as interrupt signal) by the type of terminal should not affect the operation of any daemon started previously in the terminal. While it is easy to let the server running in the background (as long as the shell command line ends with & can), but you should also do some work, let the program itself automatically into the background and does not rely on any terminal.
Daemon has no control terminal, so when certain circumstances occur, whether it is general report information or emergency information to be processed by the administrator, you want to output in some way. Syslog output function is a standard method of such information, it sends information to the syslogd daemon.
To create a Editing
(1) create a child process to terminate the parent process
because the daemon is out of control terminals, so first create a child process to terminate the parent process, making the program a false impression has finished running in the terminal shell inside. After all the work is done in the child, and users can execute other commands in the shell terminal in order to make the program run in the form of a zombie process, formally done out of the control terminal.
(2) creates a new session in the child process
of this step is to create a daemon is the most important step here is to use the system function setsid.
setsid function is used to create a new session, and served as head of the group's conversation. Three roles call setsid Ding: let the process get rid of the original control session, let the process get rid of the control group of the original process and let the process get rid of the control of the primary control terminal.
When calling the fork function, the overall sub-process session copy of the parent process (session, is a collection of one or more process group), process group, control terminals, although the parent process to exit, but the previous session period, the process group the control terminals has not changed, and therefore, that is not the true sense of the two independent open. setsid function can make the process of completely independent, so out of control all other processes.
(3) change the working directory
using a fork to create a child process also inherits the current working directory of the parent process. Because while the process is running, the current directory where the file system can not be uninstalled, therefore, the current working directory into other paths, such as "/" or "/ tmp" and so on. Change the working directory is common functions chdir.
(4) reset the file creation mask
file creation mask means masked when the corresponding bit file creation. Fork function due to the use of the new child process inherits the parent process of file creation mask, which brought a lot of trouble to the child process using file. Therefore, the file creation mask is set to 0, it can greatly enhance the flexibility of the daemon. Function sets the file creation mask that umask, the usual method is to use umask (0).
(5) close the file descriptor or redirect
with a fork a new child process will inherit some of which have opened the file from the parent process there. These files are opened daemon may never be read or write, but they are as consumption of system resources, it can lead to where the file system can not be uninstalled.
According to the type of service divided into the following
system daemons: syslogd, login, crond, at so on.
Network Daemon: sendmail, httpd, xinetd, and so on.
Independent startup daemon: httpd, named, xinetd and so on.
Passive daemon (started by xinetd): telnet, finger, ktalk and so on.

Here Insert Picture Description
Daemon realized according to the above five steps

  1 #include<stdio.h>
  2 #include<stdlib.h>
  3 #include<unistd.h>
  4 #include<sys/types.h>
  5 #include<sys/stat.h>
  6 #include<fcntl.h>
  7 void myDaemond()
  8 {
  9         pid_t pid = fork();
 10         if(pid > 0)   //(1)创建子进程 ,终止父进程
 11         {
 12                 exit(1);
 13         }
 14 
 15         pid_t pid1 = setsid(); //(2) 创建新会话
 16         if(pid == -1)
 17         {
 18                 perror("setsid errror");
 19                 exit(1);
 20         }
 21 
 22         int ret = chdir("/home/");//(3)改变工作目录
 23         if(ret == -1)
 24         {
 25                 perror("chdir error");
 26                 exit(1);
 27         }
 28 
 29         umask(0);//(4)重设文件掩码
 30 
 31         close(STDIN_FILENO);  //(5)关闭文件描述符或者重定向
 32 
 33         open("/dev/null",O_RDWR);
 34         dup2(0,STDOUT_FILENO);
 35         dup2(0,STDERR_FILENO);
 36 
 37 
 38 }
 39 
 40 
 41 int main()
 42 {
 43         myDaemond();
 44         while(1){};
 45         return 0;
 46 }
      

The results, ps aux to see daemon.
76 756 7744 1283 0.0 0.3 Yan? Ss 09:13 0:00 / lib / systemd / systemd --user
Yan 195 904 2584 1284 0.0 0.1? S 09:13 0:00 (SD-PAM)
Yan 110 084 5380 1379 0.3 0.2? R & lt the sshd 0:00 09:13: Yan @ PTS / 0
Yan 1380 0.2 0.2 29660 4932 PTS / Ss 09:13 0:00 0 -bash
Yan 1406 103 4372 0.0 72 ? Rs of 09:14 0:04 ./daemond
Yan 1407 0.0 0.1 46772 3412 pts / 0 R
+ 09:14 0:00 ps aux here for this? That is, without any control terminal

Published 38 original articles · won praise 13 · views 4322

Guess you like

Origin blog.csdn.net/YanWenCheng_/article/details/104012953