Programming Method of daemon in Linux

Source: Internet
Author: User
The Programming Method of the daemon in the Linux operating system-general Linux technology-Linux programming and kernel information. For details, refer to the following section. Daemon is a special process running in the background. It is independent of the control terminal and periodically executes a task or waits to process some 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 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. This requires you to note that copying the rules in some books (especially BSD4.3 and earlier versions of System V) will cause errors to Linux. The following describes the programming points of the daemon in Linux and provides detailed examples.

1. daemon process and Its Features

The most important feature of a daemon is that it runs in the background. At this point, the TSR of the resident memory program under DOS is similar. Second, the daemon must be isolated from the environment before running. These environments include unclosed file descriptors, control terminals, sessions and process groups, working directories, and file creation masks. These environments are generally inherited by the daemon from the parent process (especially the shell) that executes the daemon. Finally, the daemon startup method has its own special features. It can be started from the startup script/etc/rc. d when the Linux system is started. It can be started by the job planning process crond and executed by the user terminal (usually shell.

In short, apart from these special features, the daemon process is basically no different from the common process. Therefore, writing a daemon actually transforms a common process into a daemon according to the features of the preceding daemon. If you have a deep understanding of the process, it is easier to understand and program the process.

II. Key Points of daemon Programming

As mentioned above, 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 process. At the same time, Linux is a SVR4 Based on Syetem V and complies with the Posix standard, which is easier to implement than bsd4. The main points of programming are as follows;

1. 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.

If (pid = fork ())
Exit (0); // indicates the parent process. The parent process ends and the child process continues.
Related Article

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.