Linux Network Programming Learning (12) ----- Five I/O Modes (Chapter 6)

1. What are the five I/O modes?

Blocking I/O, non-blocking I/O, I/O multiplexing, signal-driven I/O (SIGIO), asynchronous I/O

Generally speaking, the program has two steps for input operation, one is to wait for data to be read, and the other is to copy the data from the system kernel to the data set area of ​​the program.

For a socket data operation, the first step is to wait for the data to be uploaded from the network to the local. When the data packet arrives, the data will be copied from the network layer to the kernel's buffer, and the second step is to copy the data from the kernel. into the data area of ​​the program.

 

2. Introduction to five blocking modes

1) Blocking I/O mode

The default mode after the socket is established is the blocking I/O mode. For UDP, the data-ready flag is relatively simple: an entire datagram has been received or not received. For example, when a process calls the recvfrom() function, the data is not ready. At this time, the process will wait for the data to be ready, copy the data to the program data area, and then return normally. Therefore, the process calls recvfrom() to the function to return This time is blocked.

2) Non-blocking I/O

When a socket is set to non-blocking, it is equivalent to telling the kernel "When the I/O operation I requested cannot be completed immediately, and you want my process to sleep and wait, don't do this, please return an error immediately to I". That is, when recvfrom() is called, the data is not ready, and the kernel immediately returns an error to the process. Therefore, when non-blocking, it is necessary to continuously test whether there is data to read, that is, polling. The application keeps polling the kernel to check I. Whether the /O operation is ready, which will greatly waste CPU resources

3) I/O multiplexing

When using I/O multiplexing technology, call the select() or poll() function. When reading data, call select()/poll() in blocking mode first, that is, wait until the data is ready to return, and then Call the recvfrom() function to copy the data to the program buffer. Compared with the blocking mode, one more function is called, but the advantage of multiplexing is that select() can wait for multiple socket descriptors, as long as one is ready , the select() function can return.

The use cases of multiplexing are:

  • When a client needs to handle the input and output operations of multiple file descriptors at the same time (generally standard input and output and network sockets), I/O multiplexing technology will have the opportunity to be used

  • When the program needs to perform operations on multiple sockets at the same time
  • If a TCP server program handles both sockets that are listening for network connections and sockets that are already connected

  • If a server program uses both TCP and UDP protocols
  • If a server uses multiple services at the same time and each service may use a different protocol (such as inetd)

4) Signal-driven I/O mode

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325329906&siteId=291194637