Programming method for Linux daemon ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ------------------------------------ Xiaohu 2002-06-12 14:16:19 daemon (Daemon) is a special process running in the background. It is independent of the control terminal and periodically performs some task or waits for some events. The daemon is a very useful process. Most of Linux is implemented with daemon. For example, the Internet server inetd, web server httpd, etc. At the same time, the daemon completes many system tasks. For example, the job planning process crred, print process LPD, etc. The programming of the daemon is not complicated, complex is that the implementation mechanism of various versions of UNIX is not the same, causing programming rules in different UNIX environments and inconsistent. This requires readers to pay attention to the rules on certain books (especially BSD4.3 and low version of System v) to Linux will have errors. The program key points of the daemon under Linux will be fully introduced below and give a detailed instance. One. The most important feature of daemon and its characteristic daemon is the background running. At this point, the resident memory program TSR under DOS is similar. Second, the daemon must be separated from the environment before its run. These environments include unsturned file descriptors, control terminals, sessions, and process groups, working directories, and file creation masks. These environments are usually the daemon inherited from the execution of its parent process (especially shell). Finally, the launch method of the daemon has its own special. It can start from the startup script /etc/rc.d when the Linux system is started, can be started by the job planning process crones, and can also be performed by the user terminal (usually the shell). In summary, the daemon is basically no different from the normal process outside of these specialities. Therefore, the writing daemon is actually the transformation of a normal process in accordance with the characteristics of the above daemon as a daemon. If the reader has more in-depth understanding of the process, it is easier to understand and program. Two. The programming of the daemon has been speaking, and the programming rules for daemon in different UNIX environments are inconsistent. Fortunately, the programming principle of the daemon is actually the same, and the difference is that the specific implementation details are different. This principle is to meet the characteristics of the daemon. At the same time, Linux is SVR4 based on SYETEM V and follows the POSIX standard, which is more convenient than BSD4. The programming points are as follows; 1. Run in the background. To avoid suspend control terminals, put daemon in the background. The method is to call the Fork in the process to terminate the parent process, let Daemon execute in the sub-process. IF (PID = fork ()) exit (0); // is a parent process, ending the parent process, the child process continues 2. Dive from the control terminal, the login session, and the process group must first introduce the process and control terminals in Linux. The relationship between the login session and process group: The process belongs to a process group. The process group number (GID) is the process number (PID) of the process team. The login session can contain multiple processes groups. These process groups share a control terminal. This control terminal is usually a login terminal that creates a process. Control terminals, login sessions, and process groups are usually inherited from the parent process. Our goal is to get rid of them, so that it is not affected by them. The method is based on the 1st point, calling setsid () to make the process a session group length: setsid (); Description: When the process is a session group, the setsid () call failed. But the first point has assured that the process is not a session team leader. After the setsid () call is successful, the process becomes a new session group leader and a new process team leader and detached from the original login session and process group.
Due to the exclusiveity of the control terminal, the process is simultaneously detached from the control terminal. 3. Prohibit Process Re-opening the control terminal now, the process has become a leader of the session without terminal. But it can reapply, open a control terminal. You can prohibit process re-opening control terminals by making the process no longer a session group length: IF (PID = fork ()) exit (0); // End the first child process, the second child process continues (the second child is not A further session team leader) 4. Close the open file descriptor process inherits the open file descriptor from the created parent process. If it is not closed, the system resources will be wasted, resulting in the file system in which the process is located cannot be removed and causing an unpredictable error. Turn off them as follows: for (i = 0; i close the open file descriptor Close (i);> 5. When you change the current work directory process activity, the file system where its work directory cannot be removed. Generally, you need to work The directory changes to the root directory. For the process that requires dump core, the write line log will change the working directory to a specific directory such as / tmpchdir ("/") 6. Resetting the file created a mask process inherited from the created parent process. The file creates a mask. It may modify the access bit of the file created by the daemon. To prevent this, create a mask clearance: umask (0); 7. Processing the SIGCHLD signal processing SIGCHLD signal is not necessary. But For some processes, especially server processes are often generated when requests to generate a sub-process processing request. If the parent process does not wait for a child process, the child process will become a zombie process (Zombie) thus occupying system resources. If the parent process is waiting to end the child The burden of the parent process will increase, affect the concurrency performance of the server process. The operation of the SIGCHLD signal can be simply set to SIG_IGN. Signal (SIG_IGN) under Linux; this, the kernel does not generate a zombie process at the end of the child. This point is different from BSD4, and BSD4 must be explicitly waiting for the sub-process to end to release the zombie process. 3. Daemon example daemon includes two parts: main program Test.c and initializer init.c. Main program every other The Log Test.log reported in the / TMP directory reports the running status. The init_daemon function in the initializer is responsible for generating a daemon. The reader can use the init_daemon function to generate its own daemon.