select函数
select函数 select()函数允许进程指示内核等待多个事件中的任何一个发生,并只在有一个或多个事件发生 或经历一段指定时间后才唤醒它 #include <sys/select.h> #include <sys/time.h> // 返回值:若有就绪描述符,则返回就绪描述符数目;若超时则返回0,出错返回-1 int select(int maxfdp1, fd_set *readset, fd_set *writeset, fd_set *exceptset, const struct timeval *timeout); select的参数: maxfdp1: 指定待测试的描述符个数,它的值是待测试的最大描述符加1 readset、writeset、exceptset: 指定让内核测试读、写、异常条件的描述符 异常条件: 某个套接字的带外数据到达 某个已置为分组模式的伪终端存在可以从其主端读取的控制状态信息 fd_set *: select使用的描述符集 系统提供了4个宏对描述符集进行操作: #include <sys/select.h> #include <sys/time.h> void FD_SET(int fd, fd_set *fdset); // 设置文件描述符集fdset中对应于文件描述符fd的位(设置为1) void FD_CLR(int fd, fd