Introduction to select, poll, and epoll for multiplexing

1. select

1.1 Function Introduction

int select(int max_fd,fd_set *readfds,fd_set *writefds,fd_set *exceptfds,struct timeval *timeout);
①The first parameter: the maximum value of the file descriptor to be waited for plus 1;
②The second parameter: the set of readable file descriptors to be waited for;
③The third parameter: the writable file description to be waited for Character set;
④The fourth parameter: the abnormal file descriptor set to be waited for;
⑤The fifth parameter: a structure representing time, used to set the waiting time of select;

1.2 Principle

These three types of file descriptors (fd for short) will be generated when the client operates the server: writefds (write), readfds (read), exceptfds (abnormal), select will block and monitor these three types of file descriptors, and wait for data to be available Read, writable, abnormal or timeout will return; after returning, find the ready descriptor fd by traversing the entire array of fdset, and then perform the corresponding IO operation.

1.3 Advantages

①Support on almost all platforms, good cross-platform support;

1.4 Disadvantages

①The file descriptors are limited, and the default maximum is 1024;
②Every time select is called, the fd collection needs to be copied from the user state to the kernel state, and traversed again;
③The scanning of the socket is a linear scan, that is, polling is used The method is inefficient, and its performance decreases as the number of file descriptors increases.

Two, poll

2.1 Function introduction

int poll(struct pollfd *fds,nfds_t nfds,int timeout);
①The first parameter: a pointer to the 0th element of a structure array, each array element is a struct pollfd structure, which is used to specify the conditions for testing a given fd; ②The second parameter:
use To specify the number of elements in the first parameter array;
③The third parameter: specify the number of milliseconds to wait, regardless of whether the I/O is ready, poll() will return;

2.2 Advantages

① There is no maximum file descriptor limit, and fd is stored in a linked list;

2.3 Disadvantages

①The polling method is used to scan the entire disk, and the performance decreases as the number of file descriptors increases;
②Every time select is called, the fd collection needs to be copied from the user state to the kernel state, and traversed again.

Three, epoll

3.1 Function Introduction

(1) Create an epoll instance: int epoll_create(int size);
①The first parameter: specifies the number of file descriptors that you want the epoll instance to check. This parameter is not the upper limit; (
2) Modify the instance:
int epoll_ctl(int epfd,int op,int fd,struct epoll_event *ev);
①The first parameter: epoll_create( ) return value;
②The second parameter: used to specify the operation to be performed (add, modify, delete);
③The third parameter: specifies which file descriptor setting in the interest list to modify;
④ The fourth parameter: the pointer to the structure epoll_event;
(3) Event waiting:
int epoll_wait(int epfd,struct epoll_event *evlist,int maxevents,int timeout);
① The first parameter: the return value of epoll_create();
② The second parameter: the structure array pointed to returns the relevant ready The caller is responsible for applying for the file descriptor information and the space of the array evlist;
③The third parameter: specifies the number of elements contained in the evlist array;
④The fourth parameter: used to determine the blocking behavior of epoll_wait() (three a blocking behavior);

3.2 Principle

When the kernel buffer of the file descriptor is not empty, a readable signal is sent to notify, and when the write buffer is not full, a writable signal is sent to notify the mechanism.

3.3 Advantages

① There is no limit on the maximum file descriptor, the upper limit is the maximum number of file handles of the operating system (1G memory supports about 100,000 handles);
② It only needs to be copied once from the user state to the kernel state, and the time notification mechanism is used to trigger it;
③ Efficiency is improved And it is not affected by the number of file descriptors, and the callback notification replaces the polling method;
④The working mode is horizontal trigger and edge trigger, the efficiency of edge trigger is higher than that of horizontal trigger, and the system will not be flooded with a large number of ready file descriptors that do not care;
⑤Separate Perform the following operations: "add/maintain tasks to be detected", "block threads/processes";
⑥epoll manages the collection to be detected based on red-black tree;
⑦In epoll, shared memory is used between the kernel and users, and mmap is used () File-mapped memory speeds up message passing with kernel space, reducing overhead;

3.4 Disadvantages

①It can only work under Linux.
insert image description here

Guess you like

Origin blog.csdn.net/xll102500/article/details/129252543