一、select
概念
select使内核挂起进程,当一个/多个IO事件发生后, 控制权返还给应用程序,由应用程序进行IO的处理
参数
int select(int maxfd, fd_set *readset, fd_set *writeset,
fd_set *exceptset, const struct timeval *timeout);
返回值:为就绪数目,出错为-1,超时为0
maxfd:待测试的最大描述符+1
readset:读描述符集合
writeset:写描述符集合
exceptset:异常描述符集合
timeout:超时时间,为空一直等,为0立即返回,为正等待一段时间
fd_set集合函数
设置描述符集合相关函数,其中的每个元素都是二进制中的0或者1:a[maxfd-1], ..., a[1], a[0]
void FD_ZERO(fd_set *fdset); //所有元素设为0
void FD_SET(int fd, fd_set *fdset); //a[fd]设为1
void FD_CLR(int fd, fd_set *fdset); //a[fd]设为0
int FD_ISSET(int fd, fd_set *fdset); //a[fd]是否为1
用法
fd_set readmask; // 每次实际传入的(因为select会改变readmask,所以需要allreads还原)
fd_set allreads; // 用于还原readmask,每次将allreads赋给readmask
FD_ZERO(&allreads); // 清空allreads
FD_SET(0, &allreads); // 0加入监听
FD_SET(socket_fd,