Linux select 机制深入分析
作为IO复用的实现方式,select是提高了抽象和batch处理的级别,不是传统方式那样阻塞在真正IO读写的系统调用上,而是阻塞在select系统调用上,等待我们关注的描述符就绪。当然现在更好的方式是epoll,比如Java中的NIO底层就是用的epoll。这篇文章只是为了搞懂select机制的原理,不看源码就不能说懂这些IO复用手法。也在面试过程中体会到了,不去实践就会发现知道的永远是皮毛。面试问题:select的最大描述符限制可以修改吗?(有待深入)
用户层API语法:
/* According to POSIX.1-2001 */
#include <sys/select.h>
/* According to earlier standards */
#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);
注:这里的API发生了变化(参见UNPv1 P127),timeout值是允许更新的,这在内核中有体现。
select系统调用的内核源码主要流程是:sys_select() -> core_sys_select() -> do_select() -> poll_select_copy_remaining。
可代码可以一目了然。
/*
* SYSCALL_DEFINE5宏的作用就是将其转成系统调用的常见形式,
* asmlinkage long sys_select(int n, fd_set __user *inp, fd_set __user *outp,fd_set __user *exp, struct timeval __user *tvp);
*/
SYSCALL_DEFINE5(select, int, n, fd_set __user *, inp, fd_set __user *, outp,
fd_