3.3I/O复用模型
前面也已经说了:在Linux下对文件的操作是利用文件描述符(file descriptor)来实现的。
在Linux下它是这样子实现I/O复用模型的:
- 调用
select/poll/epoll/pselect
其中一个函数,传入多个文件描述符,如果有一个文件描述符就绪,则返回,否则阻塞直到超时。
比如poll()
函数是这样子的:int poll(struct pollfd *fds,nfds_t nfds, int timeout);
其中 pollfd
结构定义如下:
struct pollfd {
int fd; /* 文件描述符 */
short events; /* 等待的事件 */
short revents; /* 实际发生了的事件 */
};
https://www.zhihu.com/question/29005375