-
while
true {
-
for i in stream[]; {
-
if i has data
-
read until unavailable
-
}
-
}
-
while
true {
-
select(streams[])
-
for i in streams[] {
-
if i has data
-
read until unavailable
-
}
-
}
epoll_create 创建一个epoll对象,一般epollfd = epoll_create()
epoll_ctl (epoll_add/epoll_del的合体),往epoll对象中增加/删除某一个流的某一个事件
比如
epoll_ctl(epollfd, EPOLL_CTL_ADD, socket, EPOLLIN);//注册缓冲区非空事件,即有数据流入
epoll_ctl(epollfd, EPOLL_CTL_DEL, socket, EPOLLOUT);//注册缓冲区非满事件,即流可以被写入
epoll_wait(epollfd,…)等待直到注册的事件发生
(注:当对一个非阻塞流的读写发生缓冲区满或缓冲区空,write/read会返回-1,并设置errno=EAGAIN。而epoll只关心缓冲区非满和缓冲区非空事件)。
while true { active_stream[] = epoll_wait(epollfd) for i in active_stream[] { read or write till } }