Analysis of several IO models of "I/O Model" (i.)

Source: Internet
Author: User
Tags epoll high cpu usage


Basic concepts of learning :

Synchronous && asynchronous

Synchronization: If there are multiple tasks or events to occur, these tasks or events must be carried out individually, an event or task execution will lead to a temporary wait for the entire process, these events have no way to execute concurrently;

Async: If more than one task or event occurs, these events can be executed concurrently, and an event or task execution does not cause a temporary wait for the entire process.

This is synchronous and asynchronous. For a simple example, if there is a task that includes two subtasks A and B, for synchronization, when a is in the process of execution, B waits until a executes, B can execute, and for async A and B are executed concurrently, B does not have to wait for a to execute, This will not result in a temporary wait for the entire task due to the execution of a.

blocking && non-blocking

blocking: When an event or task is executing, it issues a request operation, but because the requested action requires a condition that is not met, it waits until the condition is satisfied;

Non-blocking: When an event or task is executing, it issues a request action that, if the requested action requires a condition that is not met, immediately returns a flag informing that the condition is not satisfied and will not wait there.

This is the difference between blocking and non-blocking. That is, the difference between blocking and non-blocking is that when a request is made, if the condition is not met, it waits or returns a flag message.

blocking io&& non-blocking IO

in blocking mode, blocking IO is blocked if a specified amount of data is not being read from the network stream . For example, it is known that there will be a total of ten bytes of data sent over, but I now only receive 8 Bytes, then when the front-end is in that silly wait for the next byte to arrive, right, just wait there, Do not do anything, until the ten bytes read, this will block the release of traffic.

in nonblocking mode, non-blocking IO is immediately accessible if the amount of data from the network stream is not read to the specified size . For example, it is known that there will be a total of ten bytes of data sent over, but I now only receive 8 Bytes, then when the front thread reads the 8 bytes of data, After reading, return immediately, and then read when another two bytes come again.

Synchronous io&& asynchronous IO

In synchronous file IO, the thread initiates an IO operation and then immediately goes into a wait state until the IO operation is complete before waking up to continue execution. In asynchronous file IO mode, the thread sends an IO request to the kernel and then continues to handle the other things, and after the kernel completes the IO request, it notifies the thread that the IO operation is complete.

During synchronization, the process triggers an IO operation and waits or polls to see if the IO operation is complete. Asynchronous process after the process triggered IO operation, directly back, do their own things, io to the kernel to handle, after completion of the kernel notification process IO completion

a common network IO Model

The actual process of network IO operation involves the kernel and the process of invoking this IO operation. Take read for example, the specific operation of read is divided into the following two parts:

(1) The kernel waits for data to be read (2) to copy the data read from the kernel to the process

1. Blocking IO model

When the user process invokes the RECVFROM system call, Kernel begins the first phase of IO: Preparing the data. For Networkio, most of the time the data has not arrived at the beginning (for example, a complete UDP packet has not yet been received), at which point the kernel (kernel) waits for enough data to arrive. On this side of the user process, the entire process is blocked. When kernel waits until the data is ready, it copies the data from the kernel to the user's memory, and then kernel returns the result, and the user process removes the block state and re-runs it.

So, the Blockingio feature is that both phases of Io execution are blocked.

   

2. Non-blocking IO model

As you can see, when the user process issues a read operation, if the data in kernel is not ready, it does not block the user process, but returns an error immediately. From the user process point of view, it initiates a read operation and does not need to wait, but immediately gets a result. When the user process determines that the result is an error, it knows that the data is not ready, so it can send the read operation again. Once the data in the kernel is ready and again receives the systemcall of the user's process, it immediately copies the data to the user's memory and then returns.

Therefore, the process of waiting for data to become non-blocking, the user process is actually need to constantly actively ask kernel (kernel) data well No.

So in fact, in a non-blocking IO model, the user thread needs to constantly ask if the kernel data is ready, and it says that non-blocking IO will not hand over the CPU and will always consume the CPU.

  However, there is a very serious problem with non-blocking IO, and in the while loop it is necessary to constantly ask if the kernel data is ready, which can lead to very high CPU usage, so it is generally rare to use a while loop to read the data.

3. multiplexed IO Model

When the user process invokes select, the entire process is blocked, and at the same time, kernel "monitors" all select-responsible sockets, and when the data in any one socket is ready, select returns. This time the user process then invokes the read operation, copying the data from the kernel to the user process.


The figure is not much different from the Blockingio, in fact it is worse. Because there are two system calls (select and recvfrom) that need to be used, Blockingio only calls a system call (Recvfrom). However, the advantage of using select is that it can handle multiple connection at the same time. (So, if the number of connections being processed is not very high, the webserver of using select/epoll is not necessarily better than the Web server using multi-threading + blocking IO and may be much more delayed.) The advantage of Select/epoll is not that a single connection can be processed faster, but that it can handle more connections. )


in io Multiplexingmodel, in practice, for each socket, is generally set to become non-blocking, but, as shown, the entire user's process is actually always block. The process is simply a block of the Select function, rather than being socketio to block.

Compared to the non-blocking IO model, in the multiplexed IO model, a thread continually polls the state of multiple sockets (because in the multiplexed IO model, multiple sockets can be managed using only one thread, and the system does not need to establish new processes or threads or maintain these threads and processes. So it greatly reduces the resource usage).

The reason why the multiplexing Io is more efficient than the nonblocking IO model is that in non-blocking IO, the state of the socket is continually queried through the user thread , and in multiplexed io, polling each socket state is the kernel , This efficiency is much higher than the user thread.

4. Signal-driven IO model

Similar observers

  in the signal-driven IO model, when the user thread initiates an IO request operation, it registers a signal function with the corresponding socket, and then the user thread resumes execution, sending a signal to the user thread when the kernel data is ready, and after the user thread receives the signal, The IO read-write operation is invoked in the signal function to perform the actual IO request operation.

5. Asynchronous IO Model

Asynchronous IO model is the most ideal IO model, in the asynchronous IO model, when the user thread initiates a read operation, it can start to do other things immediately. On the other hand, from the kernel point of view, when it is subjected to a asynchronousread, it returns immediately, stating that the read request has been successfully initiated, so no block is generated for the user thread. The kernel then waits for the data to be ready and then copies the data to the user thread, and when it is all done, the kernel sends a signal to the user thread that the read operation is complete (i.e., two phases of the task), and then the data can be used directly.

It is also said that in the asynchronous IO model, both phases of the IO operation do not block the user thread, both of which are automatically completed by the kernel and then send a signal to inform the user that the thread operation is complete. This is also different from the signal-driven model, in the signal-driven model, when the user thread receives a signal indicating that the data is ready, and then requires the user thread to invoke the IO function for the actual read and write operations (but the data needs to be copied from kernel to the user process), whereas in the asynchronous IO model, Receiving a signal indicates that the IO operation has been completed (that is, the two-step operation is completed), and no need to invoke the IO function in the user thread for actual read and write operations.

Note that asynchronous IO is the underlying support that requires the operating system, and in Java7, asynchronous IO is provided.

The first four IO models are actually synchronous IO, and only the last is the true asynchronous Io, because the 2nd phase of the IO operation causes the user thread to block, either the multiplexed IO or the signal-driven model, which means that theprocess of copying the data from the kernel will cause the user thread to block.

to understand the IO model with Demo :

I went shopping with my friends at the weekend, I was hungry at noon, we were ready to go to dinner.  

Generally go to a restaurant to eat, a total of two processes:

1, whether the meal is ready -- waiting for the data

2 , the end to the table, presented to the user -- copy data from the kernel to user space

Weekend people, eat need to queue, my girlfriend and I have the following options:

(1) My friend and I finish the meal, do not know when to do a good job, had to sit in the restaurant and so on, until the good, and then eat out before leaving.

Friends would like to go shopping with me, but do not know when the meal can be done, but with me in the restaurant and so on, and can not go shopping, until after eating dinner to shop, in the middle of waiting for the cooking time wasted. This is typical of blocking .

(2) My friends are not willing to wait in vain, and want to go shopping malls, but also worried about the rice good. So we stroll for a while, come back to ask the waiter meal good no, to go back and forth several times, the meal has not eaten is almost exhausted. This is non-blocking. Need constant questioning, are you ready?

(3) similar to the second scheme, the restaurant installed an electronic screen used to display the status of the order, so that my friends and I go shopping for a while, come back without having to ask the waiter, directly to see the electronic screen on it. So that everyone's meal is good, all directly look at the electronic screen can be, this is the typical IO multiplexing, such as SELECT, poll, Epoll (nginx).

(4) Friends do not want to shop, the restaurant is too noisy, go home and have a good rest. So we call takeout, make a phone call to order, and then my friends and I can have a good rest at home, the food is good delivery staff to send to the home. This is the typical asynchronous, just need to make a phone call, and then can do their own things, the meal is ready to send.

Summary:

for these types of IO model, a good understanding of understanding is actually very simple, refueling!



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Analysis of several IO models of "I/O Model" (i.)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.