Turn from: http://blog.csdn.net/analogous_love/article/details/52806722
First, the introduction of the daemon program is always running the service-side program, also known as the daemon process.
This paper introduces the steps of writing daemon program under Linux, and gives the example program.
Ii. Introduction to the Daemon Program daemon is a long-running process that usually runs after the system is started and ends when the system shuts down. Generally speaking, the daemon program runs in the background because it has no control terminal and cannot interact with the foreground user. Daemon programs are generally used as service programs, waiting for the client program to communicate with it. We also call the running Daemon program The daemon.
Iii. rules for the writing of daemon procedures
There are some basic rules for writing daemon programs to avoid unnecessary hassles.
1, first is the program after the operation of the call fork, and let the parent process exit. The child process obtains a new process ID, but inherits the process group ID of the parent process.
2. Call Setsid to create a new session that makes itself a leader for new sessions and new process groups, and no Control terminal (TTY) for the process.
3, change the current working directory to the root directory, so as not to affect the Loadable file system. Or you can change to some specific directory.
4. Set file creation mask to 0 to avoid the impact of permissions when creating files.
5. Close the unwanted open file descriptor. Because the daemon program executes in the background and does not need to interact with the terminal, it usually turns off stdin, stdout, and stderr. Other according to the actual situation processing.
Another problem is that the daemon program cannot interact with the terminal, and it cannot use the printf method to output the information. We can use the syslog mechanism to realize the output of information to facilitate the debugging of the program. You need to start the SYSLOGD program before using Syslog, please refer to its man page, or related documentation for the use of the SYSLOGD program, which we are not discussing here.
四、一个 Daemon Program Example compile run environment for Redhat Linux 8.0.
We create a new DAEMONTEST.C program, the contents of the document are as follows:
int daemon_init (void)
{
pid_t pid;
if ((PID = fork ()) < 0) return
( -1);
else if (PID!= 0)
exit (0);/* Parent exit/
* Child continues
/* SETSID ();/* Become session leader *
ChDir ("/"); /* Change Working directory
/umask (0)/* Clear file Mode creation mask/close
(0);/* Close stdin/
CLO SE (1); /* Close stdout * * Close
(2);/* Close stderr/return
(0);
}
void sig_term (int signo)
{
if (Signo = sigterm)/
* catched signal sent by Kill (1) command */
{
s Yslog (Log_info, "program terminated.");
Closelog (); Exit (0);
}
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24-25
I did it in the actual project:
if (isdaemon) {int pid;
Signal (SIGCHLD, sig_ign);
1 in the parent process, fork returns the process ID of the newly created subprocess;//2 in the child process, fork returns 0;//3) If an error occurs, fork returns a negative value; pid = fork ();
if (PID < 0) {perror ("fork");
Exit (-1);
}//parent process exits, the child process runs else if (pid > 0) {exit (0) independently; Before//before parent and child run in the same session, parent is the leading process of conversation,//parent process as the lead process of the session, and if Exit is finished, the subprocess becomes an orphan process and is
Init adoption.
After the Setsid () is executed, the child will regain a new session ID.
When parent exits, it will not affect the child.
Setsid ();
int FD;
FD = open ("/dev/null", o_rdwr,0);
if (FD!=-1) {dup2 (Fd,stdin_fileno);
Dup2 (Fd,stdout_fileno);
Dup2 (Fd,stderr_fileno);
if (FD > 2) Close (FD); }