Introduction to the Linux daemon and instance details

Source: Internet
Author: User

Introduction to the Linux daemon and instance details

Introduction to the Linux daemon and instance details

Introduction

Daemon is a special process running in the background. it is independent of the control terminal and periodically executes a task or waits for processing certain events. daemon is a very useful process. most Linux servers are implemented using daemon. for example, the Internet server inetd and the Web Server httpd. at the same time, the daemon completes many system tasks. for example, job planning process crond and printing process lpd.

The following are some common Daemon Processes in linux.

Amd: automatically installs NFS (Network File System) Guard Processes

Key Points of daemon Programming

The programming of daemon is not complex. The complicated problem is that different versions of Unix have different implementation mechanisms, resulting in inconsistent programming rules of daemon in different Unix environments. the following describes the programming points of the daemon in Linux and provides detailed examples.

The programming rules of daemon in different Unix environments are inconsistent. fortunately, the programming principles of the daemon process are the same. The difference is that the specific implementation details are different. this principle is to satisfy the characteristics of the daemon. the main points of programming are as follows:

Run in the background. to avoid pending the control terminal, place the Daemon in the background for execution. the method is to call fork in the process to terminate the parent process and run Daemon in the background of the child process. log on to the session and process group from the control terminal. it is necessary to first introduce the relationship between processes and control terminals, logon sessions, and process groups in Linux: Processes belong to a process group, process group number (GID) is the process ID (PID) of the process leader ). A logon session can contain multiple Process Groups. these process groups share a control terminal. this control terminal is usually the login terminal for creating processes. control terminal. logon sessions and process groups are generally inherited from the parent process. our goal is to get rid of them so that they are not affected. the method is to call setsid () on the basis of to make the process become the session leader. (Description: setsid () fails to be called when the process is a session leader. however, the first point is to ensure that the process is not the session leader .) after the setsid () call is successful, the process becomes the new session leader and the new process leader, and disconnects from the original logon session and process group. because the session process is dedicated to the control terminal, the process is separated from the control terminal at the same time. prohibit the process from re-opening the control terminal. now, the process has become the no-end session leader. but it can re-apply to open a control terminal. you can disable the process from re-opening the control terminal by making the process no longer the session leader. disable the opened file descriptor. the process inherits the opened file descriptor from the parent process that created it. if you do not close it, system resources will be wasted, and the file system where the process is located will not be able to be detached and unexpected errors will occur. change the current working directory. when a process is active, the file system in which the working directory is located cannot be detached. change the working directory to the root directory. for processes that need to dump the core, write the running log to change the working directory to a specific directory, such as/tmpchdir ("/"). reset the file to create a mask. A process inherits the file creation mask from its parent process. it may modify the access location of the file created by the daemon. to prevent this, clear the file creation mask: umask (0 ). process SIGCHLD signal. it is not necessary to process SIGCHLD signals. however, some processes, especially server processes, usually generate sub-processes to process requests when requests arrive. if the parent process does not wait for the child process to end, the child process will become a zombie process (zombie) and thus occupy system resources. if the parent process waits for the child process to end, it will increase the burden on the parent process and affect the concurrent performance of the server process. in Linux, you can set the SIGCHLD signal operation to SIG_IGN. in this way, the kernel will not generate zombie processes when the child process ends. daemon instance

The daemon instance consists of the main program test. c and the initialization program init. c. the main program sends the log test to the/tmp directory every minute. log reports the running status. the init_daemon function in the initialization program is responsible for generating the daemon process. you can use the init_daemon function to generate your own daemon process.

# Include <unistd. h> # include <signal. h> # include <sys/param. h> # include <sys/types. h> # include <sys/stat. h> void init_daemon (void) {int pid; int I; if (pid = fork () exit (0); // It is the parent process, stop the parent process else if (pid <0) exit (1); // fork failed, exit // is the first sub-process, and the background continues to execute setsid (); // The first sub-process becomes the new session leader and process leader, and is separated from the control terminal if (pid = fork () exit (0); // is the first sub-process, stop the first sub-process else if (pid <0) exit (1); // fork failed, exit // is the second sub-process, continue, the second sub-process is no longer the session leader for (I = 0; I <NOFILE; ++ I) // close the opened file descriptor close (I ); chdir ("/tmp"); // change the working directory to/tmp umask (0); // reset the file creation mask return ;}

2. test. c LIST # include <stdio. h> # include <time. h> extern void init_daemon (void); // Daemon initialization function main () {FILE * fp; time_t t; init_daemon (); // initialize to Daemon while (1) // test every minute. log reports the running status {sleep (60); // if (fp = fopen ("test. log "," a ")> = 0) {t = time (0); fprintf (fp," I'm here at % s \ n ", asctime (localtime (& t); fclose (fp );}}}

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.