Simple implementation of a daemon (Daemon)

Source: Internet
Author: User

The learning process has to mention a special process running in the background-daemon. (also known as the sprite process). Linux systems start with many system service processes that do not control the terminal and do not interact directly with the user. Other processes are created when a user logs on or running a program, terminates at the end of a run or when the user logs off, but the system service process is not affected by user logon logoff, they are always running and we are calling this process a daemon.

The daemon is independent of the control terminal and periodically performs some sort of task or waits for certain occurrences to be handled. Daemons are a very useful process. Most Linux servers are implemented with 1 daemon processes, such as Internet server inetd, Web server httpd, and so on. In addition, daemons can accomplish many system tasks, such as the crond of the job planning process.

You can use the PS AXJ command to view the processes in the system.

650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M00/84/5F/wKiom1eO5-ewU5CjAACd96-nEj0626.png-wh_500x0-wm_3 -wmp_4-s_2602678297.png "title=" Capture 1. PNG "alt=" Wkiom1eo5-ewu5cjaacd96-nej0626.png-wh_50 "/>

(Because too much is only part of the interception)

You can also use the relevant parameters to have a selective view. Parameter A indicates that not only the process of the current user is listed, but also all other users ' processes, the parameter x indicates that not only the process of the control terminal is listed, but also all the processes that have no control terminal, and the parameter J indicates that the information related to the job control is listed.

Use the parameter X to view.

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/84/60/wKioL1eO7JOz-8efAACSXXdu-n8483.png "title=" Capture 2. PNG "alt=" Wkiol1eo7joz-8efaacsxxdu-n8483.png "/>

Once you understand the concept of daemons, you can also write a daemon process.

The most critical step in creating a daemon is to call the SETSID function to create a new session and become the session Leader.

The current process does not allow the leader of the process group until this function is called, otherwise the function returns-1. To ensure that the current process is not a leader of the process group, just fork and call Setsid. The child process created by fork and the parent process in the same process group, the process group leader must be the first process of the group, so the child process cannot be the first process in the group, and calling Setsid in the child process will not be a problem.

You can create a daemon by following these steps:

1, call the Umask function to set the file mode to create a shield word to 0;

2, call fork, the parent process exit (exit);

3, call Setsid to create a new session;

4. Change the current working directory to the root directory;

5. Close the file descriptor that is not needed;

6., other: Ignore the SIGCHLD signal.

#include  <stdio.h>  #include  <signal.h>  #include  <unistd.h> # include <stdlib.h>  #include  <fcntl.h> #include  <sys/stat.h> void  creat_daemon ()  {     int i;     int fd0 ;     pid_t pid;     struct sigaction sa;       umask (0);       if (  (pid =  Fork ())  < 0 ) {      }else if  (pid != 0) {           exit (0);   //Terminate parent Process       }        setsid ();  //set up new session       sa.sa_handler = sig_ign;     sigemptyset (&sa.sa_mask);     sa.sa_flags =&nBsp;0;     if ( sigaction (sigchld, &sa, null )  <  0 ) {       return;     }         if (  (Pid = fork ()) <0)  {printf ("fork error!\n");               return;      }else if ( pid != 0)      {           exit (0);     }         if ( chdir ("/")  < 0 )      {printf ("Child  dir error\n ");               Return;      }close (0);      fd0 = open ("/dev/ Null ", &NBSP;O_RDWR);  dup2 (fd0,  1);       dup2 (fd0, 2);   } int main ()  {      creat_daemon ();      while (1)         {              sleep (1 );        }    }


This article is from the "July boreas" blog, please be sure to keep this source http://luminous.blog.51cto.com/10797288/1828026

Simple implementation of a 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.