In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > IT Information >
Share
Shulou(Shulou.com)11/24 Report--
This article comes from the official account of Wechat: low concurrency programming (ID:dibingfa). Author: flash.
In order to talk about multiplexing, of course, we still have to follow the trend, adopt the idea of whipping corpses, first talk about the disadvantages of traditional network IO, and hold up the advantages of multiplexing IO by trampling.
For ease of understanding, all of the following code is pseudo-code, so you just need to know what it means.
Let's go
Blocking the IO server writes the following code in order to process the client's connection and requested data.
Listenfd = socket (); / / Open a network communication port bind (listenfd); / / bind listen (listenfd); / / listen while (1) {connfd = accept (listenfd); / / Block establish connection int n = read (connfd, buf); / / Block read data doSomeThing (buf); / / do something close (connfd) with read data / / close the connection, loop for the next connection} this code will stumble, like this.
As you can see, the thread on the server side is blocked in two places, one is the accept function and the other is the read function.
If we expand the details of the read function again, we will find that it is blocked in two stages.
This is the traditional blocking IO.
The overall process is shown below.
So, if the client of this connection does not send data all the time, the server thread will always block on the read function and will not be able to accept other client connections.
This is definitely not gonna work.
Non-blocking IO in order to solve the above problem, the key is to modify the read function.
One smart way to do this is to create a new process or thread each time to call the read function and do business processing.
While (1) {connfd = accept (listenfd); / / blocking connection establishment pthread_create (doWork); / / creating a new thread} void doWork () {int n = read (connfd, buf); / / blocking read data doSomeThing (buf); / / what close (connfd) to do with the read data / / close the connection and cycle for the next connection} so that when a connection is established to a client, you can immediately wait for a new client connection without blocking the read request of the original client.
However, this is not called non-blocking IO, it just uses multithreading to keep the main thread from going down without getting stuck on the read function. The read function provided by the operating system is still blocking.
So the real non-blocking IO, not through our user-level tricks, but to implore the operating system to provide us with a non-blocking read function.
The effect of this read function is that if no data arrives (arriving at the network card and copied to the kernel buffer), an error value (- 1) is immediately returned instead of blocking waiting.
The operating system provides the ability to set the file descriptor to non-blocking before calling read.
Fcntl (connfd, F_SETFL, O_NONBLOCK); int n = read (connfd, buffer)! = SUCCESS); in this way, the user thread is required to call read in a loop until the return value is not-1 before processing the business.
Here we notice a detail.
Non-blocking read means that the data is non-blocking before it arrives, that is, before the data reaches the network card, or before it is copied to the kernel buffer.
When the data has reached the kernel buffer, the call to the read function is still blocked and you need to wait for the data to be copied from the kernel buffer to the user buffer before returning.
The overall process is as follows
IO multiplexing creates a thread for each client, and the thread resources on the server side can easily be consumed.
Of course, there is a clever way to put the file descriptor (connfd) in an array after each accept client connection.
Fdlist.add (connfd); then get a new thread to iterate through the array, calling the non-blocking read method of each element.
While (1) {for (fd)
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.