[Linux Network] The difference between IO multiplexing select and epoll, including sample code

IO multiplexing

Decoupled operation: server waits, server processes data.
When the client is inactive (no data transmission), the server does not wait and hangs directly.
The server only processes active client IO (with data transmission)

principle:

  • When the network card receives the data depends on the sender and the transmission path. This delay is usually very high, at the millisecond (ms) level.
  • Applications process data at the nanosecond (ns) level.
  • During the entire process, the kernel state is waiting for data and processing the protocol stack is a relatively slow process. For such a long time, the user-mode process has nothing to do.
  • Therefore, decoupling is needed - if a client is inactive, just suspend the client and let the server handle other active clients.

Technical realization:

  • Use a dedicated monitoring process to detect whether the client in the table is active
  • The server only handles active clients

select

  1. Give the kernel a file descriptor detection table (up to 1024), each of which represents the network connection file descriptor corresponding to the client.
  2. The kernel returns a file descriptor table of network IO, which is set to 1 when active (with data transmission) and 0 when inactive.
  3. The user will cycle the detection table n times [O(n)] to determine whether the corresponding IO is active, and then process the corresponding network IO

Related code

Insert image description here


epoll

  1. The kernel takes over and determines the active status of the corresponding client.
  2. The kernel returns all active file descriptor tables
  3. Users handle active network IO directly [O(1)]

Related code
Insert image description here

Guess you like

Origin blog.csdn.net/qq_43537701/article/details/133237688