Linux daemon, write

Source: Internet
Author: User
Set the daemon process:
1. Create a child process and the parent process exits.
2. Create a session in the sub-process
3. Change the current directory to the root directory.
4. Reset the File Permission mask.
5. Disable the file descriptor
* 6. In the daemon, The sigchld signal is ignored (even if the fork is two times, the new process is taken over by init, and the signal (sigchld, sig_ign) operation is not performed, the zombie process will also be generated .)

Daemon overview many services will be enabled during system guidance in Linux or UNIX operating systems. These services are called daemon. To increase flexibility, the root user can select the system enabling mode, which is called the running level. Each running level configures the system in a certain way. A daemon is a process that runs in the background and is out of the terminal. The daemon is separated from the terminal to prevent the information in the execution process from being displayed on any terminal and the process is not interrupted by any terminal information generated by any terminal. Daemon processes are the background service processes in Linux. It is a long-lived process, usually independent of the control terminal and periodically executes a task or waits to process some events. Daemon is often started during system boot and ended when the system is shut down. In Linux, there are many daemon processes. Most services are implemented through daemon. At the same time, daemon can complete many system tasks, for example, job planning process crond and printing process lqd (the ending letter D indicates Daemon ). In Linux, every system interface that communicates with users is called a terminal. Every process starting from this terminal is attached to this terminal, which is called the control terminal of these processes, when the control terminal is closed, the corresponding process is automatically closed. However, the daemon can break through this restriction. It does not exit until it is executed. To prevent a process from being affected by user, terminal, or other changes, you must turn the process into a daemon. Create a daemon and create a child process. The parent process exits. This is the first step to write a daemon. Because the daemon process is out of the control terminal, after completing the first step, it will create the illusion that a program has been completed on the shell terminal. All subsequent work is completed in the sub-process, and the user can execute other commands in the shell terminal, thus achieving the separation from the control terminal in form. In Linux, the parent process exits before the child process, causing the child process to become an orphan process. Every time the system finds an orphan process, it is automatically adopted by process 1 (init, in this way, the original sub-process will become the sub-process of the INIT process. Creating a session in a sub-process is the most important step in creating a daemon. Although its implementation is very simple, it is of great significance. The system function setsid is used here. Before introducing setsid, you must first understand two concepts: Process Group and session process group: A collection of one or more processes. A process group is uniquely identified by a process group ID. In addition to the process ID (PID), the process group ID is also a required attribute of a process. Each process group has a leader process. The process ID of the leader process is equal to the Process Group ID. The process group ID will not be affected by the exit of the leader process. Session cycle: a set of one or more Process Groups. Generally, a session starts to log on to the user and ends when the user exits. During this period, all processes run by the user belong to this session period. Next, we will introduce the related content of setsid: (1) Role of setsid function: setsid function is used to create a new session and act as the group leader. Calling setsid has the following three functions: getting the process out of control of the original session, getting the process out of control of the original process group, and getting the process out of control of the original control terminal, why call the setsid function when creating a daemon? The fork function is called to create a child process in the first step of daemon creation, and then the parent process is exited. When the fork function is called, the sub-process completely copies the session period, process group, and control terminal of the parent process. Although the parent process exits, however, the session period, process group, and control terminal are not changed. Therefore, it is not truly independent, and the setsid function can make the process completely independent, to get rid of the control of other processes. This step is also necessary to change the current directory to the root directory. The child process created using fork inherits the current working directory of the parent process. Because the file system in the current directory (such as "/mnt/USB") cannot be detached while the process is running, this will cause a lot of trouble for future use (for example, the system needs to enter but the user mode for some reason ). Therefore, the common practice is to make "/" the current working directory of shohujincheng to avoid the above problems. Of course, if you have special requirements, you can also change the current working directory to another path, such as/tmp. Change the common functional chdir of the working directory. Resetting the File Permission mask is used to block the corresponding bits in the file permission. For example, if a File Permission mask is 050, the file group owner's read and executable permissions are blocked. Because the child process created using the fork function inherits the File Permission mask of the parent process, this causes a lot of trouble for the child process to use files. Therefore, setting the File Permission mask to 0 greatly enhances the flexibility of the daemon process. The UMASK function is used to set the File Permission mask. Here, the general usage is umask (0 ). Close the file descriptor is the same as the File Permission code. The child process created by using the fork function will inherit some opened files from the parent process. These opened files may never be read or written by the daemon, But they consume the same system resources and may cause the file system to be unable to be detached. After step 2, the daemon has lost contact with the control terminal. Therefore, the characters entered from the terminal cannot reach the daemon process, and the characters output by conventional methods (such as printf) in the daemon cannot be displayed on the terminal. Therefore, the three files whose file descriptors are 0, 1, and 2 (input, output, and error) have lost value and should be disabled. Disable the file descriptor as follows: ================================== for (I = 0; I <maxfile; I ++) Close (I ); ==================================, A simple daemon is created. From: http://www.360doc.com/content/10/0610/11/61151_32290457.shtml

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.