This article describes a routine daemon_init ()
#include <sys/types.h> #include <signal.h> #include <unistd.h> #include <syslog.h> #define MAXFD 64void daemon_init (const char *pname, int facility) {int i:pid_t pid;/* Fork, terminates parent process */if (PID = fork ()) exit (0);/* First Wahabbi Cheng */setsid (); signal (sighup,sig_ign);/* fork, terminating first subprocess */if (pid = fork ()) exit (0);/* second child process */daemon_proc = 1;/* set working directory to "/" * /chdir ("/");/* Clear File Mask */umask (0);/* Close all file handles */for (i = 0;i < maxfd;i++) {close (i);} /* Open LOG */openlog (pname,log_pid,facility);}
The main process established by the daemon process
1. Fork
By fork out a child process and close the parent process, the process becomes a background process, and the child process inherits the group identifier from the parent process and has its own process identifier, which guarantees that the child process will not be the first process of a process group
2, Setsid
Setsid () Creates a new process group that invokes the process as the first process of the process group, leaving the process out of the original terminal and becoming independent of the terminal process.
3, ignore Sighup signal, re-fork
This makes the process no longer the first process of the process group, and can be placed in some cases to open the terminal and re-contact the terminal
4, change the working directory, clear the file mask
The directory is adapted to cut off the connection between the process and the original file system, and ensures that the file mask is purged from the process itself, regardless of where the boot process is working, to eliminate the impact of the processes themselves on the file creation.
5. Close all files that have been opened jubing
Prevents child processes from inheriting files that are open in the parent process so that the files remain open for some conflicts
6. Open Log System
The individual's understanding of this example is that the process is first a parent process that has a process group identifier and a process identifier, and then creates a child process (the first child process) by fork, terminating the parent process, but, although the child process has its own process identifier, it inherits the process group (process Group One) identifier of the parent process , so that the process group is the original, then this first child process is not the first process of the process group. Then, through Setsid created a new process group (process Group two), because it is newly created, so it must be the first process, then the first process is out of the original process environment, independent of the terminal, and then, in this new process group (Process Group II) and re-fork a child process ( Second child process), for the second child process, the first child process is its parent process, so the parent process terminates the first child process is terminated, at this time, the second child process is independent of the original terminal, and the second child process is not the process group (Process Group II) the first process, and finally change the working directory, clear Mask, Close the file handle, open the log system, then this second sub-process is the creation of the daemon, well, I understand so, do not know understand right? Ask the great God to point out!
Linux Network Programming Learning (iv) Establishment of the-----Daemon (chapter III)