In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces what are the implementation patterns of concurrent servers in Linux, which can be used for reference by interested friends. I hope you will gain a lot after reading this article.
1 > single thread or single process
It is equivalent to a short link. After accept, it starts to receive and send data. It does not accept a new connection, that is, a server or a client.
There is no concurrency.
2 > Circular server and concurrent server
1. Circular server: a server can only receive one client at a time, and the next client connection can not be made until the current client ends its access.
two。 Concurrent server: a server can respond to visits from many clients at the same time.
3 > select+ multithreading mode
Three implementation methods of concurrent Server
1. Multi-process concurrent server
After the TCP connection, the request of each client is not directly processed by the server, but a child process is created by the server to handle it.
two。 Multithreaded concurrent server
The multi-process server is an improvement of the multi-process server. Because the multi-process server consumes a large amount of system resources when creating a process, it uses threads to replace the process, so that the service handler can be created quickly. According to statistics, creating a thread is 10100 times faster than creating a process, so a thread is called a "lightweight" process. The difference between a process and a process is that all threads in a process share the same global memory, global variables and other information.
After the TCP connection, the request of each client is not directly processed by the server, but created by the server.
3. Multiplexing Ipaw O
Select O is used to solve thread / process blocking. Select or pool is often used in that call.
4 > epoll
In the epoll after linux2.6, the practical method is to use a thread to listen to the port. When the accept receives the connection, it sets the connection to non-blocking mode, sets the epoll time to edge trigger mode, and joins the epoll management. The receiving thread blocks the wait event function in epoll. Another thread is dedicated to data transmission.
Note:
1. If epoll is set to horizontal trigger efficiency, the level of select adoption will be reduced.
Under the 2.Unix system, there is a limit on the number of descriptors opened by a single process, as well as the number of open descriptors in the system. The number of descriptors open in the system is limited by two soft and hard links. Hard connections vary according to the configuration of the machine. The soft connection limit can be modified but must be less than the hard limit.
Application:
Large-scale TCP concurrency under Linux.
Currently, there are other ways of concurrency. Like a thread pool. Process pool, etc., each mode has its advantages and disadvantages, if large-scale concurrency, it would be better to use epoll.
The time settings of epoll include edge trigger mode and horizontal trigger mode.
1. Horizontal trigger mode:
If the file descriptor is ready to perform the IO operation non-blocking, a notification will be triggered. The state of IO is allowed to be repeatedly detected at any time, and it is not necessary to execute IO,select,poll as many times as possible each time the descriptor is ready.
An event is triggered as long as the requirement is met.
two。 Edge trigger mode:
If a new IO activity arrives since the last state change of the file descriptor, a notification is triggered. Perform as many IO actions as possible when receiving an IO event notification, because if the IO is not finished in another notification, you will have to wait until the next new IO activity arrives to get the ready descriptor. Signal-driven IO belongs to edge trigger.
An event is triggered whenever the state changes.
Eg: now there is a 1000-byte message, whether horizontal trigger or edge trigger, will send a read-only notification. When 100 bytes are received, the horizontal trigger will send a read-only notification because there are still bytes to be read, but the edge trigger will keep waiting for the notification and wait for the next message to arrive until the edge trigger returns EWOULDBLOCK and discards the socket.
Thank you for reading this article carefully. I hope the article "what are the implementation patterns of concurrent servers in Linux" shared by the editor will be helpful to you. At the same time, I also hope that 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.