Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
534 views
in Technique[技术] by (71.8m points)

关于select epoll底层原理问题

selectimage.png
如上图所说select poll是轮训方式来查看所有监听的socket,这里的意思是说调用了select poll后就死循环轮训所有要监听的socket集吗,那这样的话不是很蠢效率很低吗?这里epoll是说是用回调方式来检测就绪事件,意思是说epoll调用以后进程阻塞也也不做轮训 当某个socket有数据时通知调用epoll的进程唤醒吗?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

对,是死循环。以 select 为例,源码的关键部分是:

for (;;) {
    unsigned long *rinp, *routp, *rexp, *inp, *outp, *exp;

    inp = fds->in; outp = fds->out; exp = fds->ex;
    rinp = fds->res_in; routp = fds->res_out; rexp = fds->res_ex;

    for (i = 0; i < n; ++rinp, ++routp, ++rexp) {
        // 省略代码
    }
    
    // 省略代码
    
    if (retval || timed_out || signal_pending(current)) {
        break;
    }
        
    if (table.error) {
        retval = table.error;
        break;
    }
}

所以说 epoll 效率比 select 高啊。

P.S. 其实效率低主要倒不是因为死循环,而是频繁的内核态和用户态切换。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...