Description of five I/O models

Source: Internet
Author: User

Before we say the I/O model, let's talk about the concepts of synchronous, asynchronous, blocking, non-blocking, which are four ways to call:

Synchronization: When a function call is made, the call does not return until the result is obtained, and the popular point is that one thing must be done to do the next thing.

Async: Asynchronous and synchronous are exactly relative, when an asynchronous procedure call is made, the caller cannot get the result immediately, and when the asynchronous call is complete, the caller is notified by state, notification, and callback.

Blocking: A blocking call means that the current thread is suspended until the call result is returned (the thread goes into a non-executable state where the CPU does not allocate a time slice to the thread, that is, the thread pauses), and the function returns only after the result is obtained.

Non-blocking: means that our call does not block the current thread until the result is immediately available, and returns immediately.

In these cases, the popular point is the following:

Sync: That's what I call a feature that doesn't end before I do nothing, death results;

Async: That is, I call a function, do not need to know the result of the function, the function has a result notify me (callback notification);

Blocking: is called a function that does not return the function until the result is received or the data has been accepted;

Non-blocking: is called a function, the function returns immediately, through the select notification caller;

The difference between synchronous IO and asynchronous IO: When the data is copied, the process is blocked;

The difference between blocking IO and non-blocking IO: Whether the application's call returns immediately.


Let's sum it up:

Synchronization is when a process initiates a function (Task) call, waits until the function (Task) completes, and the process continues to be active. The asynchronous case is when a process initiates a function (Task) call, does not wait for the function to return, but continues to execute, when the function returns, through the state, notification, events and other ways to notify the process task completion. Blocking is when a request is not satisfied, the process is suspended, and non-blocking does not block the current process, that is, blocking and non-blocking is for a process or thread, whereas synchronous and asynchronous are targeted at functional functions.


With the above understanding, let's look at the following five types of I/O models:

1. Blocking I/O model: The process blocks until the data copy is complete. The application process invokes an I/O function, causing the application process to block, waiting for the data to be ready, and waiting until the data is ready, until the data is ready to be copied from the kernel to the user space, and the I/O function returns a success indication.

Let's take a look at the blocking I/O model diagram, where the process of waiting for data and replicating data in the kernel occurs when the Recvfrom function is called:

650) this.width=650; "Title=" r3vvg$7 (p5u_xt%w]) 59u4f.png "src=" http://s3.51cto.com/wyfs02/M00/80/7E/ Wkiol1ddbpjh2c6faaphdj_lbpy354.png "alt=" Wkiol1ddbpjh2c6faaphdj_lbpy354.png "/>

When the Recvfrom function is called, the system first checks to see if the data is ready, and if the data is not ready, then the system is in a wait state, and when the data is ready, the data is copied from the system buffer to the user space, and then the function is returned. In a socket application, when the Recvfrom function is called, the data is not necessarily present in the user space, and the Recvfrom function is in a wait state.


2. Non-blocking I/O Model: non-blocking I/O calls the IO function repeatedly through the process (multiple system calls and returns immediately), and the process is blocked during the copy of the data. We set a socket interface to non-blocking is to tell the kernel, when the requested O operation cannot be completed, do not sleep the process, but return an error. In this way, our IO function will constantly test whether the data is ready, and if not, continue with the test until the data is ready. In constant testing, it takes up a lot of CPU time.

Let's take a look at the non-blocking I/O model diagram:

650) this.width=650; "title=" kylx8l%8~ () 76w[zvi (l[[w.png "src=" http://s5.51cto.com/wyfs02/M01/80/7E/ Wkiol1dddqvh3qn1aapfncfky5q133.png "alt=" Wkiol1dddqvh3qn1aapfncfky5q133.png "/>

Setting the socket to non-blocking mode is the notification kernel: When invoking the socket API function, do not let the thread sleep, but let the function return immediately, when returned, the function returns an error code. As shown, when a non-blocking socket calls the Recvfrom function multiple times, the kernel kernel data is not ready for the first three calls to the function, and the function immediately returns the Ewouldblock error code. When Recvfrom is called for the fourth time, the data is ready to be copied into the application's buffer, and the Recvfrom function returns a successful indication that the application is starting to process the data.

3.I/O multiplexing Model: Mainly Select or poll, two calls to a port, two return, than blocking I/O and no advantage, the key is to enable simultaneous monitoring of multiple IO ports. The I/O multiplexing model uses the Select,poll,epoll function, which also causes the process to block, but unlike blocking I/O, these two functions can block multiple I/O operations at the same time. I/O functions can be detected at the same time for multiple read operations, multiple write operations, and I/O operation functions are not actually invoked until there is data readable or writable.

Let's take a look at the I/O multiplexing Model diagram:

650) this.width=650; "title=" V9goebdz[ufivzk0z{{khev.png "src=" http://s1.51cto.com/wyfs02/M02/80/80/ Wkiom1ddeosbwjmvaapefrtwoc4365.png "alt=" Wkiom1ddeosbwjmvaapefrtwoc4365.png "/>

As you can see, we block the select call, waiting for the data treasure socket to become readable. When select returns the socket readable condition, we call Recvfrom to copy the read datagram to the application buffer area.

4. Signal-driven I/O Model: Two calls, two returns. First we allow the socket interface to drive the signal-driven I/O and install a signal processing function, and the process continues to run without blocking. When the data is ready, the process receives a sigio signal that can be called by the I/O operation function in the signal processing function to process the data. The model diagram is as follows:

650) this.width=650; "title=" 9bw9j6{xalw@yfks0b48o~q.png "src=" http://s5.51cto.com/wyfs02/M02/80/80/ Wkiom1ddezzjwjmpaapjsl407bg477.png "alt=" Wkiom1ddezzjwjmpaapjsl407bg477.png "/>

5. Asynchronous I/O Model: When data is copied, the process does not need to be blocked. When an asynchronous invocation process occurs, the caller cannot get the result immediately, and the part that actually handles the call notifies the caller of the input and output operation through state, notification, and callback after completion. Here we call the Aio_read function to pass the descriptor to the kernel, buffer pointer, buffer size and file offset, and tell the kernel how to notify us when the whole operation is complete. The system call returns immediately, and our process is not blocked while waiting for I/O to complete. When the kernel copies the data to the buffer, the application process is notified. Here is the Model diagram:

650) this.width=650; "title=" 1%98c~vejzj5jw5p3b2g9%5.png "src=" http://s2.51cto.com/wyfs02/M01/80/7E/ Wkiol1ddfv-s4aktaand0px-t6w958.png "alt=" Wkiol1ddfv-s4aktaand0px-t6w958.png "/>


Let's summarize the comparison of the various I/O models:

As we can see from the comparison, the difference between the first four models is mainly concerned with phase one, because their first phase is the same: during the data from the kernel to the application buffer of the process, the process is blocked by the recvfrom call. Both phases of the asynchronous I/O model are processed.

650) this.width=650; "title=" @4ew_s2dv3b[7ieob6aa4r7.png "src=" http://s4.51cto.com/wyfs02/M00/80/80/ Wkiom1ddgd-iehhoaajjnpfblom085.png "alt=" Wkiom1ddgd-iehhoaajjnpfblom085.png "/>

Our top four models belong to the synchronous I/O model, and the fifth model is the asynchronous I/O model.








Description of five I/O models

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.