I. Overview: Daemons, also known as daemon Processes (Daemon), are a special process that runs in the background. It is independent of the control terminal and periodically performs some sort of task or waits to handle certain occurrences. The daemon is a process that is present with the system boot and disappears as the system shuts down (that is, it will exist after power-on and the shutdown disappears). Daemons are a useful process, and most Linux servers are implemented with daemons.
Run command at Terminal: PS AXJ | Head
Parameter A indicates that not only the current user process is listed, but all other user processes are also listed.
The parameter x indicates that not only the process with the control terminal is listed, but also all processes that have no control terminal.
The parameter J indicates that information related to job control is listed. 650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/7F/F3/wKiom1cyn6Tzx0NkAABEzot1yDE417.png "title=" 2016-05-11 10-56-50 screen. png "alt=" Wkiom1cyn6tzx0nkaabezot1yde417.png "/>
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, and they are running until the computer shuts down.
All Tpgid A column is written-1 is the process of no control terminal, that is, the daemon process. In command a column with [] in the name of the kernel thread, these threads are created in the kernel, there is no user space code, so there is no program file name and command line, usually with a name beginning with K, representing kernel. The init process is the first user-level process initiated by the kernel, and Init has many important tasks, such as starting Getty (for user login), implementing RunLevel, and handling orphan processes. UDEVD is responsible for maintaining the device files in the/dev directory, Acpid is responsible for power management, SYSLOGD is responsible for maintaining the log files under/var/log, it can be seen that the daemon usually uses the name ending with D, indicating daemon.
Two. Create a daemon:
The most critical step in creating a daemon is to call the SETSID function to create a new session and become the session Leader.
int Setsid (void)
When the function call succeeds, the ID of the newly created session is returned (that is, the ID of the current process), and the error returns-1.
The result of a successful call with this function is:
1. Create a new session, the current process becomes session Leader, the ID of the current process is the session ID.
2. Create a new process group, the current process becomes the leader of the process group, and the ID of the current process is the ID of the process group.
3. If the current process originally has a control terminal, it loses the control terminal and becomes a process without control terminal. The so-called loss of control terminal is that the original control terminal is still open, can still read and write, but only a normal open file instead of the control terminal.
Three. Create a daemon process:
1. Call Umask to set the file mode creation shield Word to 0.
2. Call fork, the parent process exits (exit). Reason: (1). If the daemon was started as a simple shell command, the parent process terminates so that the shell considers that the command has been executed. (2). Ensure that the child process is not a process group leader process.
3. Call Setsid to create a new session. Setsid causes: (1) The calling process becomes the first process of a new session. (2). Call with
Process becomes the leader of a process group. (3). The calling process does not control the terminal. (You can also fork again to ensure that the daemon process does not open the TTY device later)
4. Change the current working directory to the root directory.
5. Close the file descriptor that is not required.
6. Other: Ignore the SIGCHLD signal.
Four. Create a daemon-related code: (The contents of the daemon can be defined by itself)
1 /**************************************** 2 > file name:create_daemon.c 3 > author:xiaoxiaohui 4 > mail:[email protected] 5 > created time:2016 May 10 Tuesday 20 31 minutes, 11 seconds 6 **************************** / 7 8 #include <stdio.h> 9 #include < stdlib.h> 10 #include <string.h> 11 #include <signal.h> 12 #include <unostd.h> 13 #include <fcntl.h> 14 #include <sys/stat.h> 15 #include <sys/types.h> 16 17 int main () 18 { 19 pid_t pid; 20 FILE* fd; 21 char* buf = "I ' m a daemon\n "; 22 23 umask (0); The first step is to set the file mask to 0 24 25 pid = fork (); // The second step is to create the subprocess 26 if (pid < 0) 27 { 28 printf ("fork errored \n"); 29 exit (0); 30 } 31 else if (pid > 0) 32 { 33 exit (0); 34 } 35 else //Child Process 36 { 37 setsid (); //the third step, create a new session 38 39 chdir ("/"); //The fourth step, change the current working directory 40 41 close (1); //The fifth step, close the unnecessary file descriptor 42 close (2); 43 close (3); 44 45 signal (sigchld,sig_ign);//Ignore SIGCHLD signal 46 47 while (1) 48 { 49 } 50 51 return 0; 52 }
This article is from "Narcissus" blog, please make sure to keep this source http://10704527.blog.51cto.com/10694527/1772130
Create a daemon