引言
select
() 和 pselect
() 模型允许程序监控多个文件描述符 (fd),直到一个或多个文件描述符 (fd) “准备”进行 I/O 操作(比如,可读)。
select
() and pselect
() allow a program to monitor multiple file descriptors, waiting until one or more of the file descriptors become "ready" for some class of I/O operation (e.g., input possible). A file descriptor is considered ready if it is possible to perform the corresponding I/O operation (e.g., read
(2)) without blocking.
详细描述可直接在命令行输入:
man select
函数
/* According to POSIX.1-2001 */#include/* According to earlier standards */#include #include #include /* Access macros for `fd_set'. */void FD_SET(fd, fdsetp)void FD_CLR(fd, fdsetp)int FD_ISSET(fd, fdsetp)void FD_ZERO(fdsetp)int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout);int pselect(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, const struct timespec *timeout, const sigset_t *sigmask);
描述
- select() 的 timeout 是一个 struct timeval 类型 (seconds 和 microseconds);而 pselect() 的 timeout 是一个 struct timespec 类型 (seconds 和 nanoseconds)。
- select() 会更新 timeout 参数指出还剩下多少时间;pselect() 不会修改 timeout 参数。
- 如果 pselect() 不带 sigmask 参数调用,效果与 select() 是一样的。
- nfds 是这三个 fd_set 中的最大值加 1
- timeout 参数指明了 select() 阻塞的超时时间,如果 timeout 为 NULL (no timeout),select() 将无限期地阻塞下去。
多线程应用
如果一个被 select() 监控的文件描述符 (fd) 在另一个线程关闭 close(),其结果是未定义的。在一些 UNIX 系统中,select() 不阻塞并返回,通知系统该文件描述符处于准备状态 (is ready);而其他一些 UNIX 系统则在 select() 的 timeout 范围内响应为 I/O 操作已经执行。Linux 系统上,在另一个线程关闭文件描述符对 select() 不产生任何影响。In summary, any application that relies on a particular behavior in this scenario must be considered buggy.
超时
时间结构体定义在 <sys/time.h> 文件中
struct timeval { long tv_sec; /* seconds 秒 */ long tv_usec; /* microseconds 微秒 */};
与
struct timespec { long tv_sec; /* seconds 秒 */ long tv_nsec; /* nanoseconds 纳秒 */};
返回值
- 正常,> 0
- 超时,0
- 错误,-1
错误码
当调用失败返回 -1 时,errno 将会被赋值。
- EBADF fd_set 集中有错误的文件描述符(也许是文件描述符 (fd) 已经被关闭或遇到错误)
- EINTR 捕捉到信号。参考 signal(7)
- EINVAL nfds 是负值又或者是 timeout 值不正确
- ENOMEM 无法分配内存
例子
通用的调用方法类似于这样
#include#include #include #define max(a, b) ((a) < (b) ? (b) : (a))int main(int argc, char *argv[]){ fd_set rfds; struct timeval tv; int maxfd = 0; int retval; /* Do some initialization */ int fd = 0; for (;;) { FD_ZERO(&rfds); FD_SET(fd, &rfds); maxfd = max(fd, maxfd); /* Wait up to five seconds, or NULL (no timeout) */ tv.tv_sec = 5; tv.tv_usec = 0; retval = select(maxfd + 1, &rfds, NULL, NULL, &tv); if (retval < 0) { perror("select()"); exit(EXIT_FAILURE); } else if (retval == 0) { printf("No data within five seconds.\n"); continue; } if (FD_ISSET(fd, &rfds)) { /* fd is available now */ } } return 0;}
优雅地关闭文件描述符
前面章节讲到,如果在另一个线程关闭 close() 被 select() 监控的文件描述符 (fd),其结果是未定义的。在 Linux 系统上,甚至不会对 select() 产生反应。因此无法在 for (;;) 循环中得知 select() 监控的文件描述符的变化。
如何优雅地关闭 fd 并通知到 select() 呢?
- 使用信号 因为当捕捉到信号时,select() 返回 EINTR 值,可以利用这种机制在文件描述符 (fd) 关闭时触发信号。
- 管道技术 产生一个管道,让 select() 一开始就监控管道的读端 pipe[0],如果文件描述符在另一个线程关闭,那么同时操作管道的写端 pipe[1],这样一来就能巧妙地通知 select() 了。
管道技术例子
例子源码
#include#include #include #include #include #include #ifndef max#define max(a, b) ((a) < (b) ? (b) : (a))#endifint fd = 0;int pipefd[2];void *thread_callback(void *pvoid){ /* Sleep five seconds */ sleep(5); close(fd); write(pipefd[1], "stop", 4);}int main(int argc, char *argv[]){ fd_set rfds; int retval; int maxfd = 0; int len = 0; uint8_t buf[64] = { 0x00 }; fd = open("/dev/pts/14", O_RDONLY); if (fd < 0) { perror("open /dev/pts/14"); exit(EXIT_FAILURE); } /* Create a pipe */ if (pipe(pipefd) == -1) { perror("pipe"); exit(EXIT_FAILURE); } pthread_t t; pthread_create(&t, NULL, thread_callback, NULL); for ( ; ; ) { FD_ZERO(&rfds); FD_SET(fd, &rfds); FD_SET(pipefd[0], &rfds); maxfd = max(fd, maxfd); maxfd = max(pipefd[0], maxfd); retval = select(maxfd + 1, &rfds, NULL, NULL, NULL); if (retval == -1) { perror("select()"); exit(EXIT_FAILURE); } /* No timeout, so retval will not be zero */ if (FD_ISSET(fd, &rfds)) { /* Unfortunately, it can't recv close() event here */ } if (FD_ISSET(pipefd[0], &rfds)) { len = read(pipefd[0], buf, sizeof(buf)); printf("pipe recv %s\n", buf); break; } } close(pipefd[0]); close(pipefd[1]); close(fd); exit(EXIT_SUCCESS);}
因为使用了 pthread 多线程库,因此编译时需要加上 -lpthread
选项:
gcc -o select select.c -lpthread
参考资料:
[1] man select
man pipe