What does the Linux IO multiplexing mean?

Source: Internet
Author: User
Tags epoll

Written in front: source collation and knowledge, the original link to http://www.zhihu.com/question/32163005/answer/55772739, Lo Zhiyu
Thank the author again ~ ~

Suppose you are an air tube at an airport, you need to manage all the routes to your airport, including inbound, outbound, some flights need to be on the tarmac, some flights need to go to the boarding gate to pick up passengers.

What would you do?

So here's the question:
Soon you will find the empty tube tower inside a bunch of air-traffic control, a little busy, the new air operator has not been squeezed in.
Air control between the need for coordination, the room is 1, 2 people when it is OK, the number of people after, basically become a vegetable market.
Air-control staff often need to update some common things, such as take-off display, such as the next one hours after the departure schedule, finally you will be surprised to find that everyone's time finally spent on the looting of these resources.

The reality of our air tube at the same time tube dozens of aircraft commonplace things, how they do?
They use this thing.

   ![这里写图片描述](https://pic2.zhimg.com/583d5ba3cee12e78befa8e2b749f4269_b.jpg)

This thing is called flight progress strip. Each block represents a flight, different slots represent different states, and then an air operator can manage a set of such blocks (a set of flights), and his job is to put the corresponding blocks into different trough when the flight information is updated.

This thing has not been eliminated now oh, just into the electronic.

Do you feel that a lot of efficiency is high, an empty tube tower can be dispatched to the route can be the previous method several times to dozens of times.

If you take each route as a sock (I/O stream), the empty tube is treated as your server sock management code.

The first approach is the most traditional multi-process concurrency model (each incoming new I/O stream is assigned a new process management. )
The second approach is I/O multiplexing (a single thread that records the state of each I/O stream (sock) to manage multiple I/O streams at the same time. )

In fact, the "I/O multiplexing" is probably the reason why this concept is so difficult to understand in Chinese. The so-called I/O multiplexing is actually called I/O multiplexing in English. If you search for multiplexing, it will basically show this graph:


So most people directly think of "a network cable, multiple sock reuse" this concept, including the above several answers, in fact, no matter you use multi-process or I/O multiplexing, network cable is only a good felling. Multiple sock multiplexing A network cable This function is implemented in the kernel + driver layer.

Important thing to say again: I/O multiplexing this multiplexing refers to the fact that a single thread tracks each sock (I/O stream) state (corresponding to the fight progress strip slot inside the empty tube tower) to manage multiple I /o stream. The reason for inventing it is to increase the throughput capacity of the server as much as possible.

It sounds a mouthful, look at a picture to understand.


In the same thread, through the switch mode, to transmit multiple I/O streams at the same time, (the person who learned EE can now stand out of righteousness is said to say this is called "Division Multiplexing").

What, you have not understood "a request came, Nginx use Epoll receive request process is how", look at this diagram to understand. As a reminder, Ngnix will have a lot of links coming in, Epoll will watch them all, and then, like the switch, who has the data to dial to WHO, and then call the corresponding code processing.

------------------------------------------
With this basic concept in view, the rest is well explained.

Select, poll, Epoll are all specific implementations of I/O multiplexing, the reason why there are these three ghosts exist, in fact, they appear to have a sequence.

When the concept of I/O multiplexing is raised, select is the first implementation (around 1983 in BSD).

When select is implemented, many problems are soon exposed.
Select modifies the array of arguments passed in, which is very unfriendly for a function that needs to be called many times.
Select if any one sock (I/O stream) has data, select will only return, but will not tell you that there is data on the sock, so you can only find one by yourself, 10 sock may be OK, if tens of thousands of of the sock every time to find it again, This senseless expense is quite a feast for the sea.
Select can only monitor 1024 links, this is not related to the grass, Oh, Linux definition in the header file, see Fd_setsize.
Select is not thread safe, if you add a sock to the select, and then suddenly another thread discovers that the sock does not need to be retracted. Sorry, this select does not support, if you are insane to turn off this sock, select the standard behavior is. Uh.. Unpredictable, but this is written in the documentation of the OH.
"If a file descriptor being monitored by select () are closed in another thread, and the result is unspecified"
PA Not domineering

So after 14 (1997) A bunch of people realized poll, poll fixed a lot of problems with SELECT, such as
Poll removed the limit of 1024 links, so how many links it, master you happy good.
Poll from the design, no longer modify the incoming array, but this depends on your platform, so walk the lake, or careful for the wonderful.
Actually drag 14 so long is not the problem of efficiency, but the hardware of that era is too weak, a server processing more than 1000 links is simply God's existence, select for a long time to meet the needs.

But poll is still not thread-safe, which means that you can only process a set of I/O flows within a thread, no matter how powerful the server is. You can certainly do that much, but then you have a lot of problems with the process.

So 5 years later, in 2002, the Great god Davide Libenzi realized the Epoll.

Epoll can be said to be the latest implementation of I/O multiplexing, epoll fixed most of the poll and select issues, such as:
Epoll is now thread-safe.
Epoll now not only tell you the data in the sock group, but also tell you which sock have the data, you don't have to find it yourself.

Epoll that year patch, is still in, the following link can be seen:
/dev/epoll Home Page

Attach a domineering figure, look at the same performance as God (Test code is dead chain, if someone can dig the grave to find out, you can study the details of how to measure).


Horizontal dead Connections is the meaning of the link number, called the name is just its test tool called Deadcon. The vertical axis is the number of requests processed per second, and you can see that the number of Epoll requests per second is basically not decreasing as the links become more numerous. Poll and/dev/poll are miserable.

But Epoll has a fatal flaw. Only Linux support. For example BSD above the corresponding implementation is kqueue.

In fact, some domestic well-known manufacturers to Epoll from the Android inside cut off this brain remnants of the matter I will take the initiative to tell you. What, you say no one uses the Android server, you are looking down on peer software.

and Ngnix's design principle, it will use the target platform above the most efficient I/O multiplexing model, so it will have this setting. Under normal circumstances, if possible, try to use Epoll/kqueue.

Detailed here:
Connection processing methods

PS: Above all these comparative analysis, are built under large concurrency, if you have too few concurrent numbers, with which, in fact, there is no difference. If the transcoding server like in the Open Gate Data center is tens of thousands of hundreds of thousands of concurrent, don't epoll I can go directly to the wall.

Lo Zhiyu
Links: http://www.zhihu.com/question/32163005/answer/55772739
Source: Know
Copyright belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please specify the source.

What does the Linux IO multiplexing mean?

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.