Detailed explanation of the daemon and daemon

Source: Internet
Author: User
Tags power of power

Detailed explanation of the daemon and daemon
Daemon Concept

The daemon, also known as the genie process, is a special process running in the background. It is independent from the terminal and periodically executes a task or waits for a thing to happen. Daemon is a very useful process. For example, many servers run in the background to the power of power, waiting for the client to connect and handle related problems.

The daemon process in the system is usually identified by d.

The key step to creating a daemon is to call the setsid function to create a new session, which is called a control process.

Note: The current process that calls setsid to create the daemon must not be the leader of the Process Group; otherwise,-1 is returned. This condition is ensured by fork generation of sub-processes in the current process, the parent process exits directly, and the child process can call setsid.

The result of successfully calling the setsid function is:

1. Creates a new session and the current process is called the leader. The current process id is the session id.

2. Create a new process group. The current process is the leader of the Process Group, and the current process id is the process group id.

3. If the current process originally has a control terminal, it is called a process without terminal control.

Create a daemon

1. Call umask to set the blocked characters to 0.

2. Call fork and the parent process exits.

3. Call setsid to create a session. Result 1: The Calling process becomes the first process of the session. 2. The Calling process becomes the process group leader. 3. The Calling process has no control terminal.

4. Change the current directory to the root directory.

5. disable unnecessary file descriptors

6. Ignore the SIGCHID Signal

Generate daemon code
# Include <signal. h> # include <stdlib. h> # include <fcntl. h> # include <sys/stat. h> # include <stdio. h> # include <unistd. h> void my_daemon () {int I; int fd0; pid_t pid; struct sigaction sa; umask (0 ); // set the file mask to 0 if (pid = fork () <0) {} else if (pid! = 0) {exit (0); // terminate the parent process} setsid (); // set the new session sa. sa_handler = SIG_IGN; sigemptyset (& sa. sa_mask); sa. sa_flags = 0; if (sigaction (SIGCHLD, & sa, NULL) <0) {// The Registration sub-process exits and ignores the signal return;} if (pid = fork ()) <0) {// fork again, stop the parent process, and ensure that the child process is not the first process, so as not to associate printf ("fork error! \ N "); return;} else if (pid! = 0) {exit (0);} if (chdir ("/") <0) {// change the working directory to the root printf ("child dir error \ n"); return;} close (0); fd0 = open ("/dev/null ", o_RDWR); // Disable Standard input and redirect all standards (input and output errors) to/dev/null dup2 (fd0, 1); dup2 (fd0, 2 );} int main (int argc, char const * argv []) {my_daemon (); while (1) sleep (1); return 0 ;}

Note: The above Code sets the daemon based on the definition method.

 

#include <stdio.h>#include <unistd.h>int main(int argc, char const *argv[]){    daemon(0,0);    while(1);    return 0;}

This is a function provided by the system to set the daemon process.

As shown in the figure, the daemon is out of the terminal.

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.