preface
In my previous article, IO Model for Server-side Network Programming, I explained that the core of high-performance server-side network programming is architecture, and the core of architecture is the choice of process/thread model. This paper will mainly introduce the traditional and current popular process/thread model. Before talking about process/thread model, we need to introduce a design model: Reactor model.
The Reactor pattern is event-driven, with one or more concurrent input sources, a Service Handler, and Request Handlers. The Service Handler synchronously multiplexes incoming requests to the corresponding Request Handler. If the graph is shown as follows:
I don’t know if readers have noticed that the Reactor model is very similar to the IO multiplexing model in the IO model, and they have been puzzled by these two concepts for a long time in the process of learning network programming. In fact, I/O multiplexing also uses the Reactor pattern at the design pattern level. The IO multiplexing model can be thought of as the APPLICATION of the Reactor model to the IO model, and today we’ll talk about the application of the Reactor model to the process/thread model.
In my opinion, the process/thread model can be divided into two types: non-reactor model and Reactor model (and of course, Proactor model, which is not covered in this article because it is rarely used and I don’t understand it yet). There are many kinds of process/thread models of non-REACTOR model and Reactor model, which will be listed one by one later. The process/thread model of non-REACTOR model is a traditional model, which is very rare now. The main purpose of this article is to let readers understand and compare with the process/thread model of Reactor model.
Process/thread models that are not Reactor patterns
Traditional models do not use IO multiplexing, so there are more problems. Beginners are advised to skip this section if you feel confused or uninterested and go straight to the Reactor Model section. But points 1 and 2 of this section need to be understood.
1. Single process single thread
Advantages: simple code, no need to understand the concept of process, thread, suitable for learning network programming beginners. The default model without understanding the process/thread model.
Disadvantages: no practical value.
2, single process multi-threading
Advantages: Connections can be established with multiple clients at the same time, and connection services are received and processed separately.
Disadvantages: Each connection takes up one thread, resulting in a waste of thread resources when there is no data on the connection, and the number of connections that can be established is limited.
3, multi-process single thread
Description:
(1) When the main process starts, it creates a listening socket and listens, and then forks out N child processes.
(2) Due to the inheritance of the parent process, the child process also listens on the port, and then closes the listening on the parent process.
(3) The parent process is responsible for creating, destroying, and recycling the child process, and the child process is responsible for establishing the connection ->Read-> service processing ->Write.
Since all processes are listening on the same port, this model has a well-known phenomenon called stampeders: when a connection is made, all child processes wake up, but only one of them can connect to the Client, resulting in a waste of resources (communication scheduling is also CPU consuming). Since Linux 2.6, however, the kernel has been able to eliminate the scare, which only wakes up a process waiting on Accept () when a connection arrives. Even if the kernel is not fixed, you can use locks to prevent swarms in the application layer.
Cons: This model is an evolutionary version of single-process, single-thread, but not very useful. And increased the difficulty of development. Therefore, the advantages of this model are not listed. The introduction of this model is mainly to introduce the concept of shock group. Similar situation will occur in the case of multiple processes in the Reactor model.
Process/thread model for the Reactor pattern
This pattern is usually a Reactor model + IO multiplexing, and each of the following models has some practical scenarios.
1. Single process single thread
Description: There is only one process, and events on listening sockets and connecting sockets are handled by Select.
(1) If a connection request is received, the Acceptor accepts it and establishes a connection with it. The connection socket is added to the Select socket for listening.
(2) If there is a Read event on a connection, Read-> service processing ->Write operations;
(3) The cycle repeats.
Advantages: Simple programming, for business processing not complex background, can basically meet the server side network programming. The old company’s server-side applications were all in this mode for the following reasons
(1) If a machine is not performing well, add another machine to the cluster.
(2) Business processing is not complicated.
(3) Extended into multi-process, if not multi-core meaning is not much.
(4) if the use of single-process multi-threading, C++ processing thread is not as simple as Java, but also consider the problem of concurrency, the benefit ratio is not big.
Disadvantages: It is blocked and cannot perform other operations, such as establishing connections and reading data on other sockets, when services are being processed.
2, single process multi-threading
Advantages: Relatively perfect process/thread model with low complexity in Java implementations. Many network libraries are based on this, such as Netty.
Disadvantages: To be supplemented.
3, multi-process single thread:
Advantages: Relatively simple programming, make full use of multi-core. Supports high concurrency, otherwise NGINx would not be able to adopt this mode.
Disadvantages: Child processes can still block business processing.
4, multi-process multi-threading
Description: instead of drawing a graph, the child processes the business to multithreading, refer to the thread pool in single-process multithreading.
Advantages: Take full advantage of multi-core simultaneous child processes without blocking business processes
Disadvantages: Complex programming.
5, master and slave processes + multithreading:
(1) The master process Select blocks on the listening socket, establishes a connection with the request, and passes the connection socket to the slave process.
(2) The slave process on the connection socket Select block, as soon as there is data on the connection to Read, and the business process through the thread to process. Write data to the connection if necessary.
Advantages: The establishment of connections and the reading and writing of connections are separated in different processes, which improves the processing efficiency. This model is a bit better than the single-process multithreaded model, and can also take advantage of multiple cores.
Disadvantages: Complex programming.
conclusion
This is a common process/thread model. A thread model that uses I/O multiplexing is generally called a Reactor model, so there is no need to confuse the RELATIONSHIP between I/O multiplexing and the Reactor model. From C++ to Java, although no longer engaged in network programming. However, after reading “Netty authoritative Guide”, I would like to talk about some elements that need to be considered in the design of a network library based on my previous work experience, and do some Netty sharing at the same time. It will be published in a follow-up article, so stay tuned! Remember concern plus public account oh, recording a C++ programmer to Java learning road.