Daemon (Daemon)

Source: Internet
Author: User

The concept of Guardian process

The daemon (Daemon) is generally designed to protect the normal operation of our programs/services, and start the program/recovery service again when the program is closed, abnormally exited, and so on.
For example, the daemon of the HTTP service is called the daemon of the Httpd,mysql service called Mysqld.

Or sometimes we need to let our program/service run uninterrupted, and after shutting down the terminal can also silently run in the background, in addition to this:

nohup ./xxx &

, or it can be written as a Daemon program, such as a server.

Many server processes are written as Daemon programs, such as Nginx, Redis, Apache, and so on.

Linux generally put the daemon in, the /etc/init.d/ command to start the service is generally /etc/init.d/* {start|stop|status|restart} .

The daemon exists from the terminal. When you execute a command ps -ef , the Daemon's PPID (parent process ID) is 1,tty (terminal) ??? .

Creation of the daemon process

A function can be called in Linix int daemon(int nochdir, int noclose); to make the program a daemon. Use the "Men" command (Manual command) to query: man daemon .

By principle we can also create a daemon manually.

Related concepts:

    • Process Group: Process collection, each process group has a leader (Leader) whose process ID is the process group ID.
    • session: A collection of process groups, each with a leader whose process ID is the conversation group ID.
    • control terminal (controlling Terminal): Each session can have a separate control terminal, which is connected to the control terminal Leader is the control process (controlling processes).
Fork

The parent process of the daemon is the init process, which, when it is created, first fork out a child process from the parent process, exiting the parent process, when the child process becomes orphaned, it becomes the child process of the init.

The child process inherits the parent process's session, the process group, the control terminal, the file descriptor, and so on.

SetID

By setid() Creating a new session, it is also separated from the original process group, session, and control terminal to become the leader of the conversation. At this point it may again apply for a control terminal, so we fork again, and only keep the new sub-process, so it is not the conversation leader, you can not apply to control the terminal.

Close (FD)

The file descriptor inherited from the parent process is then closed. At least close the 0,1,2 three file descriptors, respectively, corresponding to stdin, stdout, and stderr. However, it is usually used sysconf(_SC_OPEN_MAX) to get the maximum number of file descriptors allowed by the system, and then all close off.

Umask (0)

The file mask is set to be free to create read-write files and directories without being affected by the umask of the parent process.

ChDir ("/")

The daemon is generally executed until the system shuts down, and the directory in which it resides cannot be unloaded (unmounted) while it is running. By moving its working directory to the root directory, the directory used is allowed to be uninstalled. It does not have to be the root directory (in which case the Super privilege is required), you can choose a path that does not need to be uninstalled.

voidDaemon () {//Fork out a sub-process. pid_t pid = fork ();//fork failed.     if(PID = =-1) {perror ("fork"); Exit1); }//exits the parent process.     if(PID) {Exit (0); }//Create a new session.     //The child process becomes the leader of the new session and process group.     if(Setsid () = =-1) {perror ("Setsid"); Exit1); }//Fork again, the new child process is no longer the conversation leader. PID = fork ();if(PID = =-1) {perror ("fork"); Exit1); }if(PID) {Exit (0); }//Turn off file descriptors inherited from the parent process.     intMAX_FD = sysconf (_sc_open_max); for(inti =0; i < MAX_FD;    ++i) {close (i); }the//settings file creates a permission mask and does not want to be restricted by the parent process's mask. Umask0);//Set the current working directory to the system root directory. ChDir"/");}

Daemon (Daemon)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.