1 select
缺点:
(1)每次调用select,都需要把fd集合从用户态拷贝到内核态
(2)每次调用select,都需要在内核遍历传递进来的所有fd
(3)支持的文件描述符数量太小,默认是1024
2 poll
除了文件描述符比select大,其它都一样
3 epoll
(1)每个fd只会拷贝一次
(2)事先注册要文件描述符,一旦文件描述符就绪,内核采用类似callback回调机制,激活文件描述符
(3)支持文件描述符大
水平触发(LT):缺省方式,支持block和no-block
边缘触发(ET):只支持no-block,效率比LT高。内核通告有事件,若应用没有处理,则内核不会再通知
int select(int maxfd, fd_set *rd, fd_set *wd, fd_set *ed);
struct pollfd {
int fd;
short events; //等待的事件
short revents; //实际发生的事件
};
int poll(struct pollfd *fds, unsigned int nfds, int timeout)
struct epoll_event {
u32 events;
epoll_data_t data;//data.fd
};
typedef union epoll_dat {
void *ptr;
int fd;
__uint32_t u32;
__uint64_t u64;
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)
缺点:
(1)每次调用select,都需要把fd集合从用户态拷贝到内核态
(2)每次调用select,都需要在内核遍历传递进来的所有fd
(3)支持的文件描述符数量太小,默认是1024
2 poll
除了文件描述符比select大,其它都一样
3 epoll
(1)每个fd只会拷贝一次
(2)事先注册要文件描述符,一旦文件描述符就绪,内核采用类似callback回调机制,激活文件描述符
(3)支持文件描述符大
水平触发(LT):缺省方式,支持block和no-block
边缘触发(ET):只支持no-block,效率比LT高。内核通告有事件,若应用没有处理,则内核不会再通知
int select(int maxfd, fd_set *rd, fd_set *wd, fd_set *ed);
struct pollfd {
int fd;
short events; //等待的事件
short revents; //实际发生的事件
};
POLLIN
POLLOUT
int poll(struct pollfd *fds, unsigned int nfds, int timeout)
struct epoll_event {
u32 events;
epoll_data_t data;//data.fd
};
typedef union epoll_dat {
void *ptr;
int fd;
__uint32_t u32;
__uint64_t u64;
} epoll_data_t;
EPOLL_CTL_ADD
EPOLL_CTL_DEL
EPOLL_CTL_MOD
EPOLLIN
EPOLLOUT
EPOLLET
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)