select系统调用:
select系统调用的用途是:在一段时间内,监听用户感兴趣的文件描述符上的可读,可写和异常等事件。
select系统调用的原型如下:
int select(int nfds,fd_set *readfds,fd_set *writefds,fd_set *exceptfds,struct timeval *timeout);
(1)nfds指定被监控的文件描述符的总数。
(2)readfds,writefds,exceptfds参数分别指向可读,可写和异常等时间对应的文件描述符集合。
(3)timeout参数用来设置select函数的超时时间.
select成功时返回就绪(可读,可写和异常)文件描述符的总数。若果在超时时间内没用任何文件描述符就绪,select将返回0,select失败时返回-1并设置errno。如果在 select等待时间内,程序接受到信号,则select立即-1,并设置errno为EINTER。
fd_set结构的定义如下:

#include 

 #define __FD_SETSIZE 1024 

 #include 

 #define FD_SETSIZE __FD_SETSIZE 

 typedef long int __fd_mask; 

 #undef  __NFDBITS 

 #dfine NFDBITS (8 * (int)sizeof(__fd_mask) 

 typedef sruct  

 { 

     #ifdef __use_xopen 

            __fd_mask fds_bits[__FD_SETSIZE /__NFDBITS]; 

      #define __FDS_BITS(set)    ((set)->fds_bits) 

     #else 

          __fd_mask    __fds_bits[__FD_SETSIZE/__NFDBITS] 

      #define __FDS_BITS(set)     ((set)->__fds_bits); 

     #endif 

 } fd_set;


用以上定义可见,fd_set结构体仅包含一个整形数组, 该数组的每个元素的每一位标记一个文件描述符。fd_set能容纳的文件描述符数量由FD_SETSIZE指定,这就限制了select能同时处理的文件描述符的总量。
poll系统调用:
poll系统调用和select类似,也是在指定时间内轮询一定数量的文件描述符,以测试其中是否有就绪者。poll的原型如下:

此处)折叠或打开



1. #include<poll.h>
2. int poll(struct pollfd *fds,nfds_t nfds,int timeout)

1)fds参数是一个pollfd结构类型的数组,它指定所有我们感兴趣的文件描述符上发生的可读,可写和异常等事件。pollfd结构体定义如下:


struct pollfd 

 { 

     int fd;//文件描述符 

     short events;//注册的事件 

     short revents;//实际发生的事件,由内核填充 

 }; 

 poll事件类型 POLLIN POLLOUT POLLERR POLLRDHUP等。


2)nfds参数指定被监听事件集合fds的大小。 


typedef unsigned long nfds_t;



3)timeout参数指定poll的超时值,单位是毫秒。timeout为-1,poll调用将永远阻塞,直到某个事件发生;当timeout为0时,poll 调用将立即返回。


返回值含义与select相同。


poll的一个简单应用: 
在/root/pro/fd1 /root/pro/fd2中分别有内容,
1234
5678
和
1122
3344


此处)折叠或打开



1. #include <stdio.h>
2. <stdlib.h>
3. <unistd.h>
4. <errno.h>
5. <sys/types.h>
6. <stropts.h>
7. <sys/poll.h>
8. <sys/stropts.h>
9. <string.h>
10. <sys/stat.h>
11. <fcntl.h>
12. <poll.h>
13.  
14.  #define BUFSIZE 1024
15.  
16. int main(int argc, char *argv[])
17. {
18. [BUFSIZE];
19. int bytes;
20. *pollfd;
21. int i=0;
22. int nummonitor=0;
23. int numready;
24. int errno;
25. *str;
26. if(argc != 3)
27. {
28. (stderr,"Usage:the argc num error\n");
29. exit(1);
30. }
31.  
32.  
33. if((pollfd = (struct pollfd*)calloc(2, sizeof(struct pollfd))) == NULL) //为struct pollfd分配空间
34. exit(1);
35. for(i; i<2; i++) //初始化化struct pollfd结构
36. {
37. = (char*)malloc(14*sizeof(char)); 
38. (str,"/root/pro/",14); 
39. (str,argv[i+1]);//注意,需要把路劲信息放到str中,否则opne("/root/pro/argv[i]",O_RDONLY)会出错
40. ("str=%s\n",str);//原因在于,在” “之中的argv[i]是字符串,不会用变量代替argv[i].
41. (pollfd+i)->fd = open(str,O_RDONLY);
42. if((pollfd+i)->fd >= 0)
43. (stderr, "open (pollfd+%d)->fd:%s\n", i, argv[i+1]); 
44. ++;
45. (pollfd+i)->events = POLLIN;//与下面的pollfd->revent对应
46. }
47. ("nummonitor=%d\n",nummonitor);
48.        
49. while(nummonitor > 0)
50. {
51. = poll(pollfd, 2, -1);
52. if ((numready == -1) && (errno == EINTR))
53. ; //被信号中断,继续等待
54. else if (numready == -1)
55. ; //poll真正错误,推出
56. ("numready=%d\n",numready);
57. for (i=0;nummonitor>0 && numready>0; i++)
58. {
59. if((pollfd+i)->revents & POLLIN)
60. {
61.                  
62. = read(pollfd[i].fd, buf, BUFSIZE);
63. --;
64. ("pollfd[%d]->fd read buf:\n%s \n", i, buf);
65. --;
66. }
67. }
68. }
69. for(i=0; i<nummonitor; i++)
70. (pollfd[i].fd);
71. (pollfd);
72. ;
73. }

运行结果如下:



epoll是linux特有的I/O复用函数。它在实现上和使用上与select,poll有很大区别。首先epoll用一组函数来完成任务,而不是单个函数,其次epoll把用户关心的文件描述符上的事件放在内核里的一个事件表中。


epoll需要使用一个额外的文件描述符,来唯一标识内核中的这个事件表。这个文件描述符使用如下函数创建:



此处)折叠或打开



1. #include<sys/epoll.h>
2. int epoll_create(int size);

size参数只是给内核一个提示,告诉它事件表有多大。


下面的函数用来操作epoll的内核事件表



此处)折叠或打开



1. #include<sys/epoll.h>
2. int epoll_ctl(int epfd,int op,int fd,struct epoll_event *event);

fd参数是要操作的文件描述符,op参数指定操作类型,event参数指定事件。


epoll_event定义如下:


struct epoll_event 

 { 

     __uint32_t events;    /*epoll事件*/ 

     epoll_data_t data;    //用户数据 

 }; 

 typedef union epoll_data 

 { 

     void *ptr; 

     int fd;//使用做多的 

     uint32_t u32; 

     uint64_t u64; 

 }epoll_data_t; 

 epoll_ctl成功时返回0,失败则返回-1并设置errno。


epoll系统调用的主要接口是epoll_wait函数,它在一段超时时间内等待一组文件描述符上的事件,其原型如下:



此处)折叠或打开


1. #icnldue<sys/epoll.h>
2. int epoll_event(int epfd,struct epoll_event *events,int maxevents,int

成功时返回就绪的文件描述符个数,失败时返回-1并设置errno的值。


maxevents参数指定最多监听多少个事件。


epoll_wait函数如果检测到事件,就将所有的就绪事件从内核事件表中复制到它的第二个参数events指向的数组中。这个数组只用于输出epoll检测到的就绪事件。