使用Select的3个注意事项

#include <sys/select.h>

int select(int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout);

int pselect(int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, const struct timespec *timeout, const sigset_t *sigmask);

 

FD_CLR(int fd, fd_set *set);

FD_ISSET(int fd, fd_set *set);

FD_SET(int fd, fd_set *set);

FD_ZERO(fd_set *set);

注意事项:

1 每次调用前,重新设置所关心的描述符;

2 每次调用前,重新设置等待时间;

3 准备好的描述符定义为: read/write 描述符时不会阻塞 (socket 断开将可读 )

解释:

1 fd_set* 以指针形式传入, select 将扫描所关心的描述符,改变其值后返回。用户判断此返回值得知哪些描述符可读 / 可写。这意味着:在 select 返回后,描述符集必须重新设置!

2 Timeout 以指针传入,就是说系统不保证其值不会被更改!

原文地址:https://www.cnblogs.com/zhenjing/p/2021770.html