Linux Network IO Model--blocking, non-blocking and synchronous, asynchronous

Source: Internet
Author: User

In recent days in learning Nginx when the Linux network IO model, here to talk about my own understanding, if there are errors please advise. This document refers to the book Richard Stevens "Unix®network programming Volume 1, third edition:the Sockets Networking", 6.2 "I/O M Odels ".

The Linux network IO request data is divided into two segments:

1. Data preparation

2. Copying data from the kernel to the process space

In fact, the difference between blocking, non-blocking, and synchronous and asynchronous is the difference between the two phases.

Synchronous and asynchronous attention is to the message communication mechanism

Blocking and non-blocking is concerned with the state of the program when it waits for the call result ( message, return value)

Blocking (blocking IO)

In Linux, sockets are blocked by default, and the approximate process is as follows:

  

, after the process initiates the recvform, the kernel carries on the first stage prepares the data, but many times the data starts does not arrive all, for the process is in the blocking state, after the data is ready, the kernel copies the data to the process space, after the copy completes kernel returns the result, the process ends the blocking state. That is, blocking IO Two stages are all in the blocking state.

Non-blocking (non-blocking IO)

In Linux we can set the socket to non-blocking, and when the IO request is not completed, instead of turning the process into a sleep state, it returns an error.

  

, when the user initiates read, if the data is not ready, kernel will immediately return an error and no need to wait. After the process receives the error (Ewouldblock), it knows that the data is not ready, and then continues to send the read request. Kernel will continue to receive system call requests from the process until the data is ready and the kernel copies the data to the user process space. This model requires the process to initiate a system call request without interruption, which can result in wasted CPU time.

IO multiplexing (IO multiplexing)

Io multiplexing What most people know is select,poll. The benefit of Select/epoll is that a single process can simultaneously handle multiple network connections of IO. The basic principle of the select/poll is that the function will constantly poll all sockets that are responsible, and when a socket has data arrives, notifies the user of the process. The approximate process is as follows:

  

When a user process invokes a select, the entire process is blocked. At the same time the kernel polls all select-responsible sockets, and when the data in any one socket is ready, select returns. This time the user process calls system call and copies the data from the kernel to the user process space.

This model doesn't have much advantage over blocking IO, and sometimes it's not even as blocking IO. The advantage of Select/poll is that multiple connection can be processed at the same time, not a single connection processing faster.

Asynchronous IO (asynchronous I/O Model)

  

After the user process initiates the system call invocation, you are ready to do something else. From kernel's point of view, when it receives a aio_read, it returns immediately, so it does not generate blocking for the user process. Kernel waits for the data to be ready, then copies the data to the user's memory, and after the copy is completed, Kernel sends a signal to the user process to tell it that the read operation is complete.

The difference between blocking and non-blocking:

Blocking will block the process until the operation is complete and non-blocking will return as soon as the kernel prepares the data.

Differences between synchronous synchronous I/O and asynchronous asynchronous I/O

Synchronous Io is blocked in the process of requesting data, and asynchronous IO does not block during the request data.

Various IO model comparisons:

  

, blocking,non-blocking,i/o multiplexing all belong to synchronous IO.

Linux Network IO Model--blocking, non-blocking and synchronous, asynchronous

Related Article

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.