Linux io mode and select,poll,epoll interpretation

Source: Internet
Author: User
Tags epoll

Some concepts:

Virtual Space : is the space in which all the addresses that the process sees are composed. A process in the virtual space remapping all the physical addresses assigned to it.

The addressing return is related to the number of bits in the computer. It is divided into kernel space and user space. For 32-bit Linux systems, the highest 1G bytes are kernel space. The lowest 3G bytes are user space.

Process Blocking : This is an active behavior of the process itself. When a process enters a blocking state, it does not consume CPU resources.

file descriptor fd: nonnegative integer, is an index value. An open File record table that points to the process that the kernel maintains for each process.

Cache io: The default IO for most file systems is cache IO. The process is that the data is first copied to the operating system's kernel buffer (page cache), which is then copied to the application's address space.

Example:

When a read operation occurs, it undergoes two stages:

1 waiting for data preparation (waiting for the

2 copying data from the kernel to the process (copying the data from the kernel to the)

The LInux IO mode is divided into 5 types:

Blocking io (blocking IO)

Non-blocking IO (non-blocking io)

IO multiplexing (IO multiplexing)

Signal-driven IO (signal driven IO) (not commonly used)

Asynchronous IO (Asynchronous IO)

Blocking io (blocking IO):

In Linux, by default, all sockets are blocking. This is illustrated below:

There are two stages mentioned above, and in blocking IO, both phases are blocked.

Non-blocking IO (nonblocking io)

This is illustrated below:

Nonblocking io is characterized by the need for the user process to constantly proactively ask kernel data ready

IO multiplexing (IO multiplexing)

Also known as event driven IO, including Select,poll,epoll. A single process can handle the IO of multiple network connections at the same time.

The principle is that Select,poll,epoll will constantly poll the socket in charge, and when the socket has data arrives, it will notify the user process

This is illustrated below:

The user process invokes select and the entire process is block. At the same time kernel will monitor all select-responsible sockets, and when the data in any one socket is ready, select will return. Call read again at this time to copy the data from the kernel to the user process

Here we use Select,recvfrom two system call, which compares blocking IO with one system call (recvfrom) less efficient, and the advantage of using select is that it can handle multiple connection at the same time , rather than for a single connection to process faster.

Therefore, when the number of connections to be processed is not very high, Web server using Select/epoll does not necessarily perform better than the Web server using multi-threading + blocking IO, and may be more deferred.

In the IO multiplexing model, for each socket, it is generally set to non-blocking. In most cases, the entire user's process is blocked by the Select function

Asynchronous IO (Asynchronous IO)

The flow of asynchronous IO is as follows:

The user process initiates a read and returns immediately, to do other things.

After kernel receives read, no block is generated for the user process. Instead, it waits for the data to be ready, copies the data to the user's memory, and then sends a signal to the user process, telling the read to complete

Summarize:

The difference between blocking and non-blocking

Calling blocking IO will block the corresponding process until the operation is complete, and non-blocking IO returns immediately if the kernel also prepares the data

The difference between syschronous io and asynchronous IO

To determine the synchronous IO, the key point of asynchronous IO is that in real IO operations (such as Recvfrom), the process is not blocked.

In non-blocking, if the kernel data is not ready, this is not the block process. But when the data in the kernel is ready, Recvfrom will copy the data from kernel to the user's memory, when the process is block, so non-blocking,blocking are synchronous IO

Asynchronous Io, when the process initiates an IO operation, returns immediately and then ignores the signal that the IO is received kernel sent. In this process, the process is completely free of block

Synchronous asynchronous Differentiation Point is: How the user process know the data OK (own active check know, or received signal learned); After the data is ready in kernel, Who is responsible for copying the data to the user's memory (own copy or kernel is responsible for copying)

Comparison diagram for each IO model:

This article is organized from the following ref:

ref:1190000003063859

Linux io mode and select,poll,epoll interpretation

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.