Guardian Process Design

Source: Internet
Author: User

1. What is the Guardian process?

The daemon, which is often said to be Daemon (ELF), is a service process in Linux. It is characterized by:

Do not occupy the control terminal (running in the background)

Independent of the control terminal (there is no corresponding TTY using command Ps-aux view)

Periodic operation

Example: SMBD

Note L: When running the executable program, add &, which means to run in the background.

2. Design elements of the daemon process

2.1 Running in the background

The daemon needs to be independent of any one control terminal. The implementation method call is made by creating a child process to act as a daemon, and the parent process exits, so that the process can run in the background.

Pid=fork ();

if (pid>0)

Exit (0);//Parent process exits

else if (pid==0)

{

Daemon process

}

This program is not independent of the control terminal.

2.2 Independent of Control terminal

The daemon cannot occupy the control terminal, so it needs to run in the background. The implementation method is to call the Setsid () function.

Pid=fork ();

if (pid>0)

Exit (0);//Parent process exits

else if (pid==0)//Daemon

{

Setsid ();

。。。。。

}

2.3 Getting rid of the effects of the parent process:

1, modify the working directory;

The file system on which the working directory resides cannot be uninstalled when the process is active. For example, if we start the daemon from the/mnt/usb directory, then if the daemon's working directory is/MNT/USB, we will no longer be able to umount/mnt/usb in the case where the daemon is still running. Therefore, it is generally necessary to switch the daemon's working directory to the root directory. Use ChDir ("/");

2, reset the file permission mask;

The file permission mask refers to the corresponding bit in the file permission that is masked out. For example, the mask is 500, it blocks the file creator's readable and executable permissions. Because the child process inherits the file permission mask of the parent process, which inevitably affects the access rights of the newly created file in the child process, it is necessary to re-zero the permission mask in the sub-process to avoid this effect. The usual use method is the function: umask (0);

3, close the file descriptor;

As with the file permission code, the child process 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 cause the file system where the files are located to fail to unload. Therefore, these files need to be closed in the child process.

for (i=0;i<maxfile;i++)

Close (i);

The role of this daemon is to continually write content to/tmp/daemon.log

Modified: Flag is initialized to 1.

Guardian Process Design

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.