Vernacular Nginx's IO model

This article mainly introduces the 5 IO models of Linux, and the IO models of nginx and apache have been different. I will try to explain in colloquial language.

It is purely personal understanding, please correct me if there is something wrong.

Against the Conclusion Party

Let me talk about three names first:
select, poll, epoll

Efficiency:
select <poll <epoll

chestnut:

The students do the questions and the teacher checks.
Option 1: The teacher asks one by one — select
Option 2: Multiple teachers — poll
Option 3: The student takes the initiative to answer when finished. — Epoll

Obviously, Option 3 is more efficient.

5 IO models of Linux

The essence of network IO is the reading of sockets. Sockets are abstracted as streams in Linux systems, and IO can be understood as operations on streams.

For an IO access (take read for example), the data will first be copied to the operating system kernel buffer, and then from the operating system kernel buffer to the address space of the application.

That is, the process needs to wait for data.

Formally because of these two stages, the Linux system has produced the following five network mode solutions.

  • Blocking I/O (blocking IO)
  • Non-blocking I/O (nonblocking IO)
  • I/O multiplexing (IO multiplexing)
  • Signal driven I/O (signal driven IO)
  • Asynchronous I/O (asynchronous IO)

Chestnut scene, waiting for express delivery.

Blocking IO

Simply put, it's just waiting.

I just stood at the front desk and waited for the courier.

advantage:

  • I can receive the package as soon as possible.
  • The most simple and effective.

Disadvantages:

  • It's a waste of time.

Non-blocking I/O

Ask every once in a while. In the middle, just bow your head to play with your phone or walk around.

advantage:

  • I have time to do other things.

'Disadvantages:

  • The courier brother has to wait for me when he arrives, and he is often the busiest person.

I/O multiplexing

Wouldn't it be much better if you join a front desk and collect express delivery?

This front desk is reuse, and one person does the work of N people.

There are also several working modes at the front desk:

  • select: The courier is here, and I personally ask who is the courier.
  • poll: Here is a courier, I will add a front desk to ask.
  • epoll: Write the phone number (callback) on the express. When the express comes, I will call you directly to get it.

If the number of connections is small, the first two may be faster.
However, if the number of connections is too large, the system will not be able to handle it, or it is difficult to allocate so many front desks, the advantage of epoll will increase instantly.
This is also the root of nginx's awesomeness.

Asynchronous I/O

Put it at the front desk, you can take it anytime you want.

Signal driven IO

Everyone comes to register first, and then write down the phone number. When the express arrives, I will find the number by name and notify you.

to sum up

When I first learned nginx, I would definitely be confused by these models. For these low-level knowledge, I care more about the principles, which can then be reused in future programming. I think programming thinking is more important than a lot of technical terms.

Hope to help beginners, welcome to come to give pointers.

Guess you like

Origin blog.csdn.net/happy_teemo/article/details/114745921