Linux programming-daemon and linux daemon
Intro
-----
Daemon processes are the background service processes in Linux. It is a long-lived process, usually independent of the control terminal and periodically executes a task or waits to process some events. Daemon is often started during system boot and ended when the system is shut down. In Linux, there are many daemon processes. Most services are implemented through daemon. At the same time, daemon can complete many system tasks, for example, job planning process crond and printing process lqd (the ending letter d indicates Daemon ).
In Linux, every system interface that communicates with users is called a terminal. Every process starting from this terminal is attached to this terminal, which is called the control terminal of these processes, when the control terminal is closed, the corresponding process is automatically closed. However, the daemon can break through this restriction. It does not exit until it is executed. To prevent a process from being affected by user, terminal, or other changes, you must turn the process into a daemon. The complete code is pasted below.
1 /************************************** * ********* 2 this routine explains the programming method of the Linux daemon 3 ****************** * ****************************/4 # include <unistd. h> 5 # include <signal. h> 6 # include <sys/param. h> // NOFILE 7 # include <sys/stat. h> // umask 8 # include <stdio. h> 9 # include <stdlib. h> 10 # include <time. h> 11 # include <assert. h> 12 13 bool initDaemon () 14 {15 // shield some signal related to the control terminal operation 16 // prevent the daemon process from running properly The control terminal is disturbed to exit or suspend 17 assert (signal (SIGINT, SIG_IGN )! = SIG_ERR); // terminal interrupt 18 assert (signal (SIGHUP, SIG_IGN )! = SIG_ERR); // The Connection hangs up 19 assert (SIGQUIT, SIG_IGN )! = SIG_ERR); // terminal exits 20 assert (signal (SIGPIPE, SIG_IGN )! = SIG_ERR); // write data 21 assert (SIGTTOU, SIG_IGN) to the pipeline without a read process )! = SIG_ERR); // The background program attempts to write 22 assert (SIGTTIN, SIG_IGN )! = SIG_ERR); // The background program attempts to read 23 assert (signal (SIGTERM, SIG_IGN )! = SIG_ERR); // terminate 24 25 // [1] create a sub-process, the parent process exits 26 int pid = fork (); 27 if (pid) 28 {29 // The parent process exits 30 exit (0); 31} 32 else if (pid <0) 33 {34 return false; 35} 36 37 // The sub-process continues to run 38 39 // [2] Create a new session in the sub-process. setsid has three roles: 40 //. let the process get rid of the control of the original session 41 // B. let the process get rid of the control of the original process group 42 // c. let the process get rid of control 43 int ret = setsid (); 44 if (ret <0) 45 {46 return false; 47} 48 49 // [3] prohibit the process from re-opening the control terminal 50 // The process has become the no-end session leader, but it can re-apply to open Control terminal 51 // you can disable the process from re-opening the control terminal by making the process no longer a session leader 52 pid = fork (); 53 if (pid) 54 {55 // end the first sub-process 56 exit (0); 57} 58 else if (pid <0) 59 {60 return false; 61} 62 63 // The second child process continues to run 64 65 // [4] Close the opened file descriptor 66 // The process inherits the opened file descriptor from the parent process that created it, if you do not close the file system, system resources will be wasted. 67 // cause the file system where the process is located to be unable to be detached and unexpected errors 68 for (int I = 0; I <NOFILE; ++ I) 69 {70 close (I); 71} 72 73 // [5] changing the current working directory 74 // process activity, the file system of the working directory cannot be detached. Generally, the working directory is changed to the root directory. 75 ret = chdir ("/"); 76 if (ret <0) 77 {78 return false; 79} 80 81 // [6] resetting the file creation mask 82 // The process inherits the file creation mask from the parent process that created the file, it may modify the access bits of the files created by the daemon. Therefore, clear 84 umask (0); 85 86 return true; 87} 88 89 int main () 90 {91 // initialize the daemon 92 bool ret = initDaemon (); 93 if (! Ret) 94 {95 printf ("Init daemon failed \ n"); 96 return 1; 97} 98 99 FILE * file = NULL; 100 time_t t = 0; 101 102 // test every 10 seconds. log report running status 103 while (true) 104 {105 sleep (10); 106 file = fopen (". /var/test. log "," a + "); 107 if (file! = NULL) 108 {109 t = time (NULL); 110 fprintf (file, "I am here at % s \ n", asctime (localtime (& t ))); 111 fclose (file); 112} 113} 114 115 return 0; 116}
Github address for this example: https://github.com/chxuan/samples/blob/master/Daemon/Daemon.cpp