I/O multiplexing of network programming under Unix (1)

What is I/O multiplexing?

What we need is the capability to tell the kernel that we want to be notified if one or more I/O conditions are ready (i.e., input is ready to be read, or the descriptor is capable of taking more output). This capability is called I/O multiplexing and is provided by the select and poll functions. ——来自《Unix网络编程》第三卷

In many cases, using select or poll, the event response can be handed over to the underlying operating system to manage, and the operating system will notify us when an I/O event occurs.

When to use I/O multiplexing:

1、When a client is handling multiple descriptors (normally interactive input and a network socket), I/O multiplexing should be used. This is the scenario we described previously.

2、It is possible, but rare, for a client to handle multiple sockets at the same time. We will show an example of this using select in Section 16.5 in the context of a Web client.

3、If a TCP server handles both a listening socket and its connected sockets, I/O multiplexing is normally used.

4、If a server handles both TCP and UDP, I/O multiplexing is normally used.

5、If a server handles multiple services and perhaps multiple protocols, I/O multiplexing is normally used.

--From "Unix Network Programming", Volume 3

I/O model

For read, two processes are generally involved:

1. Waiting for the data to be ready 
2. Copying the data from the kernel to the process

The following discussion will be described in terms of the operations of these two phases.

There are a total of 5 models of I/O:

1. Blocking I/O

image

       The application process generates a system call. If the kernel has no data ready, it will always wait and block. When the kernel data is ready, the data will be copied from the kernel to the application process. This copy process is also blocked.

2. Non-blocking I/O

image

       The reason why it is called non-blocking I/O means that when the application process generates a system call, it will return immediately regardless of whether the kernel's data is ready. Then, the call is initiated again, which is a polling process. When the kernel data is ready, it can respond normally. This process is non-blocking. When the process of copying data from the kernel to the application process is still blocked, it should be ensured that the data is complete and consistent.

3. I/O multiplexing

image

      With I/O multiplexing, one or more system calls block on select or poll, rather than blocking on the actual call. When the kernel has data ready, it will notify select or poll. Next, it will initiate the real system call, which is the recvfrom in the picture. After that, the data will be copied to the application process normally. It is worth noting that I/O multiplexing generates two system calls, one for select (poll) and one for recvfrom. Therefore, if the process only processes a single descriptor (descriptor), using I/O multiplexing will not only have a good effect, but also have additional system overhead. Therefore, I/O multiplexing is generally used to process multiple In the case of descriptors.

4. Signal-driven I/O

image

     We can use signals to drive I/O. When a descriptor is ready, the kernel will generate a signal to notify the application process. The signal-driven model is different from the above three. For the application process, it is in a notified state while waiting to receive data. This process is equivalent to an asynchronous operation. However, for the process of copying data from the kernel to the application process, the application process is still in a blocked state.

5. Asynchronous I/O

image

    In the signal-driven I/O model, waiting for the kernel data preparation phase is an asynchronous process, while the data copy phase is blocking, that is, synchronous. But for the asynchronous I/O model, both phases are asynchronous. That is to say, when the reference process generates an aio_read, it will continue to perform other operations, and the whole process will not cause any blocking.

“We call aio_read (the POSIX asynchronous I/O functions begin with aio_ or lio_) and pass the kernel the descriptor, buffer pointer, buffer size (the same three arguments for read), file offset (similar to lseek), and how to notify us when the entire operation is complete.” ——来自《Unix网络编程》第三卷

Comparison summary of 5 I/O models:

image

 

Summarize:

   In this article, I mainly introduce what is I/O multiplexing, the application scenarios of I/O multiplexing, and the introduction of five major I/O models. In the next blog post, I will focus on the principle, practical application and code implementation of select and poll in the Unix environment.

Guess you like

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