5 Types of network I/O models

Source: Internet
Author: User
Tags epoll

The objects and steps involved when IO occurs:

Object:

For a network IO, two objects are involved, one is a process and the other is the system kernel. When a read occurs, a step is taken

Steps:

    1. Preparation of data (i.e. wait)

    2. Copying data from the kernel to the application process


The differences between several I/O models are generally different in the above two stages


5 different I/O models are described below:

Blocking IO

Non-blocking IO

Multiplexed IO

Signal-driven IO

Asynchronous IO

of the above 5 io, the first 4 are synchronous IO, the 5th is asynchronous IO

1. Blocking IO

In Linux, the default sockets are blocked

The process of a typical read operation is probably this:

When the user process invokes the Recv/recvfrom system call, it checks to see if the kernel receive buffer is ready for the data, and for network IO many times the data has not arrived at the beginning, so the process blocks waiting. Until the kernel buffer data is ready (for UDP to receive a full datagram), the kernel copies the data to the user buffer and the kernel returns the result.    

Therefore, blocking Io is characterized by block in both phases of IO execution.

2. non-blocking IO

In Linux, sockets can be set as non-blocking by parameters

Similarly, the process of a typical blocking read operation is large:

When invoking the Recv/recvfrom system call, check that the kernel is ready for the data, if not directly return an error, does not block the wait data, the user can loop call Recv/recvfrom, so if the kernel has not prepared the data, The function will always return an error until the data is ready to copy the data to the user buffer and return

Therefore, the user process is actually the need to constantly actively ask the kernel data well no.

3. multiplexed IO

The system provides a select function and a epoll to implement a multiplexed input/output model, and Select/epoll can handle the IO of multiple network interfaces with a single process, the basic principle of which is that the two functions continually poll all sockets it is responsible for, when there is any When the socket receives the data, it returns to the notifying user, at which point the user calls Recv/recvfrom and copies the data from the kernel to the user memory. The user process is always blocked, but unlike blocking IO, this mode is blocked by the Select function . While blocking IO is blocking the recv function, in addition, the advantage of select is that multiple connections can be processed simultaneously

4. Signal-driven IO

When using signal-driven I/O, when the network socket is readable, the kernel notifies the application process by sending a sigio signal so that the application can begin reading the data. This is not asynchronous I/O, because the actual reading of the data into the application process cache is still the responsibility of the application itself.

5. Asynchronous IO

When the user process initiates a read operation, the kernel receives the read operation, first it returns immediately, so it does not block the user process, and then it waits for the data to be ready, then copies the data to the user's memory, when it is finished, it sends a signal to the user process, Tells the user process that the read operation is complete



Here are the two questions to consider:

1. The difference between blocking IO and non-blocking IO:

When the call blocks IO, the process blocks until the data is ready and the copy is complete before it is unblocked.

The non-blocking IO does not block the corresponding process, and even though the data is not ready, it will return immediately

2. The difference between synchronous IO and asynchronous IO:

The difference between the two is that the synchronous Io is blocked during IO operation, where IO operation is the real IO operation, that is, the Recv/recvfrom system call in non-blocking IO, when the kernel is ready for the data, Recvfrom will copy the data from the kernel to the user, during this time, The process is blocked.

Asynchronous IO is different, when the process initiates an IO operation, it returns directly, leaving all the work to the kernel to complete, knowing that the kernel sends a signal to the user process to tell it that IO is complete, and that the process is not blocked


Non-blocking IO and asynchronous IO:

In non-blocking IO, although the process is not blocked for most of the time I, it still requires the process to go to the active check, and when the data is ready to complete, it is also necessary for the process to proactively call Recvfrom to copy the data to the user memory. Asynchronous IO is completely different. It's like a user process handing over the entire IO operation to someone else (the kernel) and then sending a signal notification when the other person finishes. During this time, the user process does not need to check the status of the IO operation, nor does it need to actively copy the data.



Finish

This article is from the "Zero Egg" blog, please be sure to keep this source http://lingdandan.blog.51cto.com/10697032/1782267

5 Types of network 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.