Chapter 4 daemon
13.1 Introduction
* Daemon is also known as a daemon. They are often started during system auto-lifting and terminated only when the system is shut down. Because they do not have control terminals, they are running in the background. UNIX systems have many daemon processes that execute daily transaction activities.
13.2 daemon features
* System processes depend on the operating system. Processes with a parent process ID of 0 are usually kernel processes, which are started as part of the system self-lifting process. (Init is an exception to this process. It is the user-layer command started by the Kernel During the bootstrap process .) The kernel process is special and usually exists throughout the lifecycle of the system. They run with Super User privileges, without control terminals, and without command lines
13.3 programming rules
* (1) the first thing to do is to call umask to set the blocking word created in file mode 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) disable unnecessary file descriptors
(6) Some daemon open/dev/null to make it have file descriptors 0, 1, and 2. In this way, no library routine attempts to read standard input, write standard output, or standard errors will produce any effect.
13.4 error records
* There are three methods to generate log messages.
(1) The kernel routine can call the log function.
(2) Most user processes (Daemon) Call the syslog function to generate log messages.
(3) A user process on this host, or a user process connected to another host on this host through a TCP/IP network, can send log messages to UDP port 514
13.5 single-instance daemon
* File and record lock mechanisms are the basis of a method to ensure that only one copy of a daemon is running.
13.6 daemon practices
* If the daemon uses a lock file, the file is usually stored in the/var/run directory.
* If the daemon supports configuration options, the configuration file is usually stored in the/etc directory.
* Daemon can be started using command lines, but they are usually started by one of the system initialization scripts (/etc/rc * or/etc/init. d /*).
* Some daemon will capture the sighup signal. When they receive the signal, reconfigure the file.
13.7 customer process-Server Process Model
* Daemon is often used as a server process.
* Generally, a server is a process waiting for the customer process to connect with it. The customer process puts forward some type of service requirements to it.
* Two-way communication: the client process sends a request to the server process, and the server process sends a response to the client process.