Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the concept of Perl daemon

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces what the concept of Perl daemon is, which can be used for reference by interested friends. I hope you will gain a lot after reading this article.

Perl daemon

Programming method of LinuxPerl daemon

The Perl daemon (Daemon) is a special process that runs in the background. It is independent of the control terminal and periodically performs certain tasks or waits for certain events to be processed. The Perl daemon is a useful process. Most of Linux's servers are implemented using Perl daemons. For example, Internet server, inetd,Web server, httpd, etc. At the same time, the Perl daemon performs many system tasks. For example, job planning process crond, printing process lpd and so on.

The programming of Perl daemon itself is not complicated. What is complicated is that the implementation mechanisms of various versions of Unix are different, resulting in different programming rules of Perl daemons in different Unix environments. This requires the reader's attention that copying some book rules (especially BSD4.3 and earlier versions of SystemV) to Linux will make an error. Following is a comprehensive introduction to the programming points of the Perl daemon under Linux and a detailed example is given.

I. Perl daemon and its characteristics

The most important feature of the Perl daemon is running in the background. At this point, the resident memory program TSR under DOS is similar. Second, the Perl daemon must be isolated from the environment in which it was run. These environments include unclosed file descriptors, control terminals, session and process groups, working directories, and file creation masks. These environments are usually inherited by the Perl daemon from the parent process that executes it, especially the shell. *, the startup method of the Perl daemon has its own special features. It can be started from the startup script / etc/rc.d when the Linux system starts, can be started by the job planning process crond, and can be executed by the user terminal (usually shell).

In short, apart from these particularities, the Perl daemon is basically no different from a normal process. Therefore, writing a Perl daemon is actually transforming an ordinary process into a Perl daemon according to the characteristics of the Perl daemon described above. If the reader has a deeper understanding of the process, it will be easier to understand and program.

II. Programming points of Perl daemon

As mentioned earlier, the programming rules for Perl daemons are not consistent in different Unix environments. Fortunately, the programming principles of the Perl daemon are all the same, but the difference lies in the specific implementation details. This principle is to satisfy the characteristics of the Perl daemon. At the same time, Linux is a SVR4 based on SyetemV and follows the Posix standard, so it is more convenient to implement than BSD4. The main points of programming are as follows

1. Run in the background.

To avoid suspending the control terminal, the Daemon is put into the background for execution. The method is to call fork in the process to terminate the parent process and let the Daemon execute in the background in the child process.

If (pid=fork ())

Exit (0); / / is the parent process, ends the parent process, and the child process continues

two。 Detach from the control terminal and log in to the session and process group

It is necessary to introduce the relationship between the process and the control terminal, the login session and the process group in Linux: the process belongs to a process group, and the process group number (GID) is the process number (PID) of the process leader. A login session can contain multiple process groups. These process groups share a control terminal. This control terminal is usually the login terminal that creates the process.

The control terminal, login session, and process group are usually inherited from the parent process. Our aim is to get rid of them so that they will not be affected by them. The method is to call setsid () on the basis of point 1 to make the process the session leader:

Setsid ()

Description: the call to setsid () fails when the process is the session leader. However, the * * point has guaranteed that the process is not the session leader. After a successful call to setsid (), the process becomes the new session leader and the new process leader, and is detached from the original login session and process group. Because the session process is exclusive to the control terminal, the process is separated from the control terminal at the same time.

3. Prohibit the process from reopening the control terminal

Now, the process has become a non-terminal session leader. But it can reapply to open a control terminal. You can prevent the process from reopening the control terminal by preventing the process from becoming the session leader:

If (pid=fork ())

Exit (0); / / ends the * child process, and the second child process continues (the second child process is no longer the session leader)

4. Close the open file descriptor

The process inherits the open file descriptor from the parent process that created it. If it is not shut down, system resources will be wasted, the file system on which the process is located cannot be unmounted, and unexpected errors will be caused. Close them as follows:

For (iDestruct I) closes open file descriptor close (I)

5. Change the current working directory

When a process is active, the file system where its working directory is located cannot be unmounted. You generally need to change the working directory to the root directory. For the core to be dumped, the process that writes the running log changes the working directory to a specific directory such as / tmpchdir ("/")

6. Reset the file to create the mask

The process inherits the file creation mask from the parent process that created it. It may modify the access bits of files created by the Perl daemon. To prevent this, clear the file creation mask: umask (0)

7. Processing SIGCHLD signal

It is not necessary to process SIGCHLD signals. However, for some processes, especially server processes, they often generate child processes to process requests when the request arrives. If the parent process does not wait for the child process to finish, the child process will become a zombie process (zombie) and consume system resources. If the parent process waits for the child process to finish, it will increase the burden on the parent process and affect the concurrency performance of the server process. Under Linux, the operation of SIGCHLD signal can be simply set to SIG_IGN.

Signal (SIGCHLD,SIG_IGN)

In this way, the kernel will not produce a zombie process when the child process ends. This is different from BSD4, where you have to explicitly wait for the child process to finish before you can release the zombie process.

Thank you for reading this article carefully. I hope the article "what is the concept of Perl daemon" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report