select-poll和epoll多路复用(事件驱动)技术
linux内核集成了select,poll和epoll三种NIO多路复用技术(多路复用使用事件驱动模型作为编程范式),多路复用技术可以一个进程同时监视多个文件描述符,在大并发条件下可以提高线程利用率,减少线程资源浪费。
select
原理解析
函数解析
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include <sys/select.h> #include <sys/time.h> #include <sys/types.h> #include <unistd.h> int select (int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout) ;void FD_CLR (int fd, fd_set *set ) ;int FD_ISSET (int fd, fd_set *set ) ;void FD_SET (int fd, fd_set *set ) ;void FD_ZERO (fd_set *set ) ;#include <sys/select.h> int pselect (int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, const struct timespec *timeout, const sigset_t *sigmask) ;
实际运用
poll
原理解析
函数解析
1 2 3 4 5 6 7 8 9 10 #include <poll.h> int poll (struct pollfd *fds, nfds_t nfds, int timeout) ;#define _GNU_SOURCE #include <signal.h> #include <poll.h> int ppoll (struct pollfd *fds, nfds_t nfds, const struct timespec *tmo_p, const sigset_t *sigmask) ;
实际运用
epoll
原理解析
函数解析
1 2 3 4 5 6 #include <sys/epoll.h> int epoll_create (int size) ;int epoll_create1 (int flags) ;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) ;int epoll_pwait (int epfd, struct epoll_event *events, int maxevents, int timeout, const sigset_t *sigmask) ;
实际运用