Linux process pool, thread pool

Linux process pool, thread pool

A child process is created through process dynamic memory allocation. Although this way of creating a child process can obtain resources, it has many disadvantages.

1. Dynamically creating a process is time-consuming, which will result in slower customer response. 
2. The dynamically created child process (or child thread) is usually only used to serve one client. This will result in a large number of processes (threads) on the system, which will consume a lot of CPU resources when switching between processes (threads). 
3. The dynamically created child process (thread) is a complete mapping of the parent process (thread). The current process (thread) must carefully manage system resources such as file descriptors and heap memory allocated by it, so that the available resources of the system will drop sharply, thereby affecting server performance. 
4. Due to the limited resources of the system, there will be a limited number of dynamically created processes (threads), which will increase the number of responses to client requests.

In order to effectively solve customer requests under a large number of connections, it is necessary to adopt a method to avoid the shortcomings brought about by dynamic creation. The process (thread) pool is divided into a system resource area in advance, which has been created and initialized when the system starts. Users can obtain resources directly, thus avoiding dynamic allocation of resources.

The concept of process pool thread pool

The process pool is very similar to the thread pool, so only the process pool is introduced.

1. The process pool is a group of processes pre-created by the server. The number of these sub-processes is between 3 and 10. The http daemon process uses a process pool containing 7 sub-processes to achieve concurrency. The number of threads in the thread pool is about the same as the number of CPUs. 
2. All child processes in the process pool run the same code and have the same attributes, such as priority, PGID, etc. Because the process pool is created at the beginning of the server startup, they do not need to open unnecessary file descriptors (inherited from the parent process), nor use a large heap memory by mistake (copying the parent process) 
. Compared with dynamically creating subprocesses, the cost of selecting an existing subprocess is obviously much smaller.

Subprocess Selection Algorithm

1. The main process actively selects the child process through a certain algorithm. The simplest and most commonly used algorithm is the round-robin algorithm. 
2. The main process and all child processes are in the same shared queue, and they are all in sleep state at the beginning. When a new task arrives, the main process will add the task to the message queue. An algorithm will be used to wake up one of the child processes, and this child process will take out and execute the task, while the other processes are still in a sleep state. 
3. When a sub-process is selected, the main process also needs to tell the sub-process the data needed to perform the task, and to pass these data will create a pipeline between the sub-process and the main process. All these data can be defined as global variables, and these data will be shared by all threads.

Guess you like

Origin blog.csdn.net/prokgtfy9n18/article/details/76801939