Linux IO multiplexing mechanism (select, poll, epoll) and 5 IO models

I/O multiplexing : Monitors multiple file descriptors ( advantage ) , and notifies the program when a descriptor is ready . Select , poll , epoll are essentially synchronous IO (Linux) .

There are three multiplexing mechanisms in Linux:

1.select


Select : Three file descriptor sets are required, and events of interest can be added to the file descriptor set. Select multiplexing features are as follows:

              1. The number of file descriptors is limited to 1024        -- data structure: bitmap

            2. Each call needs to re- copy the 3 fd sets from user mode to kernel mode

           3. The kernel needs to traverse all fds , which is inefficient

2.poll


Poll features : 1. There is no maximum limit for file descriptors     -- data structure: linked list

                2. Each call needs to copy the fd set from user mode to kernel mode

               3. The kernel needs to traverse all fds , which is inefficient

3.epoll


epoll : 1. There is no maximum limit for file descriptors       -- data structure: red- black tree

      2. Just copy fd once to kernel mode

      3. The kernel only needs to judge whether the ready linked list is empty, it does not need to traverse all fds , which is efficient, and copies the ready fd to user space

Triggering methods of select, poll and epoll

  1. select and poll only support horizontal triggering
  2. Epoll supportstwotrigger modes, horizontal trigger and edge trigger            

                     Horizontal trigger : 1. Read operation

                                          The buffer is not empty, ready to read each time it returns

                                       2. Write operation

                                         The buffer is not full, each time it returns to write ready

                   Edge trigger :1.Read operation

                                        When the read buffer data increases, it returns to read ready      -- a new IO event arrives

                                     2. Write operation

                                        When the write buffer data is reduced, return to write ready      - a new IO event arrives

  

 Use scenarios of select, poll, and epoll : When the number of fds is small and they are all active, it may be more efficient to use select or poll , because epoll has multiple callback functions.


Five IO models of Linux :

1. Blocking IO : When the kernel has no data to read, the io call is blocked until the kernel has data, and the data is copied from the kernel to the user space, and the io call returns.

2. Non-blocking IO : When the kernel has no data to read, the io operation returns immediately. When there is data to read, the data is copied from the kernel to the user space before returning ( the copying process is still blocked ) . But requires the user process to poll the kernel until data is read.

3. IO multiplexing : Monitor multiple file descriptors and multiplex multiple IO blocks to one select block.

4. Signal-driven IO: The kernel sends a SIGIO signal when the descriptor is ready , and reads the data through the registered signal handler.

5. Asynchronous IO : After sending asynchronous IO , the IO operation returns immediately. When the kernel has data to read, the kernel automatically copies the data to the user space ( without blocking the user process ) , and sends a signal to the user process after the copy is completed.


Guess you like

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