Linux daemon Parsing

Source: Internet
Author: User

Linux daemon Parsing
First, let's talk about the difference between background processes and daemon processes.

The biggest differences are as follows:

(A) The daemon is completely out of the terminal console, and the background program is not completely out of the terminal (results will still be output to the terminal before the terminal is closed); (B) the daemon will not be affected when you close the terminal console, and the background program will stop as the user exits. You must run the daemon in nohup command & format to avoid the impact. (c) the session group and the current directory of the daemon, and the file descriptor are independent. The back-end operation only performed a fork on the terminal, so that the program was executed in the background, which did not change;
Features of daemon

Daemon is a special process running in the background. It is detached from the terminal, which can prevent the process from being interrupted by signals generated by any terminal, information generated in the execution process is not displayed on any terminal. The daemon periodically executes a task or waits for some events to be processed. Most Linux servers use daemon.

Key Points of daemon Programming

1. Shield some signals related to control terminal operations to prevent the control terminal from being disturbed and exited or suspended before the daemon process is properly started. The Code is as follows:

/* Process possible terminal signals */signal (SIGTTOU, SIG_IGN); signal (SIGTTIN, SIG_IGN); signal (SIGTSTP, SIG_IGN); signal (SIGHUP, SIG_IGN );

2. Run in the background.

/* Is the parent process, ends the parent process, and the child process continues */if (fork () exit (0 );

3. Exit Control terminal and process group:

(1) A process belongs to a process group, and the process group number (PGID) is the process number (PID) of the process leader)

(2) The process in the same process group shares a control terminal. By default, this control terminal is the terminal that creates the process.

(3) The control terminal and process group associated with a process are generally inherited from the parent process. Therefore, the child process is still affected by the parent process terminal, the signal generated by the terminal is sent to all processes in the foreground process group.

For the above reason, we need to let the sub-process completely get rid of the impact of the terminal. We need to call setsid () to make the sub-process a new session leader. The Code is as follows:

setsid();

After setsid () is successfully called, the process that calls this function becomes the new session leader and the new process leader, and is out of relationship with the original process group. Because the session process is dedicated to the control terminal, the process is separated from the control terminal at the same time.

4. Prohibit the process from re-opening the control terminal. The method is to create a sub-process again and let the parent process exit. The sub-process is no longer the session leader, so as to achieve the goal. The Code is as follows:

/* Terminate the first child process, and the second child process continues */if (fork () exit (0 );

5. Disable the opened file descriptor. Because a process inherits the opened file descriptor from the parent process that created it, it is generally not required. If it is not disabled, system resources will be wasted. The Code is as follows:

#define NOFILE  256for(i=0; i<NOFILE; i++)    close(i);

6. Change the current working directory. When a process is active, the file system in which the working directory is located cannot be uninstalled. Therefore, you need to change the working directory of the daemon to the appropriate directory. The Code is as follows:

chdir("/tmp");

7. Reset the file to create a mask. A process inherits the file creation mask from its parent process. It may modify the access permissions of the files created by the daemon. The Code is as follows:

umask(0);

8. process the SIGCHLD signal (sub-process exit signal ). If you do not wait until the child process ends, the child process will become a zombie process and occupy system kernel resources.

/* Set the sub-process exit signal to SIG_IGN to allow the system to help recycle process resources */signal (SIGCHLD, SIG_IGN );
The overall code is as follows:
# Define NOFILE 256 void DaemonMode () {int num = 0; int fd0, fd1, fd2;/* shield possible signals */signal (SIGTTOU, SIG_IGN); signal (SIGTTIN, SIG_IGN); signal (SIGTSTP, SIG_IGN); signal (SIGHUP, SIG_IGN); if (fork () exit (0); setsid (); if (fork ()) exit (0); chdir ("/tmp/httpd"); umask (0); for (; num <NOFILE; num ++) close (num ); /* redirects input and output. Because the descriptors are closed before, the new open values are 0, 1, 2 */fd0 = open ("/dev/null", O_RDWR); fd1 = dup (0 ); fd2 = dup (0); signal (SIGCHLD, SIG_IGN );}
Add the setsid () function:
If the calling process is already the leader of a process group, this function returns an error. To prevent this situation, we usually call fork () to create a sub-process and terminate the parent process. The sub-process continues and calls this function in the sub-process. If the process that calls this function is not a process group leader, the function creates a new session and the process that calls the setsid () function becomes the leading process of the new session, and disconnects from the session group and Process Group of the parent process. Because the session is dedicated to the control terminal, the process is separated from the control terminal at the same time.

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.