TCP Concurrent Server programming is essential for network services. TCP concurrency usually has several fixed design patterns. They have their own advantages and applications. The following briefly discusses the differences between these modes:
1. Single process, single thread mode
After accept, data receiving and receiving on the connection is started. After receiving the data, the connection is processed, sent, and new connections are not received, unless the processing of the connection is completed.
Advantages:
Simple.
Disadvantages:
Because it is only a client service, there is no possibility of concurrency.
Application:
It is used only for one client service.
2. multi-process mode
When accept returns a successful result, it fork the connection and processes the data sent and received on the connection. The process will end after the connection is processed.
Advantages:
Programming is relatively simple, so you do not need to consider data synchronization between threads.
Disadvantages:
High resource consumption. The consumption of starting a process is much greater than that of starting a thread. At the same time, when processing many connections, many processes need to be started to process more. At this time, the pressure on the system will be relatively high. In addition, the number of processes in the system must be considered.
Application:
It is easy to use when there is not much client data, for example, less than 10 clients.
3. Multithreading
Similar to the multi-process method, but starts a thread for a connection.
Advantages:
Compared with the multi-process method, it will save some resources and be more efficient.
Disadvantages:
Compared with the multi-process method, programming complexity is increased because data synchronization and lock protection need to be considered. Too many threads cannot be started in another process. In Linux, threads are actually processes in the system, and thread scheduling is executed according to the process scheduling method.
Application:
This method is similar to the multi-process method. It is applicable to a small number of clients.
4. Select + multithreading Mode
There is a thread dedicated to listening to the port. After the accept returns, this descriptor is put into the descriptor set FD. A thread uses the select round training descriptor set to receive data over a data connection, another thread specifically sends data. Of course, you can also use a thread to receive and send messages. The descriptor can be set to non-blocking mode or blocking mode. Generally, the connection is set to non-blocking mode, and the sending thread is independent.
Advantages:
Compared with the previous modes, this mode greatly increases the concurrency.
Disadvantages:
Generally, the system uses a large array to implement the descriptor set. When the SELECT statement is called each time, the descriptor array is poll. When the number of connections is large, the efficiency will decrease. When the number of connections exceeds 1000, the efficiency will decrease to unacceptable.
Application:
At present, TCP concurrency on Windows and Unix generally adopts the select method. It should be said that the application is still very extensive.
5. epoll Mode
Epoll is added after linux2.6. The specific usage is: a thread is dedicated to port listening. When accept receives a connection, it sets the connection to non-blocking mode and sets the epoll event to edge triggering mode, add to epoll management. The wait event function of the receiving thread blocking in epoll. Another thread is used to send data.
Advantages:
Because epoll is advanced in implementation, this method can implement concurrency on a large scale. Our current application in a Dell PC server three years ago can achieve 20 thousand concurrent connections, and the performance is also good.
Disadvantages:
Because it involves threads and non-blocking, the encoding complexity increases. This method is only applicable to Linux 2.6
After the kernel.
Note:
1. If the epoll event is set to a horizontal trigger, the efficiency will be reduced to a similar select level.
2. In Unix systems, there is a limit on the number of opened descriptors for a single process, and a limit on the number of opened descriptors in the system. The number of opened descriptors in the system is limited by two. The hardware limitation varies depending on the machine configuration. The soft limit can be changed, but it must be smaller than the system's hard limit. In SuSE Linux, you can modify the limit by the number of ulimit-N under the root user.
Application:
Large-scale TCP concurrency in Linux.
There are other concurrent methods, such as the process pool and thread pool. Each mode has its advantages and disadvantages. It is most important to use the appropriate mode when appropriate.
If a large number of concurrent jobs are required and tested, I suggest using the epoll method to discard the normal select method.