Linux Daemon Code full comment

Source: Internet
Author: User

#include  <unistd.h> #include  <signal.h> #include  <sys/param.h> #include   <sys/types.h> #include  <sys/stat.h>void init_daemon (void) {             int pid;             int i;        /*  when:step 1;      *  what: Put daemon into the background run;     *   What: To avoid suspending the control terminal;      *  how: Call fork in the process to terminate the parent process and let Daemon run in the background in the child process.      */    if (Pid=fork ())          exit (0);//is parent process, end parent process     else if (pid< 0)          exit (1);//fork failure, Exit                    &Nbsp; /* when:step 2.      * what: Out of control terminal, login session and process group      *   Why: Setsid () fails when the process is called in, Step1 has ensured that the process is not a session leader      *  how: Call Setsid () in the child process       */    setsid ();          //PS:  after the successful process becomes the new session leader and the new process group leader, and the original login session and process group detachment, due to the session to control the terminal's exclusivity, the process at the same time and control Terminal detachment.          /* when:step3;     *  What: Prohibit the process from reopening the control terminal; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;*&NBSP;WHY&NBSP: The session leader without terminal can re-apply to open a control terminal;    &NBSP;&NBSP;*&NBSP;HOW&NBSP: Make the process no longer a conversation leader     */         if (Pid=fork ())         exit (0);//is the first child process, ending the first subprocess      else if (pid< 0)         exit (1)//fork failure, exit          //ps: The first child process is the new session group leader exit, the second child process continues, but the second child process is not a new session group leader         /*  when:step4;     * what: Close open file descriptor;      * why &NBSP;: The process inherits the open file descriptor from the creation of his parent process, which, if not closed, wastes system resources, and another convenience can lead to the                cannot delete unexpected errors such as file system cannot be uninstalled;      * how : Call Close () closes the open file;      */    for (i=0; i< nofile; i++)          close (i);             /* when:step5;     * what: Change the current working directory;      *  why: The file system on which the working directory resides cannot unload      *  how when the process activity is active: calling ChDir () is generally required Change the working directory to the root directory     */    chdir ("/");           &nBsp; /* when:step6;     * what: Resetting the document creation mask      *  why : Process Punch Create he has to inherit the file creation mask from the parent process, and he may modify the Read permissions of the files created by the daemon      * how  : Call Umask (0) to clear the file creation mask     */    umask (0);         /* when:step7;     * what: Processing the child process end signal sigchild   &NBSP;&NBSP;&NBSP;*&NBSP;WHY&NBSP: Prevents a child process from becoming a zombie process cannot be killed, occupying system resources      * how  : Ignore this signal under Linux to     */    signal (sigchld, sig_ign);         return;    }example.c:int main (void) {     FILE *fp = NULL;    time_t t;         //initialized to Daemon    int_daemon ();         while  (1) {&NBSP;&NBSp;      sleep (;        if ) (FP  = fopen ("Test.log",  "a"))  > 0) {             t = time (0);             fprintf (fp,  "I ' m here at %s\r\n",  asctime (LocalTime (&t)));             fclose (FP);         }    }}









Linux Daemon Code full comment

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.