Analysis of the principle of Linux multiplexing mechanism--select/poll

Preface

There are five main IO models for Linux access devices, namely non-blocking IO model, blocking IO model, IO multiplexing model, signal-driven model and asynchronous IO model. This article mainly analyzes the IO multiplexing model. The IO multiplexing model under Linux is mainly implemented by select/poll/epoll and other mechanisms.
The IO multiplexing model can monitor multiple devices in a non-blocking manner. The specific model is shown in the figure below. As
Insert image description herecan be seen in the figure, select/poll can monitor multiple devices. As long as any one device meets the conditions, select/poll A specific response will be returned, otherwise it will sleep and wait. In short, let select/poll monitor the conditions that the user is interested in.

principle

select call analysis

Insert image description hereFrom the above figure, we can see the following points:
1. The final core operation of the select system call is processed in the do_select function. The original function refers to the fs/select.c file.
2. The do_select function mainly completes the following key operations

  • Call poll_initwait to initialize the poll_wqueues structure, including the initialization of the callback function, which is used to associate the driver function for callback processing;
  • Loop through the monitored file descriptors. When the monitoring conditions are met, call the *f_op->poll (that is, the member function unsigned
    int (*poll) (struct file *, struct poll_table_struct *);) function in the structure file_operations for processing. ;
  • When the monitoring conditions are not met, schedule_timeout is called for sleep processing, and wakes up when timeout occurs or is interrupted by other signals.

3. The conditions for the do_select function to exit the loop: the monitoring conditions are met, timeout occurs, and it is awakened by other signal interruptions.

The difference between select/poll

Poll is basically similar to select. They both use polling file descriptors for monitoring. When the file descriptor is large, the overhead will increase and lead to performance degradation. Select supports a maximum number of file descriptors by default of 1024, while poll does not. Specific restrictions.

Guess you like

Origin blog.csdn.net/liang_zhaocong/article/details/130660957