- I/O Multiplexing
- Signal-Driven I/O
- Linux-specific epoll
I/O Multiplexing
File descriptors are examined through the select system call or the poll system call.
- select function
int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *errorfds, struct timeval *timeout);
The parameters respectively mean: the range of file descriptors to test (0 to fd-1), file descriptors that meet three different condition requirements, and timeout period. - poll function
int poll(struct pollfd fds[], nfds_t nfds, int timeout);
The parameters are: an array containing elements with file descriptors, states to be checked, returned states; the array length; and the timeout period.
The difference between the two is that the array size passed into poll is not limited by the size defined by fd_set. Poll’s events and revents are a bit more versatile. Select is used more widely. In newer versions, select’s performance has been optimized to be comparable to poll.
- epoll function The epoll function includes three APIs: epoll_create(), epoll_ctl(), and epoll_wait(). Among the three calls, epoll has the best performance and most effectiveness. However, it can only be used on Linux.
This is mainly because a server will have multiple socket connections, and if a thread were created for each socket, it would consume considerable resources. Therefore, epoll is needed to optimize performance.