basis

blocking

Block I/O, which means that the kernel’s I/O operation is not returned to user space until the operation is complete. Blocking refers to the execution state of a user-space program. The traditional IO model is synchronous blocking IO. In Java, sockets created by default are blocked.

synchronous

Synchronous IO is a way of INITIATING IO in user space and kernel space. Synchronous IO means that the user space thread is the active one initiating THE IO request and the kernel space is the passive one receiving the IO request. Asynchronous I/O, on the other hand, means that the system kernel is the active one initiating I/O requests, while the user-space thread is the passive one receiving them

Four kinds of IO

Blocking IO

Synchronizing non-blocking IO

Non-blocking I/O, which means that the program in user space can immediately return to user space to perform the user’s operation without waiting for the kernel I/O operation to complete. In other words, the kernel will immediately return a status value to the user at the same time. This IO model typically encounters two situations

  • In the absence of data in the kernel buffer, the system call returns immediately, returning a message indicating that the call failed
  • In the case of data in the kernel buffer, it blocks until the data is copied from the kernel buffer to the user process buffer. After the replication is complete, the system call returns success and the application process begins processing cached data in user space

To emphasize, the NIO (synchronous non-blocking IO) model is not the NIO (New IO) library for Java.

IO Multiplexing

The classic Reactor design pattern, sometimes referred to as asynchronous blocking IO, is used for Selector selectors in Java and epoll in Linux. How do you avoid the polling wait problem in the synchronous non-blocking IO model? This is the IO multiplexing model. In the IO multiplexing model, a new system call is introduced to query the ready state of IO. In Linux, the corresponding system call is the SELECT /epoll system call. With this system call, a process can monitor multiple file descriptors, and once a descriptor is ready (typically kernel buffer readable/writable), the kernel can return the ready state to the application. The application then makes the corresponding IO system call based on the ready state.

Currently support IO multiplexing system call, select, epoll and so on. The SELECT system call, which is supported on almost all operating systems, has good cross-platform features. Epoll was introduced in the Linux 2.6 kernel as a Linux enhanced version of the SELECT system call. In the IO multiplexing model, through the SELECT /epoll system call, a single application thread can poll hundreds or thousands of socket connections continuously, and return the corresponding read and write operation when one or more socket network connections have IO ready state.

The characteristics of

IO multiplexing model of IO involves two types of System Call (System Call), the other is SELECT /epoll (ready query), one is IO operations. The IO multiplexing model is built on the infrastructure of the operating system, that is, the kernel of the operating system must be able to provide multiway separated system calls select/epoll.

Asynchronous IO

Asynchronous IO refers to the way that user space and kernel space are called in reverse. Threads in user space become passive receivers, while kernel space becomes active callers. This is somewhat similar to the typical Java callback pattern, where user-space threads register various IO event callback functions with kernel space and the kernel calls them. In the asynchronous IO model, the user program does not need to block during the whole kernel data processing process, including the kernel reading data from the network physical device (nic) to the kernel buffer and copying the data from the kernel buffer to the user buffer.

Reference book: Netty, Redis, Zookeeper: High Concurrency