Steps for compiling a Daemon program in Linux

Source: Internet
Author: User
For details about how to compile the Daemon program in Linux, refer to general Linux technology-Linux programming and kernel information. 1. Introduction The Daemon program is a continuously running server program, also known as a Daemon process.

This article describes how to write a Daemon program in Linux and provides an example program.

Ii. Daemon program introduction Daemon is a long-running process. It usually runs after the system is started and ends only when the system is shut down. Generally, 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 a Daemon process.

3. Daemon programming rules

There are some basic rules for writing Daemon programs to avoid unnecessary troubles.

1. Run the program and call fork to exit the parent process. 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, make yourself the leader of the new session and the new process group, and make the process have no control terminal (tty ).

3. Change the current working directory to the root directory to avoid affecting the file system that can be loaded. Or you can change it to a specific directory.

4. Set the file creation mask to 0 to avoid the impact of permissions on the file creation.

5. disable unnecessary open file descriptors. Because the Daemon program is executed in the background and does not require terminal interaction, STDIN, STDOUT, and STDERR are usually disabled. Handle other issues based on actual conditions.

Another problem is that the Daemon program cannot interact with the terminal, and the printf method cannot be used to output information. We can use the syslog mechanism to output information to facilitate program debugging. Start the syslogd program before using syslog. For details about how to use the syslogd program, refer to its man page or related documents. We will not discuss it here.

4. A Daemon program is compiled and run in the Redhat Linux 8.0 environment.

Create a daemontest. c program with the following content:

# Include
# Include
# Include
# Include
# Include
# Include
# Include

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 */
Close (1);/* close stdout */
Close (2);/* close stderr */
Return (0 );}
Void sig_term (int signo)
{If (signo = SIGTERM)
/* Catched signal sent by kill (1) command */
{Syslog (LOG_INFO, "program terminated .");
Closelog (); exit (0 );}
}
Int main (void)
{If (daemon_init () =-1)
{Printf ("can't fork self \ n"); exit (0 );}
Openlog ("daemontest", LOG_PID, LOG_USER );
Syslog (LOG_INFO, "program started .");
Signal (SIGTERM, sig_term);/* arrange to catch the signal */
While (1) {sleep (1);/* put your main program here */}
Return (0 );}

Run the following command to compile the program: After gcc-Wall-o daemontest. c is compiled, a program named daemontest is generated and run./daemontest to test the program.

The ps axj command can be used to display information about the daemon program running in the system, including the process ID, session ID, and control terminal.

Partial display content:

PPID PID PGID SID TTY TPGID STAT UID TIME COMMAND

1098 1101 1101 1074 pts/1 1101 S 0-bash 1 1581 777 777? -1 S 500 gedit 1 1650 1650 1650? -1 S 500./daemontest 794 1654 1654 794 pts/0 1654 R 500

Ps axj shows that the daemontest running process is 1650.

Let's take a look at the information in the/var/log/messages file: Apr 7 22:00:32 localhost

Daemontest [1650]: program started.

Displays the information we want to output in the program.

Run the kill 1650 command to kill the process. The/var/log/messages file contains the following information:

Apr 7 22:11:10 localhost daemontest [1650]: program terminated.

Run the ps axj command to check the daemontest process.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.