select 函数原型:
int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout);
最后一个超时时间,即等待指定的设备集可用的最长等待时间,如果timeout和值设置为0,select 会立即返回;如果设置为NULL,
select 会一直阻塞直到设备集可用。
如果给定一个指定一个不为0的超时时间,timeout是一个输入输出参数,即函数调用时不但会读取该值,同时还会改写它的值,
函数返回时,timeout的值会减去此次调用时系统流逝的时间。
比如,在超时时间到达之前并且设备集可用之前,出现了错误(可能是由于信号中断所致),select 返回后timeout的时间已经
不再是刚开始设置的值,它的值已经是减去select 调用时系统流逝的时间。
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<stdbool.h> #include<stdint.h> #include<errno.h> #include<assert.h> #include<unistd.h> #include<sys/select.h> #include<signal.h> #include<errno.h> #include<pthread.h> static int pfd[2]; void *input_proc(void *param) { char ch; while((ch = getchar()) != 'q'){ write(pfd[1], &ch, 1); } } void alarm_proc(int signo) { printf("sigalarm time %ld\n", time(NULL)); } int main(int argc, char *argv[]) { int ret; fd_set rset; pthread_t pid; struct timeval timeout; if (pipe(pfd) < 0){ printf("pipe failed!\n"); } timeout.tv_sec = 6; timeout.tv_usec = 0; signal(SIGALRM, alarm_proc); pthread_create(&pid, NULL, input_proc, NULL); alarm(1); for(;;){ FD_ZERO(&rset); FD_SET(pfd[0], &rset); printf("before select @time : %ld, timeout:%lu,%lu\n", time(NULL), timeout.tv_sec, timeout.tv_usec); ret = select(pfd[0]+ 1, &rset, NULL, NULL, &timeout); if (ret < 0) { if (errno == EINTR){ printf("EINTR occur @time : %ld , timeout:%lu,%lu\n", time(NULL), timeout.tv_sec, timeout.tv_usec); alarm(1); }else{ printf("errno:%d %s\n", errno, strerror(errno)); break; } } else if (ret == 0){ printf("TIMEOUT @time : %ld , timeout:%lu,%lu\n", time(NULL), timeout.tv_sec, timeout.tv_usec); break; } else{ printf("READY @time : %ld , timeout:%lu,%lu\n", time(NULL), timeout.tv_sec, timeout.tv_usec); break; } } alarm(0); close(pfd[0]); close(pfd[1]); return 0; }
编码 :gcc test.c -o test -lpthread
运行:./test
before select @time : 1402021644, timeout:6,0
sigalarm time 1402021645
EINTR occur @time : 1402021645 , timeout:5,28758
before select @time : 1402021645, timeout:5,28758
sigalarm time 1402021646
EINTR occur @time : 1402021646 , timeout:4,26835
before select @time : 1402021646, timeout:4,26835
sigalarm time 1402021647
EINTR occur @time : 1402021647 , timeout:3,26973
before select @time : 1402021647, timeout:3,26973
sigalarm time 1402021648
EINTR occur @time : 1402021648 , timeout:2,26494
before select @time : 1402021648, timeout:2,26494
sigalarm time 1402021649
EINTR occur @time : 1402021649 , timeout:1,26133
before select @time : 1402021649, timeout:1,26133
sigalarm time 1402021650
EINTR occur @time : 1402021650 , timeout:0,25331
before select @time : 1402021650, timeout:0,25331
TIMEOUT @time : 1402021650 , timeout:0,0
以上是程序超时退出,一直没有可读数据。下面是模拟在超时时间内使设备集可读,在超时时间内输入一个字符:
before select @time : 1402021790, timeout:6,0
sigalarm time 1402021791
EINTR occur @time : 1402021791 , timeout:5,27328
before select @time : 1402021791, timeout:5,27328
sigalarm time 1402021792
EINTR occur @time : 1402021792 , timeout:4,24340
before select @time : 1402021792, timeout:4,24340
bsigalarm time 1402021793
EINTR occur @time : 1402021793 , timeout:3,23934
before select @time : 1402021793, timeout:3,23934
READY @time : 1402021793 , timeout:2,819633
总之,对于select 调用我们要正确的处理中断引起的错误,所以一般情况下,不管是单次的读写,还是多次的读写,我们都要加循环调用,
对于一次的读或写,我们在select 之前设置一次超时即可,对于多次的读写,在每次调用select 之前都要重新设置一下timeout的值。