This document describes the concepts and usage of the Linux daemon.
Daemon is a background service process. It is usually independent of the control terminal and periodically executes tasks or waits for events. The daemon is often started during system boot and ended when the system is shut down. In Linux, every process running from the terminal is attached to this terminal. When the terminal is closed, the process is automatically closed. However, the daemon can break through this restriction. To prevent a process from being affected by changes in the user or terminal, you should turn the process into a daemon.
To write a daemon, follow these steps:
1. Create a child process and exit the parent process.
After exiting the parent process, the child process becomes an orphan process and is adopted by process 1 (INIT process.
The key code is as follows:
PID = fork ();
If (pid> 0)
{
Exit (0 );
}
2. Create a session in the sub-process
First, we will introduce two concepts: Process Group and session group.
A process group is a collection of one or more processes, uniquely identified by the Process Group ID. Each process group has a leader process. The process ID of the leader is the process group ID, and the process group ID is not affected by the leader's exit.
A session Group is a collection of one or more Process Groups. Generally, a session begins with a user logon and ends with a user logout.
Next, we will focus on the setsid function, which is the most important part of daemon creation. The setsid function is used to create a new session and act as the group leader. The setsid function has three functions.
(1) Remove the process from the control of the original session Group
(2) Remove the process from the control of the original Process Group
(3) freeing the process from the control of the original control terminal
Why is the setsid function executed? The reason is that when we used the fork function to create a sub-process, we copied the session group, process group, control terminal, and other information of the parent process. Although the parent process exited, however, the sub-process retains the information. Therefore, we need to execute the setsid function to change the information and "jailbreak ".
Prototype: pid_t setsid (void)
Return: The Process Group ID -- Success
-1 -- Failure
3. Change the current directory to the root directory.
Generally, the file system in the current directory environment where the child process inherits the parent process cannot be detached, which may be troublesome for future use (for example, the system needs to enter the single-user mode ). The common practice is to use the root directory "/" as the current working directory of the daemon.
4. Reset the File Permission mask.
The Subprocess inherits the permission mask of the parent process, causing inconvenience. Generally, the mask is reset to 0, which is more flexible.
5. Disable the file descriptor
Child processes inherit some opened files from the parent process, which consume system resources. As the sub-process is out of control from the terminal, files with file descriptors 0, 1, and 2 (input, output, and Error Reporting) also lose value and should be closed.
The example taken from "Detailed description of Embedded Linux application development" is as follows:
# Include <stdio. h> <br/> # include <stdlib. h> <br/> # include <string. h> <br/> # include <fcntl. h> <br/> # include <sys/types. h> <br/> # include <unistd. h> <br/> # include <sys/Wait. h> </P> <p> # define maxfile 65535 </P> <p> int main (void) <br/>{< br/> pid_t PC; <br/> int I, FD, Len; <br/> char * Buf = "This Is A dameon/N"; <br/> Len = strlen (BUF ); </P> <p> Pc = fork (); // Step 1 <br/> If (Pc <0) <br/>{< br/> printf ("error fork/N"); <br/> exit (1 ); <br/>}< br/> else if (Pc> 0) <br/>{< br/> exit (0 ); <br/>}</P> <p> setsid (); // The sub-process is running. Step 2 <br/> chdir ("/"); // step 3 <br/> umask (0); // Step 4 <br/> for (I = 0; I <maxfile; I ++) // daemon creation completed <br/>{< br/> close (I); <br/>}</P> <p> (;;) <br/> {// The daemon process is started at every 10 seconds. write a statement to log <br/> If (FD = open ("/tmp/dameon. log ", o_creat | o_wronly | o_append, 0600) <0) <br/>{< br/> perror (" open "); <br/> exit (1 ); <br/>}</P> <p> write (FD, Buf, Len + 1); <br/> close (FD ); <br/> sleep (10); <br/>}< br/>}
After compilation and running, run tail-F/tmp/dameon. log to query the effect of periodic updates to the file.
Run PS-Ef | grep daemon to query the daemon status.
This section is complete.