int select(int nfds, fd_set *readfds, fd_set *writefds,
fd_set *exceptfds, struct timeval *timeout);
void FD_CLR(int fd, fd_set *set);//从指定的文件描述符集合中删除指定文件描述符fd
int FD_ISSET(int fd, fd_set *set);//判断文件描述符fd 是否存在于文件描述符集合set中
void FD_SET(int fd, fd_set *set);// 将文件描述符fd 放到 文件描述符集合set中
void FD_ZERO(fd_set *set); //清空一个文件描述符集合
nfds : 文件描述符数量,不过指的是 所包含的最大文件描述符 +1 的值
readfds:可读的 文件描述符集合
writefds:可写的 文件描述符集合
exceptfds:异常的文件描述符集合
timeout 超时设置,阻塞。如果不实现超时设置,那么该函数会死等
EINTR A signal was caught; see signal(7). 阻塞等待 可以被信号打断
EINVAL nfds is negative or exceeds the RLIMIT_NOFILE resource limit (see getrlimit(2)).
EINVAL the value contained within timeout is invalid.
ENOMEM unable to allocate memory for internal tables.
The time structures involved are defined in <sys/time.h> and look like
秒+微秒
struct timeval {
long tv_sec; /* seconds */
long tv_usec; /* microseconds */
};
基于select函数的特性,我们可以用它来实现一个安全的休眠
/* select定时器 */
void selectAlrm(int sec)
{
struct timeval tm;
tm.tv_sec = sec;
tm.tv_usec = 0;
select(0, NULL, NULL, NULL, &tm);
return ;
}
微秒级别的实现方法一致;