What does the Linux IO multiplexing mean?

Source: Internet
Author: User
Tags epoll

Written in front: This article is organized in the knowledge, the original link is 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. Include inbound, outbound, some flights need to be on the tarmac waiting, some flights need to go to the boarding gate to pick up passengers.

What would you do?

So here's the question:
Very quickly you find that the empty tube tower inside a lot of air traffic control, a little busy, the new air operator has not been squeezed in.
There is a need for coordination between the air operator, the room inside is 1, 2 people when it is OK, the number of people later. It's basically a vegetable market.


Air-control officers often need to update some common things. For example, take-off display, for example, one hours after the departure schedule. At the end of the day, you'll be amazed to find that everyone's time is finally spent on these resources.

The reality of our air tube at the same time tube dozens of aircraft unusual 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 is able to manage a set of such blocks (a set of flights) while he works. is when there is a new update on the flight information. Put the corresponding blocks into different trough.

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

is not to think that the efficiency is very high, an empty tube tower can be dispatched to the route can be the previous method several times to dozens of times.

Suppose you take each route as a sock (I/O stream), and the empty pipe 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. )
Another approach is I/O multiplexing (a single thread that manages multiple I/O streams at the same time by recording the state of each I/O stream (sock).

)

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. Let's say you search for multiplexing, basically you'll figure out this:


So most people directly associate to "a network cable." Multiple sock Reuse "This concept contains several answers above. In fact, whether you use multi-process or I/O multiplexing. The 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 the status of each sock (I/O stream) through a record (the fight progress strip slot inside the corresponding empty tube tower) To manage multiple I/O streams at the same time. 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.


Inside the same thread, by dialing the switch way. To transfer multiple I/O streams at the same time. (people who have studied EE are now able to stand up and say that this is called "Time Division Multiplexing").

What, you haven't figured out "what is the process of using Epoll to receive requests when a request arrives?" Take a 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 switch. Whoever has the data will dial to whom. Then call the appropriate code processing.

------------------------------------------
After understanding the main concept, the rest is very well explained.

Select, poll, and Epoll are all detailed implementations of I/O multiplexing. The reason why these three ghosts exist is that they appear in order.

The concept of I/O multiplexing was raised later. Select is the first implementation (around 1983 implemented in BSD).

When select is implemented, very quickly exposes a lot of problems.


Select changes the array of parameters that are passed in. This is very unfriendly to a function that needs to be called very many times.
Select assumes that no matter what a sock (I/O stream) appears, select will only return, but will not tell you that there is data on the sock, so you can only find one by yourself, and 10 sock may be okay. If the sock of tens of thousands of were to be searched every time, the deadweight of the expense would be quite heroic.
Select can only monitor 1024 links. This has nothing to do with the grass. Linux is defined in the header file, see Fd_setsize.
Select is not thread safe, suppose you add a sock to select, and then suddenly another thread discovers it. Nima. This sock is not necessary. To take back. I am sorry. This select does not support, assuming you are insane to actually turn off this sock, select standard behavior is.

Uh.. Not pre-tested, this but written in the documentation of 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, for example
Poll removed the limit of 1024 links, so how many links it, master you happy good.
Poll from a design standpoint. No more changes to the incoming array, but this depends on your platform, so walk the lake. It's better to be careful.


In fact, it was not a matter of efficiency to drag the 14 so long, but the hardware of that era was too weak, and a server dealing with more than 1000 links was simply God's existence, and select had been meeting demand for a long time.

But poll is still not thread-safe, which means that no matter how powerful the server is. You can also only process a set of I/O flows within a thread. Of course you can cooperate with that process, 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 poll and select issues, for example:
Epoll is now thread-safe.


Epoll now not only tells you the sock group of data, but also tells you the details of which sock have data, you do not have to find it yourself.

Epoll the patch of the year, and now, the following links can be seen:
/dev/epoll Home Page

Put on a domineering picture. Look at the same performance as God (the test code is dead chain.) Suppose someone can dig a grave and find it. Be able to study how the details are measured).


The horizontal axis dead connections is the meaning of the link number. This name is just the test tool called Deadcon. The vertical axis is the number of requests processed per second that you can see. Epoll the number of requests processed per second basically does not decrease as the link becomes more numerous. Poll and/dev/poll are very dire.

But Epoll has a fatal flaw.

。 There is only Linux support.

For example BSD above the corresponding implementation is kqueue.

In fact, some well-known domestic manufacturers to Epoll from the Android inside cut off this kind of brain remnants of the matter I will take the initiative to tell you.

What, you say no one uses Android to do server. You look 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. Let's say you have too few concurrent numbers to use, in fact, there's no difference.

Assuming that the transcoding server in the Open Gate Data center is like a tens of thousands of hundreds of thousands of concurrency, it's not epoll I can go straight to the wall.

Lo Zhiyu
Links: http://www.zhihu.com/question/32163005/answer/55772739
Source: Know
Copyright belongs to all authors. 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.