In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article focuses on "how to understand common IO models: blocking, non-blocking, multiplexing, out-of-step". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to understand common IO models: blocking, non-blocking, multiplexing, and asynchronism.
In Unix network programming, Stevens gives five IO programming models, of which the most important and commonly used is the multiplexing model (Multiplexing). The five models are:
Blocking IO
Non-blocking IO
IO Multiplexing (multiplexing io), based on select/poll/epoll
Signal-driven IO SIGIO
Asynchronous IO (posix aio_abi and libaio)
A good way to explain these models is to divide the network IO into two stages. In the first stage, the client sends a request to the server until the data is transmitted from the network and is completely ready. In the second stage, data is copied from kernel space to the program cache (that is, user space), which is the stage in which the application actually performs recvfrom system calls. In the first phase, the program either blocks recvfrom calls, blocks methods such as select, or does something else (polling, async, etc.).
With this basic understanding, let's look at these models one by one.
Blocking Imax O model
Blocking IO is the most basic IO model, and it is also the default mode in daily use.
As shown in the figure above, the application makes a request and attempts to execute a recvfrom system call to get the data. Because the data is not ready (perhaps the server begins to process the request), the current request thread is blocked and can only wait until the data is readable or an exception is thrown. If it is a single-threaded application, the main thread hangs and the CPU is left empty. If it is multithreaded, the current thread hangs and CPU switches time slices to execute other threads.
Non-blocking IO model
A normal Socket object has a method such as setblocking (False) or ConfigureBlocing (false) that sets the current IO thread to non-blocking mode.
As shown in the figure, when socket is set to nonblocking, the thread will continue to initiate recvfrom polling in the first phase (data preparation phase, remember the two-phase IO mentioned earlier, which is useful here). If the data is not ready, it will get an EWouldBLOCK error signal until the data is ready, and the recvfrom will actually pull the data. Polling consumes a lot of CPU time, so this model is rarely used.
IO multiplexing
I multiplexing O reuse, which is a programming model based on select function, is also the most commonly used one. Some people like to call it the asynchronous blocking model, which I think is easy to misunderstand. in fact, this model has nothing to do with the so-called asynchronous Ibank O.
As shown in the figure above, in the first phase, we can register multiplex IO objects through the select function. Every registered IO object blocks on the select function until the state of the IO object changes. At this point the data is ready to read / write. The scheduler iterates through these IO objects and returns IO objects that are ready to read / write. Immediately after entering the second phase, we begin to call the recvfrom function on the prepared IO object.
For the multiplexing model, Java NIO implements the selectors module of the scheduler Selector,python based on select library, which provides encapsulation objects based on select, poll and epoll libraries respectively.
Through the above process, we can also see that, in fact, IO multiplexing is very similar to blocking IO, basically both phases are in a blocking state. It's just that the former blocks on the select function in the first stage and on the recvfrom call in the second stage, while the latter blocks on the recvfrom all the way. And IO multiplexing actually involves more step overhead because of the need to register and traverse IO objects. But the advantage of multiplexing is that it can connect multiple IO objects at the same time, combined with multithreading technology, it can bring great flexibility.
Asynchronous IO model
The three models mentioned earlier are essentially blocked in the real IO phase (the second phase). In the asynchronous IO model, after the application sends out a data request, it no longer waits and returns directly. During this period, the thread does not block. After that, the kernel processes the two stages of IO, then sends a signal to the application, and the program gets the data directly.
Unix-based systems have an POSIX asynchronous IO library, aio_abi, and a third-party library, aiolib, which may be better known. Asynchronous IO models are very complex and rarely seen. I also have some doubts about whether the concept of co-programming proposed by the asyncio package in python is the same thing as the asynchronous IO here. The async described by Stevens here obviously requires a related implementation based on the system kernel, and the simple programming concept of asyncio is probably not talking about the same level of things.
Metaphor
Finally, let's make an analogy. If you compare the IO model to online shopping, it can be regarded as a phase of IO from the moment you submit the order to the courier bringing the goods to the door of your neighborhood. After that, the little brother called you: "your express, open the door", which is equivalent to the data is ready, and sent a notice. After that, you run to the door and move the courier to your home one by one. This is the second stage.
Blocking model is equivalent to you put forward an order, do not eat or drink, just wait for the express delivery, until the little brother called you, and then wheezed the express delivery to the home, you began to eat, drink, sleep and so on. Under the non-blocking model, you start to move freely after you submit the order. But will still run to the door of the community every few minutes to see if the express delivery has arrived (polling), has been exhausted. It won't stop until the little brother calls you and you bring the express home. In the multiplexing model, you submit multiple orders at the same time, but instead of foolishly running back and forth to the door, you keep an eye on the rookie wrapped message (blocked in select) until it shows that at least one delivery has arrived, and you will whoosh past, of course, the delivery will still move itself (recvfrom blocking). Under the asynchronous model, you hire an omnipotent butler, Giffords, as long as you place the order, don't worry about it, he will take care of everything, and finally he will send the express delivery directly to you in accordance with your agreed place and way.
At this point, I believe you have a deeper understanding of "how to understand common IO models: blocking, non-blocking, multiplexing, and out-of-step". Let's do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue 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.