Created
December 13, 2013 01:54
-
-
Save ndzj081221130/7938753 to your computer and use it in GitHub Desktop.
select
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> Written with [StackEdit](https://stackedit.io/). | |
> fd_set rfds; | |
> | |
> int rv; | |
> | |
> FD_ZERO(&rfds); | |
> | |
> FD_SET(event_fd,&rfds); | |
> | |
> do{ | |
> | |
> rv =select(event_fd+1,&rfds,NULL,NULL,NULL); | |
> | |
> }while(rv == -1 && errno == ENITR ); | |
然后调用 | |
> do{ | |
> | |
> rv = read(event_fd,&result,sizeof(result)); | |
> | |
> }while(rv == -1 && errno == ENITR); | |
int select(int maxfdp,fd_set *readfds,fd_set *writefds,fd_set *errorfds,struct timeval *timeout); | |
若将NULL以形参传入,即不传入时间结构,就是将select置于`阻塞状态`,一定等到监视文件描述符集合中某个文件描述符发生变化为止 | |
当有错误发生时则返回-1,错误原因存于errno, | |
此时参数readfds,writefds,exceptfds和timeout的值变成不可预测。 | |
1. EBADF 文件描述词为无效的或该文件已关闭 | |
2. EINTR 此调用被信号所中断 | |
3. EINVAL 参数n 为负值。 | |
4. ENOMEM 核心内存不足 | |
一般的程序都是阻塞在select函数执行的位置。应该不是oom啊,read之后才有判断返回值是否为0啊。detect_oom函数在返回0时,表示内存溢出。 | |
> ssize_t read(int fd , void * buf , size_t count) | |
从文件描述符中读取count个内容,并存储到buf中。 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment