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

How to use process pool to achieve high concurrency server

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

What this article shares with you is about how to use process pool to achieve high concurrency servers. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article. Let's take a look at it with the editor.

Necessity of process pool

Why use a process pool / thread pool? Cause:

1. Dynamic creation of processes / threads is time-consuming and leads to slow customer response

2. Dynamically requested processes / threads are usually used to serve only one connection, which will result in a large number of processes / threads in the system, and process switching will consume a lot of CPU time.

3. The dynamically created child process is a complete mirror of the current process. Therefore, the current process must carefully manage its allocated system resources such as fd, socket and heap memory, otherwise the replication of these resources by child processes will lead to the decline of system available resources, thus affecting the performance of the server.

The concurrent server implemented by the process pool can solve the above problems. A process pool is a set of child processes pre-created by the server, all of which run the same code and have the same attributes, such as priority, PGID (process group), etc., without opening unnecessary file descriptors and without using large chunks of heap memory. When a new client connection arrives, the main process uses the equalization algorithm to select a child process from the process pool to serve, which is much less expensive than the temporary creation process. As for how to select child processes, there are two ways:

1. Random / alternate selection of idle child processes

2. The main process and all child processes share a work queue, and the child processes sleep on the queue and wake up a child process when a new connection arrives.

After selecting a good child process, the main process also needs to inform the target child process through some mechanism that there is a new task to deal with and transmit the necessary data. The easiest way is for the parent-child process to set up the pipeline in advance. There is no need for pipes for thread pools because the parent and child threads themselves share global data.

To sum up, the implementation of process pool is as follows:

Dealing with multiple customers

One of the issues to consider when using process pools to handle concurrent connections is whether both listen socket and connect socket events are managed by the main process.

Server programs usually need to deal with three types of events: IO events, signal signal and timing events. The synchronous IO model is usually used to implement the reactor pattern, and the asynchronous IO model is used to implement the proactor pattern. Combined with the synchronous and asynchronous IO model, the following two server event handling modes can be designed:

Semi-synchronous / semi-reactor mode

Semi-synchronous / semi-asynchronous mode

The specific introduction of IO reuse model refers to the sixth chapter of UNIX network programming, which will not be carried out here.

Semi-synchronous / semi-asynchronous mode

In the first mode, the master process uniformly manages the two kinds of socket, because the master and slave processes share the request queue, and any operation on the queue needs to be locked, which affects the performance of the CPU. In the second mode, the main process manages all listening socket, and the child process manages its own connection socket. The child process can call accept to obtain the TCP connection queue, and the main process only needs to notify the implementation method:

A Unix domain socket is established between the two processes as a channel for message delivery (using the socketpair function), and then the parent process calls sendmsg to send a special message to the channel. The kernel will do special processing to pass the message to the child process, which calls recvmsg to receive the message from the channel.

In addition, when designing the process pool, you need to consider whether multiple requests from the same client can reuse a TCP connection (just add a keepalive field at the http level). If the customer's request is stateless, the server can use different child processes to serve the customer's different requests:

If a customer task has a contextual state (such as a shopping cart, which can never be refreshed and emptied at a time), the customer's request can only be processed with the same subprocess. Using the EPOLLONESHOT feature of epoll ensures that customer connections are handled by only one process throughout the lifecycle.

For a socket that registers EPOLLONESHOT events, the operating system triggers its registered event at most once, and only once. Therefore, when the child processes events on the socket, it is not possible for other processes to manipulate the socket.

Concrete realization

Defining a class process that describes the child process, including the PID of the child process, and the pipeline pipe; process pool for parent-child process communication uses single mode to ensure that the program creates only one instance of the process pool, which is a necessary condition for the program to handle signals correctly:

The following is the data created after the parent process starts, including the pipeline that processes the signal, the array that carries the child process information, the process identity, and the epoll event table, and is initialized. So when you call fork to generate a child process, the child process inherits the data, and the child process saves its own data.

The execution process of the parent process is:

For more information about SIGNAL signals, refer to Chapter 10 of Advanced programming in the UNIX Environment:

The communication between the parent and child processes in the process pool using signals is as follows:

Child process implementation

The data for the child process is as follows:

The child process flow is as follows:

When using a process pool, the listening socket is usually created by the main function, and the socket needs to be closed after exiting.

The above is how to use process pool to achieve high concurrency server. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

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

Internet Technology

Wechat

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

12
Report