Knowledge about the Linux creation daemon

Source: Internet
Author: User

Knowledge about the Linux creation daemon

Http://www.114390.com/article/46410.htm

Linux Creation Daemon Knowledge, this article mainly introduces the Linux creation Daemon knowledge, the need for friends can refer to the following

keywords:linux, daemon

Create child process, parent process exits
This is the first step in the process of writing the daemon. Since the daemon is out of control, the first step is to create the illusion that a program has finished running in the shell terminal. All subsequent work is done in the child process, and the user can execute other commands in the shell terminal to disengage 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, and each time the system discovers an orphan process, it is automatically adopted by the 1th process (init), so that the original child process becomes a child of the INIT process.
create a new session in a child process
This step is the most important step in creating the daemon, although its implementation is very simple, but its significance is very significant. The system function Setsid is used here, and before we introduce SETSID, we need to understand two concepts: Process group and session period
Process group: is a collection of one or more processes. The process group has a process group ID to uniquely identify. In addition to the process number (PID), the process group ID is also a prerequisite property for a process. Each process group has a leader process, and the process number of its leader process equals the process group ID. And the process group ID will not be affected by the exit of the leader process.
Session Period: The session period is a collection of one or more process groups. Typically, a session begins to log on with the user and terminates when the user exits, during which all processes running by that user are part of the session.
Next, you can specifically describe the SETSID content:
(1) Setsid function:
The SETSID function is used to create a new session and serve as the leader of the conversation group. Calling Setsid has the following 3 functions:
Let the process get rid of the control of the original session
Let the process get rid of the control of the original process group
Let the process get rid of control of the original control terminal
So why call the SETSID function when creating the daemon? Because the first step in creating the daemon calls the fork function to create the child process, and then exits the parent process. Because when the fork function is called, the child process copies the session period, the process group, the control terminal of the parent process and so on, although the parent process exits, but the session period, the process group, the control terminal are not changed, so it is not really independent, and the SETSID function can make the process completely independent, So as to get rid of the control of other processes.
change the current directory to the root directory
This step is also a necessary step. A child process created with fork inherits the current working directory of the parent process. Because the file system in which the current directory resides (such as "/mnt/usb") is not unloaded while the process is running, this can cause a lot of trouble for later use (such as the system entering single-user mode for some reason). Therefore, the usual practice is to let "/" as the current working directory of the Daemon, so as to avoid the above problems, of course, if you have special needs, you can also change the current working directory to other paths, such as/tmp. A common functional chdir that changes the working directory.
Reset file Permission Mask
The file permission mask refers to the corresponding bit in the file permission that is masked out. For example, there is a file permission mask of 050, which masks the file group owner's readable and executable permissions. Because the new child process using the Fork function inherits the file permission mask of the parent process, this creates a lot of trouble for the child process to use the file. Therefore, setting the file permission mask to 0 can greatly enhance the daemon's flexibility. The function that sets the file permission mask is umask. Here, the usual method of use is Umask (0).
Close File Descriptor
As with the file permission code, a new child process with the fork function inherits some files that have already been opened from the parent process. These open files may never be read and written by the daemon, but they consume system resources as well, and may cause the file system in which they reside to fail to be unloaded.

Articles you may be interested in:
    • PHP daemon plus linux command nohup implementation tasks executed once per second
    • Shell scripts are shared as daemon instances that ensure that PHP scripts do not hang out
    • Python implementation of Linux under the daemon process of writing methods
    • Java implementation of Linux under the double daemon process
    • The daemon under Linux
    • Writing Linux system daemon instances using Python
    • C Language writing Linux daemon instance
    • Linux Shell Implementation Daemon script

Knowledge about the Linux creation daemon

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.