The "daemon" of Linux programming

Source: Internet
Author: User
Tags assert

Intro

-----

The daemon, which is usually called the daemon process, is the background service process in Linux. It is a long-lived process, usually independent of the control terminal and periodically performs some sort of task or waits to handle certain occurrences. Daemons often start when the system boots, and terminate when the system shuts down. Linux systems have many daemons, most of which are implemented through daemons, while the daemon can accomplish many system tasks, such as the job planning process Crond, the printing process lqd, and so on (the end letter D is the daemon meaning).

Because in Linux, each system communicates with the user interface called the terminal, every process that starts from this terminal will be attached to this terminal, this terminal is called the control terminal of these processes, when the control terminal is closed, the corresponding process will automatically shut down. But the daemon is able to break through this limitation, and it will start running from execution until the entire system shuts down. If you want a process to be unaffected by user or terminal or other changes, you must turn the process into a daemon, and the complete code is pasted below.

1 /************************************************2 * This routine explains how the Linux daemon is programmed3 ************************************************/4#include <unistd.h>5#include <signal.h>6#include <sys/param.h>//Nofile7#include <sys/stat.h>//umask8#include <stdio.h>9#include <stdlib.h>Ten#include <time.h> One#include <assert.h> A  - BOOLInitdaemon () - { the     //shielding some signals about control terminal Operation -     //prevents the daemon from functioning properly, exits or hangs due to interference from the control terminal . -ASSERT (Signal (SIGINT, sig_ign)! = Sig_err);//Terminal Interrupt -ASSERT (Signal (SIGHUP, sig_ign)! = Sig_err);//connection Hang-off +ASSERT (Signal (sigquit, sig_ign)! = Sig_err);//Terminal Exit -ASSERT (Signal (sigpipe, sig_ign)! = Sig_err);//write data to a pipeline without a read process +ASSERT (Signal (Sigttou, sig_ign)! = Sig_err);//Background Program attempted write operation AASSERT (Signal (sigttin, sig_ign)! = Sig_err);//Background Program attempted read Operation atASSERT (Signal (SIGTERM, sig_ign)! = Sig_err);//termination -  -     //[1] Creating a child process, the parent process exits -     intPID =fork (); -     if(PID) -     { in         //Parent Process Exits -Exit0); to     } +     Else if(PID <0) -     { the         return false; *     } $ Panax Notoginseng     //child process continues to run -      the     //[2] Creating a new session in a child process, SETSID has three functions +     //A. Getting the process out of control of the original session A     //B. Getting the process out of control of the original process group the     //c. Get the process out of control of the original control terminal +     intRET =Setsid (); -     if(Ret <0) $     { $         return false; -     } -  the     //[3] Disable process re-opening control terminal -     //the process has become a session leader without terminal, but it can re-apply to open a control terminalWuyi     //you can prevent a process from reopening the control terminal by making the process no longer a session leader thePID =fork (); -     if(PID) Wu     { -         //end the first child process AboutExit0); $     } -     Else if(PID <0) -     { -         return false; A     } +  the     //The second child process continues to run -  $     //[4] closing open file descriptors the     //The process inherits the open file descriptor from the parent process that created it, and if it does not, it will waste system resources . the     //causes the file system where the process is located to fail to unload and cause unexpected errors the      for(inti =0; i < Nofile; ++i) the     { - Close (i); in     } the  the     //[5] Changing the current working directory About     //process activity, the file system in which the working directory resides cannot be removed, typically changing the working directory to the root directory theret = ChDir ("/"); the     if(Ret <0) the     { +         return false; -     } the Bayi     //[6] Reset file creation Mask the     //The process inherits the file creation mask from the parent process that created it, and it may modify the access bit of the file created by the daemon the     //so the file creation mask is cleared -Umask0); -  the     return true; the } the  the intMain () - { the     //Initialize Daemon the     BOOLRET =Initdaemon (); the     if(!ret)94     { theprintf"Init Daemon failed\n"); the         return 1; the     }98  Aboutfile* file =NULL; -time_t T =0;101 102     //report running status to Test.log every 10 seconds103      while(true)104     { theSleepTen);106File = fopen ("./var/test.log","A +");107         if(File! =NULL)108         {109t =Time (NULL); thefprintf (file,"I am here at%s\n", Asctime (LocalTime (&T )));111 fclose (file); the         }113     } the  the     return 0; the}

The example of GitHub address: Https://github.com/chxuan/samples/blob/master/Daemon/Daemon.cpp

The "daemon" of Linux programming

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.