一、select
- int select(int maxfdp1, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *tvptr); //成功则返回已准备好的文件描述符个数,超时则返回0,出错则返回-1
- int FD_ZERO(fd_set *fdset);
- int FD_SET(int fd, fd_set *fdset);
- int FD_CLR(int fd, fd_set *fdset);
- int FD_ISSET(int fd, fd_set *fdset);
1 #include <sys/select.h> 2 #include <unistd.h> 3 4 int main(int argc, char *argv[]) 5 { 6 fd_set readset, writeset; 7 int ret = 0; 8 FD_ZERO(&readset); 9 FD_ZERO(&writeset); 10 FD_SET(3, &readset); 11 FD_SET(5, &writeset); 12 13 ret = select(6, &readset, &writeset, NULL, NULL); 14 switch (ret) 15 { 16 case -1: //error 17 break; 18 case 0: //timeout 19 break; 20 default: //success 21 if(FD_ISSET(3, &readset)) 22 { 23 ;//read fd(3) 24 } 25 if(FD_ISSET(5, &writeset)) 26 { 27 ;//write fd(5) 28 } 29 break; 30 } 31 return 0; 32 }
二、poll
- int poll(struct pollfd fdarray[], nfds_t nfds, int timeout);
三、epoll