Detailed explanation of the daemon in Linux system

Source: Internet
Author: User
Tags log log printf root directory

Daemon Daemon, is a long life of a process. They are often started when the system is bootstrap, and are terminated only when the system shuts down. Because they do not have control terminals, they are run in the background. UNIX systems have many daemons that perform day-to-day business activities.

1. System Bootstrap

The word bootstrapping comes from the idea that people stand up on their own "bootstrap" institutions. The computer must have the ability to activate all of its components so that the operating system can be loaded, and then the operating system takes on more complex tasks that cannot be accomplished by bootstrap code alone.

Self-lift has only two functions: power On Self test and disk boot.

Self-Test: When we press the computer power switch, the first few seconds the machine does not seem to have any reaction, in fact, the computer is in the power of the self-test, to determine that all its components are working correctly. If a component is faulty, alert messages appear on the monitor (if the monitor is not working properly, the alarm is made by a string of beeps). Since most computer work is very reliable, it is very rare to have an electric self-test.

Disk boot: Find the disk drive with the operating system installed. The operating system is loaded from disk for two reasons, one is easy to upgrade the operating system, the other is the user has the freedom to choose the operating system.

When the power self-test and disk boot are complete, the bootstrap operation initiates a read-write operating system file and copies them to the random memory, at which point the machine is the computer in the real sense. Computer startup can have cold start and hot start two ways, the difference between them is that the thermal boot does not carry out the machine self-test (machine itself configuration inspection and testing), when the computer in use for some reason caused by the panic, the computer can be hot start processing.


2, the concept of the daemon process

The daemon can be viewed through the PS AXJ command:
Parameter A indicates that not only the current user's process is listed, but also all other user's processes, and parameter x indicates that not only the process that contains the control terminal, but also all processes that have no control terminals, parameter J indicates that information related to job control is listed.

The

code is as follows: PPID PID pgid SID TTY tpgid STAT UID time COMMAND


0 1 1 1? -1 Ss 0 0:01/sbin/init


0 2 0 0? -1 s< 0 0:00 [Kthreadd]


2 3 0 0? -1 s< 0 0:00 [migration/0]


2 4 0 0? -1 s< 0 0:00 [ksoftirqd/0] ...


1 2373 2373 2373? -1 s<s 0 0:00/sbin/udevd--daemon ...


1 4680 4680 4680? -1 Ss 0 0:00/usr/sbin/acpid-c/etc ...


1 4808 4808 4808? -1 Ss 102 0:00/sbin/syslogd-u syslog ...



All tpgid a column written-1 are not control the terminal process, that is, the daemon process. In the command column, the [] enclosed name represents the kernel thread, which is created in the kernel, has no user space code, and therefore does not have a program file name and command line, usually with a name beginning with K, representing kernel. Init process We are already familiar with, UDEVD is responsible for maintaining the device files in/dev directory, Acpid is responsible for power management, SYSLOGD responsible for maintaining/var/log log files, you can see that the daemons usually use the name of the end of D, said Daemon.
The most critical step in creating a daemon is to call the SETSID function to create a new session and become a session Leader.

Example:

/C + + code copy content to clipboard
    1. void Daemonize (void)
    2. {
    3. pid_t pid;
    4. printf ("Into Deamonizen");
    5. if (PID = fork () < 0)
    6. {
    7. Perror ("fork");
    8. Exit (1);
    9. }
    10. else if (PID!= 0)
    11. {
    12. Exit (0);
    13. }
    14. Setsid ();
    15. if (ChDir ("/") < 0)
    16. {
    17. Perror ("ChDir");
    18. Exit (1);
    19. }
    20. Close (0);
    21. Open ("/dev/null", O_RDWR);
    22. Dup2 (0, 1);
    23. Dup2 (0, 2);
    24. printf ("Out Deamonizen");
    25. }

3, write the daemon process

There are some basic rules to follow when writing daemon programs:

(1) The first thing to do is call Umask to set the file mode creation screen Word to 0.
(2) Call fork and then exit the parent process.
(3) Call Setsid to create a new session.
(4) Change the current working directory to the root directory.
(5) Close the file descriptor that is no longer needed.
(6) Some daemons open/dev/null so that they have file descriptors 0, 1, and 2, and any library routines that attempt to read standard input, write standard output, or standard errors have no effect.

One of the issues associated with the daemon is how to handle error messages, and the need for a centralized daemon error logging facility, which is the SYSLOGD process.

4, the Daemon procedure Convention

In order to function properly, some daemons are implemented as single-instance, with only one copy of the daemon running at any one time. File lock and record lock mechanisms are the basis for a method that ensures that only one copy of a daemon is running.
In Unix systems, the daemon follows the following public conventions:

(1) If the daemon uses a lock file, the file is usually stored in the/var/run directory. The name of the lock file is usually the Name.pid,name is the name of the daemon or service.
(2) If the daemon supports configuration options, then the configuration file is usually stored in the/etc directory. The name of the configuration file is usually name.conf.
(3) Daemons can be started by the command line, but usually they are initiated by the system initialization script.
(4) If a daemon has a configuration file, it reads the daemon when it is started, but generally does not view it again after that.

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.