This article mainly introduces the five IO models of Linux, nginx and Apache IO model differences. I’ll try to explain in colloquial terms.

Purely personal understanding, if there is something wrong, welcome to point out.

Against conclusion party

Let’s start with three names: select, poll and epoll

Efficiency: select < poll < epoll

Chestnut:

The students do the exercises and the teacher checks them

  • Plan 1: the teacher asks one by one — select
  • Option 2: Multiple teachers — Poll
  • Plan 3: When the students finish, they will answer the questions. — epoll

Obviously, plan 3 is more efficient.

5 IO models for Linux

The essence of network IO is to read socket. Socket is abstracted as stream in Linux system, and IO can be understood as the operation of convection.

For an IO access (for example, read), data is copied to the operating system kernel’s buffer before being copied from the operating system kernel’s buffer to the application’s address space.

That is, the process needs equal data.

Because of these two phases, Linux produces the following five network modes.

  • Blocking I/O (Blocking IO)
  • Nonblocking I/O (NONblocking IO)
  • I/O multiplexing
  • Signal Driven I/O
  • Asynchronous I/O (Asynchronous IO)

Chestnut scene, waiting for the delivery.

Blocking IO

Simply put, it’s death waiting.

I’m just standing at the front desk waiting for the delivery guy. I’m not leaving until he comes.

Advantages:

  • I can get the package the fastest.
  • The simplest and most effective.

Disadvantages:

  • It’s a waste of time.

Non-blocking I/O

Ask every once in a while. In between, you look down on your phone or walk around.

Advantages:

  • I have time for other things.

‘faults:

  • The delivery guy had to wait for me when he arrived, and he was usually the busiest person.

I/O multiplexing

Wouldn’t it be better to have a front desk to collect deliveries?

This front desk is reuse, one person doing N people’s work.

The front desk also has several working modes:

  • Select: The express came, I ask a person, who is the express.
  • Poll: I got a delivery and I added a receptionist to ask who it was.
  • Epoll: The number written on the express (callback), the express came to the little sister directly call me to pick up.

The first two may be faster if the number of connections is small. But with so many connections, the system can’t handle, or allocate, so many receptionists, and epoll’s advantage explodes.

This is what makes Nginx great.

Asynchronous I/O

Leave it at the front desk. You can grab it whenever you want.

Signal-driven IO

Everyone should register first, and then write down their phone number, and I’ll call you when it arrives.

conclusion

At the beginning of learning Nginx, I will definitely be confused by these models. For these basic knowledge, I care more about the principle, and then can reuse in the future programming. I think the idea of programming is more important than a lot of jargon.

Hope to help beginners, welcome big guys to give advice.