Select, iocp, epoll, kqueue and various I/O reuse mechanisms

Source: Internet
Author: User
Http://blog.csdn.net/heyan1853/article/details/6457362

 

First, introduce several common I/O models and their differences, As follows:

    • Blocking I/O

    • Nonblocking I/O

    • I/O multiplexing (SelectAndPoll)

    • Signal driven I/O (Sigio)

    • Asynchronous I/O (the POSIXAIO _Functions)

Blocking I/O 
Block socket. Is an illustration of the call process:

The following examples will explain in detail. First, the application calls recvfrom () to transfer to the kernel. Note that there are two processes in the kernel: Wait for data and copy data from kernel to user. Recvfrom () is returned only after the last copy complete. This process is always blocked.

Nonblocking I/O: 
The call process of a non-blocking Socket opposite to blocking I/O is shown as follows:

As you can see, if you operate on it directly, it is a round robin .. Until there is data in the kernel buffer.

I/O multiplexing (select and poll) 
The most common I/O multiplexing model, select.

The SELECT statement is blocked first and is returned only when active sockets exist. AndBlocking I/OIn contrast, the SELECT statement has two system calls, but the SELECT statement can process multiple sockets.

Signal driven I/O (sigio) 
Only supported by UNIX systems. For more information, see relevant documents.

AndI/O multiplexing (select and poll)In contrast, it removes the need for select blocking and polling. When there is an active socket, it is processed by the registered handler.

Asynchronous I/O (the POSIX aio_functions) 
* Nix systems are rarely supported, while windows iocp is based on this model.

Fully asynchronous I/O multiplexing mechanism, because the above four models will at least be blocked when the kernel copy data to appliction. This model is used when the copyCompleteThe application is notified.Pure asynchronous. It seems that only WindowsComplete PortThis is the model, and the efficiency is also outstanding.

The following is a comparison of the above five models.

It can be seen that the more backward, the less blocking, the theoretical efficiency is also the best.

=============================================== ======================

The five models are clear, and the rest is to set select, epoll, iocp, and kqueue to the seat by number.

Select and iocp correspond to 3rd and 5th models respectively. What about epoll and kqueue? In fact, the SELECT statement belongs to the same model, but is more advanced. It can be viewed as having some features of the 4th models, such as the callback mechanism.

So why are epoll and kqueue more advanced than select?

The answer is, none of themRound Robin. Because they use callback instead. Think about it. When there are many sockets, each select () request will be scheduled by traversing fd_setsize sockets. No matter which socket is active, it will be traversed. This will waste a lot of CPU time. If you can register a callback function for a socket and automatically complete related operations when they are active, the polling is avoided. This is exactly what epoll and kqueue do.

Windows or * nix (iocp or kqueue/epoll )?

It is true that Windows's iocp is very good and seldom supports it at present.Asynchronous I/OBut due to the limitations of the system, large servers are still in UNIX. In addition, compared with iocp, kqueue/epoll has an additional blocking layer from the kernel copy data to the application layer, which cannot be countedAsynchronous I/O class.However, the small blocking layer is insignificant, and kqueue and epoll have already done well.

Provide consistent interfaces, IO Design Patterns

In fact, any model can be abstracted to provide consistent interfaces, such as Ace and libevent, which are generally cross-platform, in addition, they automatically choose the optimal I/O multiplexing mechanism, and users only need to call interfaces. Here we have to talk about two design models,ReactorAndProactor.There is a classicArticleHttp://www.artima.com/articles/io_design_patterns.htmlvalue for reading, libeventisReactorModel, provided by aceProactorModel. They are actually encapsulation of various I/O multiplexing mechanisms.

What is the I/O mechanism of the Java NiO package?

I once naively thought that Java NiO encapsulates iocp .. Now it can be determined that the current Java nature is the select () model, which can be checked/JRE/bin/NiO. dll. Why is the efficiency of the Java Server good .. I don't know, maybe it's a good design .. -_-.

=============================================== ======================

Summary:

    1. Only when iocp is asynchronous I/O, other mechanisms may be somewhat congested.
    2. Select is inefficient because it requires polling every time. However, inefficiency is relative, depending on the situation, and can also be improved through good design.
    3. Epoll, kqueue is in reacor mode, and iocp is in proactor mode.
    4. The Java NiO package is a select model ..

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.