I/O multiplexing:
Select:
Function prototype and use:
#include <sys/select.h> #include <sys/time.h> int select(int maxfdp1, fd_set *readset,fd_set *writeset,fd_set *exceptset,const struct timeval *timeout) void FD_ZERO(fd_set *fdset);// Empty Collection Void FD_SET (int fd, fd_set * fdset);//Add a given file descriptor to the collection Void FD_CLR (int fd, fd_set * fdset);//Deletes a given file descriptor from the collection Int FD_ISSET (int fd, fd_set * fdset);//Check whether the file descriptor specified in the set can be read or written
Disadvantages:
-
Every time you call select, you need to copy the fd set from user mode to kernel mode. It also needs to traverse all fds passed in the kernel, which is too expensive -
The number of file descriptors supported by select is too small. The default value is 1024, and the system kernel needs to be recompiled to increase the size
Poll:
Function prototype and use:
# include <poll.h> int poll ( struct pollfd * fds, unsigned int nfds, int timeout); /*Poll structure*/ struct pollfd { Int fd;/* file descriptor*/ Short events;/* waiting events*/ Short events;/* Actual events*/ };
Disadvantages:
-
Only the upper limit of the select descriptor is solved, but the efficiency problem is not solved.
Epoll:
Function prototype and use:
#include <sys/epoll.h> int epoll_create(int size); int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event); int epoll_wait(int epfd, struct epoll_event * events, int maxevents, int timeout); struct epoll_event { __uint32_t events; /* Epoll events */ epoll_data_t data; /* User data variable */ };
Disadvantages:
-
More complex to use
Simple summary:
|
|
|
|
---|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
reference material: