030 UNIX再学习 -- 函数 select、poll、epoll

                                          
                                                                                   
                                                                                
                                           

这部分是相当重要的一部分,之前在工作项目中有用到过,特意认真的看过。文章最后会把我项目用到部分的源码贴出。

再有值得纪念的一下,原创文章数终于赶上转载文章数了。说明我找到的学习方法是对了,一开始茫然不知所措时,学习是做加法,将看到的好的文章转载;但到了一定阶段,要懂得做减法了,将之前转载部分系统的分类总结;最后就是举一反三,将难题,转化成自己熟悉的问题解决。

文章开头总是很难的,要讲的东西太多,不知道从哪下笔。我就以下面参看这篇文章为基础开讲:

参看:select、poll、epoll之间的区别总结[整理]

一、相关概念

select、poll、epoll 都是 I/O 多路复用的机制。I/O 多路复用机制,可以监视多个描述符,一旦某个描述符就绪(一般是读就绪或者写就绪),能够通知程序进行相应的读写操作。但 select、poll、epoll本质上都是同步 I/O,因为它们都需要在读写事件就绪后自己负责进行读写,也就是说这个读写过程是阻塞的,而异步 I/O 则无需自己负责进行读写,异步 I/O 的实现会负责吧数据从内核拷贝到用户空间。

上面这段文字简单介绍了一下我们要讲到的这三个函数。其中有几个概念需要搞清楚。

(1)I/O 多路复用机制

I/O 多路复用,也称为 I/O 多路转接。APUE 上只有这样一句话。
为了使用这种技术,先构建一张我们感兴趣的描述符(通常都不止一个)的列表,然后调用一个函数,直到这些描述符中的一个已准备好进行 I/O 时,该函数才返回。poll、pselect 和 select 这 3 个函数使我们能够执行 I/O 多路转接。在从这些函数返回时,进程会被告知哪些描述符已准备好可以进行 I/O。
这是什么鬼,还是没有理解多路转接到底是怎么回事,然后我各种百度(谷歌虽好但要翻墙)也没查到合适的解释。
I/O 多路转接,英文是 I/O multiplexing,最后在知乎上看到还比较容易理解的解释。
I/O multiplexing 这里面的 multiplexing 指的其实是在单个线程通过记录跟踪每一个Socket (I/O流)的状态(对应空管塔里面的 Fight progress strip槽)来同时管理多个 I/O 流。 发明它的原因,是尽量多的提高服务器的吞吐能力。

(2)同步 I/O

稍后再讲

(3)异步 I/O

稍后再讲

(4)阻塞 I/O 与非阻塞 I/O

比如 read 和 write,通常 IO 操作都是阻塞 I/O 的,也就是说当你调用 read 时,如果没有数据收到,那么线程或者进程就会被挂起,直到收到数据。

这样,当服务器需要处理1000个连接的的时候,而且只有很少连接忙碌的,那么会需要1000个线程或进程来处理1000个连接,而1000个线程大部分是被阻塞起来的。由于CPU的核数或超线程数一般都不大,比如4,8,16,32,64,128,比如4个核要跑1000个线程,那么每个线程的时间槽非常短,而线程切换非常频繁。这样是有问题的:
1、线程是有内存开销的,1个线程可能需要512K(或2M)存放栈,那么1000个线程就要512M(或2G)内存。
2、线程的切换,或者说上下文切换是有CPU开销的,当大量时间花在上下文切换的时候,分配给真正的操作的CPU就要少很多。

那么,我们就要引入非阻塞I/O的概念,非阻塞IO很简单,通过 fcntl(POSIX)或 ioctl(Unix)设为非阻塞模式,这时,当你调用 read 时,如果有数据收到,就返回数据,如果没有数据收到,就立刻返回一个错误,如EWOULDBLOCK。这样是不会阻塞线程了,但是你还是要不断的轮询来读取或写入。


于是,我们需要引入IO多路复用的概念。多路复用是指使用一个线程来检查多个文件描述符(Socket)的就绪状态,比如调用select和poll函数,传入多个文件描述符,如果有一个文件描述符就绪,则返回,否则阻塞直到超时。得到就绪状态后进行真正的操作可以在同一个线程里执行,也可以启动线程执行(比如使用线程池)。


这样在处理1000个连接时,只需要1个线程监控就绪状态,对就绪的每个连接开一个线程处理就可以了,这样需要的线程数大大减少,减少了内存开销和上下文切换的CPU开销。

使用select函数的方式如下图所示:

二、函数 select


     
     
  1.    /* According to POSIX.1-2001 */
  2.        #include <sys/select.h>
  3.        /* According to earlier standards */
  4.        #include <sys/time.h>
  5.        #include <sys/types.h>
  6.        #include <unistd.h>
  7.        int select(int nfds, fd_set *readfds, fd_set *writefds,
  8.                   fd_set *exceptfds, struct timeval *timeout);
  9.        返回值:准备就绪的描述符数目:若超时,返回 0;若出错,返回 -1

1、参数解析

(1)参数 timeout它指定愿意等待的时间长度,单位为秒和微妙

时间函数,我们讲了好多次了。参看:C语言再学习 -- 时间函数
结构体 timeval 定义如下:

     
     
  1.       struct timeval {   
  2.                time_t      tv_sec;     /* seconds */   
  3.                suseconds_t tv_usec;    /* microseconds */   
  4.            };   
timeout 的取值有以下 3 中情况:
    timeout == NULL
        永远等待如果捕捉到一个信号则中断此无限期等待。当所指定的描述符中的已准备好或捕捉到一个信号则返回。如果捕捉到一个信号,则 select 返回 -1,errno 设置为 EINTR。 
    timeout->tv_sec == 0 && timeout->tv_usec == 0
        根本不等待。测试所有指定的描述符并立即返回。这是轮询系统找到多个描述符状态而不阻塞 select 函数的方法。
    timeout->tv_sec != 0 || timeout->tv_usec != 0
        等待指定的秒数和微妙数。当指定的描述符之一已准备好,或当指定的时间值已经超过时立即返回。如果在超时到期时还没有一个描述符准备好,则返回值是 0。(如果系统不提供微秒级的精度,则 timeout->tv_usec 值取整到最近的支持值)与第一种情况一样,这种等待可被捕捉到的信号中断。

(2)中间 3 个参数 readfds(读)、writefds(写) 和 exceptfds(异常) 是指向描述符集的指针。

这 3 个描述符集说明了我们关心的可读、可写或处于异常条件的描述符集合。每个描述符集存储在一个 fd_set 数据类型中。这个数据类型是由实现选择的,它可以为每一个可能的描述符保持以为。我们可以认为它只是一个很大的字节数组。


重点来了,对于 fd_set 数据类型,唯一可以进行的处理是:分配一个这种类型的变量,将这种类型的一个变量值赋给同类型的另一个变量,或对这种类型的变量使用以下 4 个函数中的一个。

     
     
  1.        void FD_CLR(int fd, fd_set *set)//
  2.        int  FD_ISSET(int fd, fd_set *set);
  3.        void FD_SET(int fd, fd_set *set);
  4.        void FD_ZERO(fd_set *set);
这些接口可实现为宏或函数。调用 FD_ZERO 将一个 fd_set 变量的所有位设置为 0要开启描述符集中的一位,可以调用 FD_SET调用 FD_CLR 可以清除一位。最后,可以调用 FD_ISSET 测试描述符集中的一个指定位是否已打开
在声明了一个描述符集之后,必须用 FD_ZERO 将这个描述符集置为 0,然后在其中设置我们关心的各个描述符的位。具体操作如下所示:

     
     
  1. fd_set rset;
  2. int fd;
  3. FD_ZERO (&rset);
  4. FD_SET (fd, &rset);
  5. FD_SET (STDIN_FILEND, &rset);
从 select 返回时,可以用 FD_ISSET 测试该集中的一个给定位是否仍处于打开状态。

     
     
  1. if (FD_ISSET (fd, &rset))
  2. {
  3.  ....
  4. }
select 的中间 3 个参数(指向描述符集的指针)中的任意一个(或全部)可以是空指针,这表示对相应条件并不关心。如果所有 3 个指针都是 NULL,则 select 提供了比 sleep 更精确的定时器。

(3)select 第一个参数 nfds 的意思是“最大文件描述符编号值加 1”

考虑所有 3 个描述符集,在 3 个描述符集中找出最大描述符编号值,然后加 1,这就是第一个参数值。
也可将第一个参数设置为 FD_SETSIZE,这是 <sys/select.h> 中的一个常量,它指定最大描述符数(经常是 1024),但是对大多数应用程序而言,此值太大了。

     
     
  1. root@ubuntu:/usr/include # grep "FD_SETSIZE" * -rn
  2. i386-linux-gnu/sys/select.h: 79: #define FD_SETSIZE  __FD_SETSIZE
  3. i386-linux-gnu/bits/typesizes.h: 63: #define __FD_SETSIZE  1024

2、返回值

select 有 3 个可能的返回值。

(1)返回值 -1表示出错。

这是可能发生的,例如,在所指定的描述符一个都没准备好时捕捉到一个信号。在此情况下,一个描述符集都不修改。
错误码有:

     
     
  1.        EBADF  An invalid file descriptor was given in one of the sets.  (Perhaps a file descriptor that was already closed, or one on which
  2.               an error has occurred.)
  3.        EINTR  A signal was caught; see signal(7).
  4.        EINVAL nfds is negative or the value contained within timeout is invalid.
  5.        ENOMEM unable to allocate memory for internal tables.

(2)返回值 0 表示没有描述符准备好。

若指定的描述符一个都没准备好,指定的时间就过去了,那么就会发生这种情况。此时,所有描述符集都会置 0.

(3)一个正返回值说明了已经准备好的描述符数。

该值是 3 个描述符集中已准备好的描述符数之和,所以如果同一描述符已准备好读和写,那么在返回值中会对其计两次数。在这种情况下,3 个描述符集中仍旧打开对的位对应于已准备好的描述符。

  对于“准备好”的含义要作一些更具体的说明。
    若对读集(readfds)中的一个描述符进行的 read 操作不会阻塞,则认为此描述符是准备好的。
    若对写集(writefds)中的一个描述符进行的 write 操作不会阻塞,则认为此描述符是准备好的。
    若对异常条件集(exceptfds)中的一个描述符有一个未决异常条件,则认为此描述符是准备好的。(现在,异常条件包括:网络链接上到达带外的数据,或者在处于数据包模式的伪终端上发生了某些条件)。
    对于读、写和异常条件,普通文件的文件描述符总是返回准备好。

3、示例说明


     
     
  1. 示例一:用来循环读取键盘输入
  2. #include <stdio.h>
  3. #include <sys/select.h>
  4. #include <time.h>
  5. #include <sys/types.h>
  6. #include <unistd.h>
  7. #include <sys/stat.h>
  8. #include <fcntl.h>
  9. #include <assert.h>
  10. int main (void)
  11. {
  12.   int keyboard;
  13.   int ret, i;
  14.   char c;
  15.  fd_set readfd;
  16.   struct timeval timeout;
  17.   //打开 /dev/tty 只读非阻塞
  18.  keyboard = open ( "/dev/tty", O_RDONLY | O_NONBLOCK);
  19.   //assert 断言宏
  20.  assert (keyboard > 0);
  21.   while ( 1)
  22.  {
  23.   timeout.tv_sec = 5;
  24.   timeout.tv_usec = 0;
  25.   FD_ZERO (&readfd);
  26.   FD_SET (keyboard, &readfd);
  27.   ret = select (keyboard + 1, &readfd, NULL, NULL, &timeout);
  28.    //select error when ret = -1
  29.    if (ret == -1)
  30.    perror ( "select error");
  31.  
  32.     //data coming when ret>0
  33.    else if (ret)
  34.   {
  35.     if (FD_ISSET (keyboard, &readfd))
  36.    {
  37.     i = read (keyboard, &c, 1);
  38.      if ( '\n' == c)
  39.       continue;
  40.      printf ( "the input is %c\n", c);
  41.     
  42.      if ( 'q' == c)
  43.        break
  44.    }
  45.   }
  46.     //time out when ret = 0
  47.    else if (ret == 0)
  48.     printf ( "time out\n");
  49.  }
  50. }
  51. 输出结果:
  52. 5秒内操作的话)
  53. s
  54. the input is s
  55. d
  56. the input is d
  57. f
  58. the input is f
  59. q
  60. the input is q
  61. (结束)
  62. 5秒内不操作的话)
  63. time out
  64. time out
  65. time out
  66. time out

     
     
  1. //示例二:通过select系统调用进行io多路切换,实现异步读取串口数据
  2. #include<stdio.h> 
  3. #include<stdlib.h> 
  4. #include<unistd.h> 
  5. #include<sys/types.h> 
  6. #include<sys/stat.h> 
  7. #include<sys/signal.h> 
  8. #include<fcntl.h> 
  9. #include<termios.h> 
  10. #include<errno.h> 
  11. #include <string.h>
  12.  
  13. #define FALSE -1 
  14. #define TRUE 0 
  15. #define flag 1 
  16. #define noflag 0 
  17.  
  18. int wait_flag = noflag; 
  19. int STOP = 0
  20. int res; 
  21.  
  22. int speed_arr[] = 
  23.   { B38400, B19200, B9600, B4800, B2400, B1200, B300, B38400, B19200, B9600, 
  24. B4800, B2400, B1200, B300, }; 
  25. int name_arr[] = 
  26.   { 38400, 19200, 9600, 4800, 2400, 1200, 300, 38400, 19200, 9600, 4800, 2400
  27. 1200, 300, }; 
  28. void 
  29. set_speed (int fd, int speed) 
  30.   int i; 
  31.   int status; 
  32.   struct termios Opt; 
  33.   tcgetattr (fd, &Opt); 
  34.   for (i = 0; i < sizeof (speed_arr) / sizeof ( int); i++) 
  35.     { 
  36.       if (speed == name_arr[i]) 
  37.     { 
  38.       tcflush (fd, TCIOFLUSH); 
  39.       cfsetispeed (&Opt, speed_arr[i]); 
  40.       cfsetospeed (&Opt, speed_arr[i]); 
  41.       status = tcsetattr (fd, TCSANOW, &Opt); 
  42.       if (status != 0
  43.         { 
  44.           perror ( "tcsetattr fd1"); 
  45.           return
  46.         } 
  47.       tcflush (fd, TCIOFLUSH); 
  48.     } 
  49.     } 
  50.  
  51. int 
  52. set_Parity (int fd, int databits, int stopbits, int parity) 
  53.   struct termios options; 
  54.   if (tcgetattr (fd, &options) != 0
  55.     { 
  56.       perror ( "SetupSerial 1"); 
  57.       return (FALSE); 
  58.     } 
  59.   options.c_cflag &= ~CSIZE; 
  60.   switch (databits) 
  61.     { 
  62.     case 7
  63.       options.c_cflag |= CS7; 
  64.       break
  65.     case 8
  66.       options.c_cflag |= CS8; 
  67.       break
  68.     default
  69.       fprintf ( stderr, "Unsupported data size\n"); 
  70.       return (FALSE); 
  71.     } 
  72.   switch (parity) 
  73.     { 
  74.     case 'n'
  75.     case 'N'
  76.       options.c_cflag &= ~PARENB;   /* Clear parity enable */ 
  77.       options.c_iflag &= ~INPCK;    /* Enable parity checking */ 
  78.       break
  79.     case 'o'
  80.     case 'O'
  81.       options.c_cflag |= (PARODD | PARENB); 
  82.       options.c_iflag |= INPCK; /* Disnable parity checking */ 
  83.       break
  84.     case 'e'
  85.     case 'E'
  86.       options.c_cflag |= PARENB;    /* Enable parity */ 
  87.       options.c_cflag &= ~PARODD; 
  88.       options.c_iflag |= INPCK; /* Disnable parity checking */ 
  89.       break
  90.     case 'S'
  91.     case 's':           /*as no parity */ 
  92.       options.c_cflag &= ~PARENB; 
  93.       options.c_cflag &= ~CSTOPB; 
  94.       break
  95.     default
  96.       fprintf ( stderr, "Unsupported parity\n"); 
  97.       return (FALSE); 
  98.     } 
  99.  
  100.   switch (stopbits) 
  101.     { 
  102.     case 1
  103.       options.c_cflag &= ~CSTOPB; 
  104.       break
  105.     case 2
  106.       options.c_cflag |= CSTOPB; 
  107.       break
  108.     default
  109.       fprintf ( stderr, "Unsupported stop bits\n"); 
  110.       return (FALSE); 
  111.     } 
  112.   /* Set input parity option */ 
  113.   if (parity != 'n'
  114.     options.c_iflag |= INPCK; 
  115.   tcflush (fd, TCIFLUSH); 
  116.   options.c_cc[VTIME] = 150
  117.   options.c_cc[VMIN] = 0;   /* Update the options and do it NOW */ 
  118.   if (tcsetattr (fd, TCSANOW, &options) != 0
  119.     { 
  120.       perror ( "SetupSerial 3"); 
  121.       return (FALSE); 
  122.     } 
  123.   return (TRUE); 
  124.  
  125. void 
  126. signal_handler_IO (int status) 
  127.   printf ( "received SIGIO signale.\n"); 
  128.   wait_flag = noflag; 
  129.  
  130. int 
  131. main () 
  132.   printf ( "This program updates last time at %s   %s\n", __TIME__, __DATE__); 
  133.   printf ( "STDIO COM1\n"); 
  134.   int fd; 
  135.   fd = open ( "/dev/ttyUSB0", O_RDWR); 
  136.   if (fd == -1
  137.     { 
  138.       perror ( "serialport error\n"); 
  139.     } 
  140.   else 
  141.     { 
  142.       printf ( "open "); 
  143.       printf ( "%s", ttyname (fd)); 
  144.       printf ( " succesfully\n"); 
  145.     } 
  146.  
  147.   set_speed (fd, 115200); 
  148.   if (set_Parity (fd, 8, 1, 'N') == FALSE) 
  149.     { 
  150.       printf ( "Set Parity Error\n"); 
  151.       exit ( 0); 
  152.     } 
  153.  
  154.   char buf[ 255]; 
  155.   fd_set rd; 
  156.   int nread = 0
  157.   while( 1
  158.   { 
  159.     FD_ZERO(&rd); 
  160.     FD_SET(fd, &rd); 
  161.     while(FD_ISSET(fd, &rd)) 
  162.     { 
  163.         if(select(fd+ 1, &rd, NULL, NULL, NULL) < 0
  164.         { 
  165.             perror( "select error\n"); 
  166.         } 
  167.         else 
  168.         { 
  169.             while((nread = read(fd, buf, sizeof(buf))) > 0
  170.             { 
  171.                 printf( "nread = %d,%s\n",nread, buf); 
  172.                 printf( "test\n"); 
  173.                 memset(buf, 0 , sizeof(buf)); 
  174.             } 
  175.         } 
  176.     } 
  177.   } 
  178.   close (fd); 
  179.   return 0

4、示例解析

示例一:用来循环读取键盘输入。

常见的程序片段:

     
     
  1. fs_set readset;
  2. FD_ZERO(&readset);
  3. FD_SET(fd,&readset);
  4. select(fd+ 1,&readset, NULL, NULL, NULL);
  5. if(FD_ISSET(fd,readset){……}
通过 select 返回值,做判断语句。
负值,select 错误;正值,某些文件可读、写或异常;0,等待超时,没有可读、写或异常的文件。

示例二:通过select系统调用进行 io 多路切换,实现异步读取串口数据

首先是串口读取数据,配置是怎么回事? 然后你要了解串口通信。
这个之前有彻底的总结过的, 从新开一篇文章说道说道,在此不做过多解释。

串口编程,除了通过 select 系统调用,在没有数据时阻塞进程,串口有数据需要读时唤醒进程。
还有两种方法,首先是最简单的循环读取程序第二个是通过软中断方式,使用信号 signal 机制读取串口,这里需要注意的是硬件中断是设备驱动层级的,而读写串口是用户级行为,只能通过信号机制模拟中断,信号机制的发生和处理其实于硬件中断无异。

     
     
  1. //代码一:循环读取数据
  2. #include<stdio.h> 
  3. #include<stdlib.h> 
  4. #include<unistd.h> 
  5. #include<sys/types.h> 
  6. #include<sys/stat.h> 
  7. #include<fcntl.h> 
  8. #include<termios.h> 
  9. #include<errno.h> 
  10.  
  11. #define FALSE -1 
  12. #define TRUE 0 
  13.  
  14. int speed_arr[] = { B38400, B19200, B9600, B4800, B2400, B1200, B300,B38400, B19200, B9600, B4800, B2400, B1200, B300, }; 
  15. int name_arr[] = { 38400192009600480024001200300, 38400, 192009600, 4800, 2400, 1200300, }; 
  16. void set_speed(int fd, int speed)
  17.   int   i;  
  18.   int   status;  
  19.   struct termios   Opt; 
  20.   tcgetattr(fd, &Opt);  
  21.   for ( i= 0;  i < sizeof(speed_arr) / sizeof( int);  i++) {  
  22.     if  (speed == name_arr[i]) {      
  23.       tcflush(fd, TCIOFLUSH);      
  24.       cfsetispeed(&Opt, speed_arr[i]);   
  25.       cfsetospeed(&Opt, speed_arr[i]);    
  26.       status = tcsetattr(fd, TCSANOW, &Opt);   
  27.       if  (status != 0) {         
  28.         perror( "tcsetattr fd1");   
  29.         return;      
  30.       }     
  31.       tcflush(fd,TCIOFLUSH);    
  32.     }   
  33.   } 
  34.  
  35. int set_Parity(int fd,int databits,int stopbits,int parity) 
  36. {  
  37.     struct termios options;  
  38.     if  ( tcgetattr( fd,&options)  !=  0) {  
  39.         perror( "SetupSerial 1");      
  40.         return(FALSE);   
  41.     } 
  42.     options.c_cflag &= ~CSIZE;  
  43.     switch (databits)  
  44.     {    
  45.     case 7:      
  46.         options.c_cflag |= CS7;  
  47.         break
  48.     case 8:      
  49.         options.c_cflag |= CS8; 
  50.         break;    
  51.     default:     
  52.         fprintf( stderr, "Unsupported data size\n"); return (FALSE);   
  53.     } 
  54.     switch (parity)  
  55.     {    
  56.         case 'n'
  57.         case 'N':     
  58.             options.c_cflag &= ~PARENB;   /* Clear parity enable */ 
  59.             options.c_iflag &= ~INPCK;     /* Enable parity checking */  
  60.             break;   
  61.         case 'o':    
  62.         case 'O':      
  63.             options.c_cflag |= (PARODD | PARENB);  
  64.             options.c_iflag |= INPCK;             /* Disnable parity checking */  
  65.             break;   
  66.         case 'e':   
  67.         case 'E':    
  68.             options.c_cflag |= PARENB;     /* Enable parity */     
  69.             options.c_cflag &= ~PARODD;     
  70.             options.c_iflag |= INPCK;       /* Disnable parity checking */ 
  71.             break
  72.         case 'S':  
  73.         case 's'/*as no parity*/    
  74.             options.c_cflag &= ~PARENB; 
  75.             options.c_cflag &= ~CSTOPB; break;   
  76.         default:    
  77.             fprintf( stderr, "Unsupported parity\n");     
  78.             return (FALSE);   
  79.         }   
  80.      
  81.     switch (stopbits) 
  82.     {    
  83.         case 1:     
  84.             options.c_cflag &= ~CSTOPB;   
  85.             break;   
  86.         case 2:     
  87.             options.c_cflag |= CSTOPB;   
  88.            break
  89.         default:     
  90.              fprintf( stderr, "Unsupported stop bits\n");   
  91.              return (FALSE);  
  92.     }  
  93.     /* Set input parity option */  
  94.     if (parity != 'n')    
  95.         options.c_iflag |= INPCK;  
  96.     tcflush(fd,TCIFLUSH); 
  97.     options.c_cc[VTIME] = 150;  
  98.     options.c_cc[VMIN] = 0; /* Update the options and do it NOW */ 
  99.     if (tcsetattr(fd,TCSANOW,&options) != 0)    
  100.     {  
  101.         perror( "SetupSerial 3");    
  102.         return (FALSE);   
  103.     }  
  104.     return (TRUE);   
  105.  
  106. int main() 
  107.     printf( "This program updates last time at %s   %s\n",__TIME__,__DATE__); 
  108.     printf( "STDIO COM1\n"); 
  109.     int fd; 
  110.     fd = open( "/dev/ttyS0",O_RDWR); 
  111.     if(fd == -1
  112.     { 
  113.         perror( "serialport error\n"); 
  114.     } 
  115.     else 
  116.     { 
  117.         printf( "open "); 
  118.         printf( "%s",ttyname(fd)); 
  119.         printf( " succesfully\n"); 
  120.     } 
  121.  
  122.     set_speed(fd, 115200); 
  123.     if (set_Parity(fd, 8, 1, 'N') == FALSE)  { 
  124.         printf( "Set Parity Error\n"); 
  125.         exit ( 0); 
  126.     } 
  127.     char buf[] = "fe55aa07bc010203040506073d"
  128.     write(fd,&buf, 26); 
  129.     char buff[ 512];  
  130.     int nread;   
  131.     while( 1
  132.     { 
  133.         if((nread = read(fd, buff, 512))> 0
  134.         { 
  135.             printf( "\nLen: %d\n",nread); 
  136.             buff[nread+ 1] = '\0'
  137.             printf( "%s",buff); 
  138.         } 
  139.     } 
  140.     close(fd); 
  141.     return 0

     
     
  1. //代码二:通过signal机制读取数据
  2. #include<stdio.h> 
  3. #include<stdlib.h> 
  4. #include<unistd.h> 
  5. #include<sys/types.h> 
  6. #include<sys/stat.h> 
  7. #include<sys/signal.h> 
  8. #include<fcntl.h> 
  9. #include<termios.h> 
  10. #include<errno.h> 
  11.  
  12. #define FALSE -1 
  13. #define TRUE 0 
  14. #define flag 1 
  15. #define noflag 0 
  16.  
  17. int wait_flag = noflag; 
  18. int STOP = 0
  19. int res; 
  20.  
  21. int speed_arr[] = 
  22.   { B38400, B19200, B9600, B4800, B2400, B1200, B300, B38400, B19200, B9600, 
  23. B4800, B2400, B1200, B300, }; 
  24. int name_arr[] = 
  25.   { 38400, 19200, 9600, 4800, 2400, 1200, 300, 38400, 19200, 9600, 4800, 2400
  26. 1200, 300, }; 
  27. void 
  28. set_speed (int fd, int speed) 
  29.   int i; 
  30.   int status; 
  31.   struct termios Opt; 
  32.   tcgetattr (fd, &Opt); 
  33.   for (i = 0; i < sizeof (speed_arr) / sizeof ( int); i++) 
  34.     { 
  35.       if (speed == name_arr[i]) 
  36.     { 
  37.       tcflush (fd, TCIOFLUSH); 
  38.       cfsetispeed (&Opt, speed_arr[i]); 
  39.       cfsetospeed (&Opt, speed_arr[i]); 
  40.       status = tcsetattr (fd, TCSANOW, &Opt); 
  41.       if (status != 0
  42.         { 
  43.           perror ( "tcsetattr fd1"); 
  44.           return
  45.         } 
  46.       tcflush (fd, TCIOFLUSH); 
  47.     } 
  48.     } 
  49.  
  50. int 
  51. set_Parity (int fd, int databits, int stopbits, int parity) 
  52.   struct termios options; 
  53.   if (tcgetattr (fd, &options) != 0
  54.     { 
  55.       perror ( "SetupSerial 1"); 
  56.       return (FALSE); 
  57.     } 
  58.   options.c_cflag &= ~CSIZE; 
  59.   switch (databits) 
  60.     { 
  61.     case 7
  62.       options.c_cflag |= CS7; 
  63.       break
  64.     case 8
  65.       options.c_cflag |= CS8; 
  66.       break
  67.     default
  68.       fprintf ( stderr, "Unsupported data size\n"); 
  69.       return (FALSE); 
  70.     } 
  71.   switch (parity) 
  72.     { 
  73.     case 'n'
  74.     case 'N'
  75.       options.c_cflag &= ~PARENB;   /* Clear parity enable */ 
  76.       options.c_iflag &= ~INPCK;    /* Enable parity checking */ 
  77.       break
  78.     case 'o'
  79.     case 'O'
  80.       options.c_cflag |= (PARODD | PARENB); 
  81.       options.c_iflag |= INPCK; /* Disnable parity checking */ 
  82.       break
  83.     case 'e'
  84.     case 'E'
  85.       options.c_cflag |= PARENB;    /* Enable parity */ 
  86.       options.c_cflag &= ~PARODD; 
  87.       options.c_iflag |= INPCK; /* Disnable parity checking */ 
  88.       break
  89.     case 'S'
  90.     case 's':           /*as no parity */ 
  91.       options.c_cflag &= ~PARENB; 
  92.       options.c_cflag &= ~CSTOPB; 
  93.       break
  94.     default
  95.       fprintf ( stderr, "Unsupported parity\n"); 
  96.       return (FALSE); 
  97.     } 
  98.  
  99.   switch (stopbits) 
  100.     { 
  101.     case 1
  102.       options.c_cflag &= ~CSTOPB; 
  103.       break
  104.     case 2
  105.       options.c_cflag |= CSTOPB; 
  106.       break
  107.     default
  108.       fprintf ( stderr, "Unsupported stop bits\n"); 
  109.       return (FALSE); 
  110.     } 
  111.   /* Set input parity option */ 
  112.   if (parity != 'n'
  113.     options.c_iflag |= INPCK; 
  114.   tcflush (fd, TCIFLUSH); 
  115.   options.c_cc[VTIME] = 150
  116.   options.c_cc[VMIN] = 0;   /* Update the options and do it NOW */ 
  117.   if (tcsetattr (fd, TCSANOW, &options) != 0
  118.     { 
  119.       perror ( "SetupSerial 3"); 
  120.       return (FALSE); 
  121.     } 
  122.   return (TRUE); 
  123.  
  124. void 
  125. signal_handler_IO (int status) 
  126.   printf ( "received SIGIO signale.\n"); 
  127.   wait_flag = noflag; 
  128.  
  129. int 
  130. main () 
  131.   printf ( "This program updates last time at %s   %s\n", __TIME__, __DATE__); 
  132.   printf ( "STDIO COM1\n"); 
  133.   int fd; 
  134.   struct sigaction saio; 
  135.   fd = open ( "/dev/ttyUSB0", O_RDWR); 
  136.   if (fd == -1
  137.     { 
  138.       perror ( "serialport error\n"); 
  139.     } 
  140.   else 
  141.     { 
  142.       printf ( "open "); 
  143.       printf ( "%s", ttyname (fd)); 
  144.       printf ( " succesfully\n"); 
  145.     } 
  146.  
  147.   saio.sa_handler = signal_handler_IO; 
  148.   sigemptyset (&saio.sa_mask); 
  149.   saio.sa_flags = 0
  150.   saio.sa_restorer = NULL
  151.   sigaction (SIGIO, &saio, NULL); 
  152.  
  153.   //allow the process to receive SIGIO 
  154.   fcntl (fd, F_SETOWN, getpid ()); 
  155.   //make the file descriptor asynchronous 
  156.   fcntl (fd, F_SETFL, FASYNC); 
  157.  
  158.   set_speed (fd, 115200); 
  159.   if (set_Parity (fd, 8, 1, 'N') == FALSE) 
  160.     { 
  161.       printf ( "Set Parity Error\n"); 
  162.       exit ( 0); 
  163.     } 
  164.  
  165.   char buf[ 255]; 
  166. while (STOP == 0
  167.     { 
  168.       usleep ( 100000); 
  169.       /* after receving SIGIO ,wait_flag = FALSE,input is availabe and can be read */ 
  170.       if (wait_flag == 0
  171.     { 
  172.       memset (buf, 0, sizeof(buf)); 
  173.       res = read (fd, buf, 255); 
  174.       printf ( "nread=%d,%s\n", res, buf); 
  175. //    if (res ==1) 
  176. //      STOP = 1;       /*stop loop if only a CR was input */ 
  177.       wait_flag = flag; /*wait for new input */ 
  178.     } 
  179.     } 
  180.  
  181.  
  182.   close (fd); 
  183.   return 0

5、select 函数实现

下面我们分两个过程来分析select:

(1) select的睡眠过程

支持阻塞操作的设备驱动通常会实现一组自身的等待队列如读/写等待队列用于支持上层(用户层)所需的 BLOCK 或NONBLOCK 操作。当应用程序通过设备驱动访问该设备时(默认为 BLOCK 操作),若该设备当前没有数据可读或写,则将该用户进程插入到该设备驱动对应的读/写等待队列让其睡眠一段时间,等到有数据可读/写时再将该进程唤醒。
select 就是巧妙的利用等待队列机制让用户进程适当在没有资源可读/写时睡眠,有资源可读/写时唤醒。下面我们看看 select 睡眠的详细过程。
select 会循环遍历它所监测的 fd_set 内的所有文件描述符对应的驱动程序的 poll 函数。驱动程序提供的 poll 函数首先会将调用 select 的用户进程插入到该设备驱动对应资源的等待队列(如读/写等待队列),然后返回一个 bitmask 告诉 select 当前资源哪些可用。当 select 循环遍历完所有 fd_set 内指定的文件描述符对应的 poll 函数后,如果没有一个资源可用(即没有一个文件可供操作),则 select 让该进程睡眠,一直等到有资源可用为止,进程被唤醒(或者timeout )继续往下执行。

下面分析一下代码是如何实现的。
select 的调用 path 如下:sys_select -> core_sys_select -> do_select
查看 kernel/fs/select.c  其中最重要的函数是 do_select, 最主要的工作是在这里, 前面两个函数主要做一些准备工作。do_select 定义如下:

    
    
  1. int do_select(int n, fd_set_bits *fds, s64 *timeout)
  2. {
  3.          struct poll_wqueues table;
  4.          poll_table *wait;
  5.          int retval, i;
  6.          rcu_read_lock();
  7.          retval = max_select_fd(n, fds);
  8.          rcu_read_unlock();
  9.          if (retval < 0)
  10.                    return retval;
  11.          n = retval;
  12.          poll_initwait(&table);
  13.          wait = &table.pt;
  14.          if (!*timeout)
  15.                    wait = NULL;
  16.          retval = 0;        //retval用于保存已经准备好的描述符数,初始为0
  17.          for (;;) {
  18.                    unsigned long *rinp, *routp, *rexp, *inp, *outp, * exp;
  19.                    long __timeout;
  20.                    set_current_state(TASK_INTERRUPTIBLE);    //将当前进程状态改为TASK_INTERRUPTIBLE
  21.                    inp = fds->in; outp = fds->out; exp = fds->ex;
  22.                    rinp = fds->res_in; routp = fds->res_out; rexp = fds->res_ex;
  23.                    for (i = 0; i < n; ++rinp, ++routp, ++rexp) { //遍历每个描述符
  24.                             unsigned long in, out, ex, all_bits, bit = 1, mask, j;
  25.                             unsigned long res_in = 0, res_out = 0, res_ex = 0;
  26.                             const struct file_operations *f_op = NULL;
  27.                             struct file *file = NULL;
  28.                             in = *inp++; out = *outp++; ex = * exp++;
  29.                             all_bits = in | out | ex;
  30.                             if (all_bits == 0) {
  31.                                      i += __NFDBITS;       // //如果这个字没有待查找的描述符, 跳过这个长字(32位)
  32.                                      continue;
  33.                             }
  34.                             for (j = 0; j < __NFDBITS; ++j, ++i, bit <<= 1) {     //遍历每个长字里的每个位
  35.                                      int fput_needed;
  36.                                      if (i >= n)
  37.                                                break;
  38.                                      if (!(bit & all_bits))
  39.                                                continue;
  40.                                      file = fget_light(i, &fput_needed);
  41.                                      if (file) {
  42.                                                f_op = file->f_op;
  43.                                                MARK(fs_select, "%d %lld",
  44.                                                                  i, ( long long)*timeout);
  45.                                                mask = DEFAULT_POLLMASK;
  46.                                                if (f_op && f_op->poll)
  47. /* 在这里循环调用所监测的fd_set内的所有文件描述符对应的驱动程序的poll函数 */
  48.                                                         mask = (*f_op->poll)(file, retval ? NULL : wait);
  49.                                                fput_light(file, fput_needed);
  50.                                                if ((mask & POLLIN_SET) && (in & bit)) {
  51.                                                         res_in |= bit; //如果是这个描述符可读, 将这个位置位
  52.                                                         retval++;  //返回描述符个数加1
  53.                                                }
  54.                                                if ((mask & POLLOUT_SET) && (out & bit)) {
  55.                                                         res_out |= bit;
  56.                                                         retval++;
  57.                                                }
  58.                                                if ((mask & POLLEX_SET) && (ex & bit)) {
  59.                                                         res_ex |= bit;
  60.                                                         retval++;
  61.                                                }
  62.                                      }
  63.                                      cond_resched();
  64.                             }
  65. //返回结果
  66.                             if (res_in)
  67.                                      *rinp = res_in;
  68.                             if (res_out)
  69.                                      *routp = res_out;
  70.                             if (res_ex)
  71.                                      *rexp = res_ex;
  72.                    }
  73.                    wait = NULL;
  74. /* 到这里遍历结束。retval保存了检测到的可操作的文件描述符的个数。如果有文件可操作,则跳出for(;;)循环,直接返回。若没有文件可操作且timeout时间未到同时没有收到signal,则执行schedule_timeout睡眠。睡眠时间长短由__timeout决定,一直等到该进程被唤醒。
  75. 那该进程是如何被唤醒的?被谁唤醒的呢?
  76. 我们看下面的select唤醒过程*/
  77.                    if (retval || !*timeout || signal_pending(current))
  78.                             break;
  79.                   if(table.error) {
  80.                             retval = table.error;
  81.                             break;
  82.                    }
  83.                    if (*timeout < 0) {
  84.                             /* Wait indefinitely */
  85.                             __timeout = MAX_SCHEDULE_TIMEOUT;
  86.                    } else if (unlikely(*timeout >= (s64)MAX_SCHEDULE_TIMEOUT - 1)) {
  87.                             /* Wait for longer than MAX_SCHEDULE_TIMEOUT. Do it in a loop */
  88.                             __timeout = MAX_SCHEDULE_TIMEOUT - 1;
  89.                             *timeout -= __timeout;
  90.                    } else {
  91.                             __timeout = *timeout;
  92.                             *timeout = 0;
  93.                    }
  94.                    __timeout = schedule_timeout(__timeout);
  95.                    if (*timeout >= 0)
  96.                             *timeout += __timeout;
  97.          }
  98.          __set_current_state(TASK_RUNNING);
  99.          poll_freewait(&table);
  100.          return retval;
  101. }

(2)select的唤醒过程

前面介绍了 select 会循环遍历它所监测的 fd_set 内的所有文件描述符对应的驱动程序的 poll 函数。驱动程序提供的poll 函数首先会将调用 select 的用户进程插入到该设备驱动对应资源的等待队列(如读/写等待队列),然后返回一个 bitmask 告诉 select 当前资源哪些可用。
一个典型的驱动程序poll函数实现如下:

    
    
  1. (摘自《Linux Device Drivers – ThirdEdition》Page 165)
  2. static unsigned int scull_p_poll(struct file *filp, poll_table *wait)
  3. {
  4.     struct scull_pipe *dev = filp->private_data;
  5.     unsigned int mask = 0;
  6.     /*
  7.      * The buffer is circular; it is considered full
  8.      * if "wp" is right behind "rp" and empty if the
  9.      * two are equal.
  10.      */
  11.     down(&dev->sem);
  12.     poll_wait(filp, &dev->inq,  wait);
  13.     poll_wait(filp, &dev->outq, wait);
  14.     if (dev->rp != dev->wp)
  15.         mask |= POLLIN | POLLRDNORM;    /* readable */
  16.     if (spacefree(dev))
  17.         mask |= POLLOUT | POLLWRNORM;   /* writable */
  18.     up(&dev->sem);
  19.     return mask;
  20. }
将用户进程插入驱动的等待队列是通过poll_wait做的。
Poll_wait定义如下:

    
    
  1. static inline void poll_wait(struct file * filp, wait_queue_head_t * wait_address, poll_table *p)
  2. {
  3.          if (p && wait_address)
  4.                    p->qproc(filp, wait_address, p);
  5. }
这里的p->qproc在do_select内poll_initwait(&table)被初始化为__pollwait,如下:

    
    
  1. void poll_initwait(struct poll_wqueues *pwq)
  2. {
  3.          init_poll_funcptr(&pwq->pt, __pollwait);
  4.          pwq->error = 0;
  5.          pwq->table = NULL;
  6.          pwq->inline_index = 0;
  7. }
__pollwait定义如下:

    
    
  1. /* Add a new entry */
  2. static void __pollwait(struct file *filp, wait_queue_head_t *wait_address,
  3.                                      poll_table *p)
  4. {
  5.          struct poll_table_entry *entry = poll_get_entry(p);
  6.          if (!entry)
  7.                    return;
  8.          get_file(filp);
  9.          entry->filp = filp;
  10.          entry->wait_address = wait_address;
  11.          init_waitqueue_entry(&entry->wait, current);
  12.          add_wait_queue(wait_address,&entry->wait);
  13. }
通过 init_waitqueue_entry 初始化一个等待队列项,这个等待队列项关联的进程即当前调用 select 的进程。然后将这个等待队列项插入等待队列 wait_address。Wait_address 即在驱动 poll 函数内调用 poll_wait(filp, &dev->inq,  wait);时传入的该驱动的 &dev->inq 或者 &dev->outq 等待队列。

注: 关于等待队列的工作原理可以参考下面这篇文档:
参看:等待队列(二)

到这里我们明白了select如何当前进程插入所有所监测的fd_set关联的驱动内的等待队列,那进程究竟是何时让出CPU进入睡眠状态的呢?
进入睡眠状态是在do_select内调用schedule_timeout(__timeout)实现的。当select遍历完fd_set内的所有设备文件,发现没有文件可操作时(即retval=0),则调用schedule_timeout(__timeout)进入睡眠状态。
唤醒该进程的过程通常是在所监测文件的设备驱动内实现的,驱动程序维护了针对自身资源读写的等待队列。当设备驱动发现自身资源变为可读写并且有进程睡眠在该资源的等待队列上时,就会唤醒这个资源等待队列上的进程。
举个例子,比如内核的8250 uart driver:
Uart是使用的Tty层维护的两个等待队列, 分别对应于读和写: (uart是tty设备的一种)

    
    
  1. struct tty_struct {
  2.          ……
  3.          wait_queue_head_t write_wait;
  4.          wait_queue_head_t read_wait;
  5.          ……
  6. }
当uart设备接收到数据,会调用tty_flip_buffer_push(tty);将收到的数据push到tty层的buffer。
然后查看是否有进程睡眠的读等待队列上,如果有则唤醒该等待会列。
过程如下:

    
    
  1. serial8250_interrupt -> serial8250_handle_port -> receive_chars -> tty_flip_buffer_push ->
  2. flush_to_ldisc -> disc->receive_buf
  3. 在disc->receive_buf函数内:
  4. if (waitqueue_active(&tty->read_wait)) //若有进程阻塞在read_wait上则唤醒
  5. wake_up_interruptible(&tty->read_wait);
到这里明白了select进程被唤醒的过程。由于该进程是阻塞在所有监测的文件对应的设备等待队列上的,因此在timeout时间内,只要任意个设备变为可操作,都会立即唤醒该进程,从而继续往下执行。这就实现了select的当有一个文件描述符可操作时就立即唤醒执行的基本原理。

6、函数 pselect


    
    
  1. #include <sys/select.h>
  2. int pselect(int nfds, fd_set *readfds, fd_set *writefds,
  3.                    fd_set *exceptfds, const struct timespec *timeout,
  4.                    const sigset_t *sigmask);

(1)pselect 与 select 不同之处

select 的超时值用 timeval 结构指定,但 pselect 使用 timespec 结构。timespec 结构以秒和纳秒表示超时值,而非秒和微妙。如果平台支持这样的时间精度,那么 timespec 就能提供精确的超时时间。
结构体 timespec 定义如下:

    
    
  1. struct timespec {
  2.                long    tv_sec;         /* seconds */
  3.                long    tv_nsec;        /* nanoseconds */
  4.            };
pselect 的超时值被声明为 const,这保证了调用 pselect 不会改变此值。
pselect 可使用可选信号屏蔽字。若 sigmask 为 NULL,那么在与信号有关的方面,pselect 的运行状况和 select 相同。否则,sigmask 指向一信号屏蔽字,在调用 pselect 时,以原子操作的方式安装该信号屏蔽字。在返回时,恢复以前的信号屏蔽字。

(2)示例说明

参看:pselect()

    
    
  1. #include        <time.h>
  2. #include        <stdio.h>
  3. #include        <stdlib.h>
  4. #include        <signal.h>
  5. #include        <unistd.h>
  6. #include        <sys/select.h>
  7. #define BUFFSIZE 80
  8. void sig_int(int signo);
  9. void err_sys(const char *p_error);
  10. void sig_alrm(int signo)
  11. {
  12.     char s[] = "receive";
  13.     psignal(signo, s);
  14.     return;
  15. }
  16. int
  17. main (int argc, char **argv)
  18. {
  19.         int             maxfdp1;
  20.         fd_set          rset;
  21.         sigset_t        sigmask;
  22.         ssize_t         nread;
  23.         char            buf[BUFFSIZE];
  24.       sigset_t sigset;
  25.    struct sigaction act;
  26.    // set SIGALRM signal handler
  27.    act.sa_handler = sig_alrm;
  28.    if (sigemptyset(&act.sa_mask) == -1)      
  29.     err_sys( "sigemptyset");
  30.    act.sa_flags = 0;
  31.    if (sigaction(SIGALRM, &act, NULL) == -1)
  32.     err_sys( "sigaction");
  33.    // initialize signal set and addition SIGALRM into sigset
  34.    if (sigemptyset(&sigset) == -1)
  35.     err_sys( "sigemptyet");
  36.    if (sigaddset(&sigset, SIGALRM) == -1)
  37.     err_sys( "sigaddset");
  38.    alarm( 1);   
  39.         FD_ZERO(&rset);
  40.         FD_SET(STDIN_FILENO, &rset);
  41.         maxfdp1 = STDIN_FILENO + 1;
  42.         if (pselect(maxfdp1, &rset, NULL, NULL, NULL, &sigset) <= 0)
  43.                 err_sys( "pselect error");
  44.         if (FD_ISSET(STDIN_FILENO, &rset))
  45.   {
  46.                 if ((nread = read(STDIN_FILENO, buf, BUFFSIZE)) == -1)
  47.                         err_sys( "read error");
  48.                 if (write(STDOUT_FILENO, buf, nread) != nread)
  49.                         err_sys( "write error");
  50.         }
  51.         exit( 0);
  52. }
  53. void
  54. sig_int (int signo)
  55. {
  56.         char    s[] = "received";
  57.         psignal(signo, s);
  58.         return;
  59. }
  60. void
  61. err_sys (const char *p_error)
  62. {
  63.         perror(p_error);
  64.         exit( 1);
  65. }
  66. 输出结果:
  67. d
  68. receive: Alarm clock
  69. d
上段代码如果没有 CTRL+C 送上一个 SIGINT 信号,将永远阻塞在与用户的交互上,ALARM 产生的 SIGALRM 信号永远打断不了 PSELECT, ALARM 信号被成功屏蔽。

三、函数 poll

poll 函数类似于 select,但是程序员结构有所不同。虽然 poll 函数时 system V 引入进来支持 STREAMS 子系统的,但是 poll 函数可用于任何类型的文件描述符

    
    
  1. #include <poll.h>
  2. int poll(struct pollfd *fds, nfds_t nfds, int timeout);
  3. 返回值:准备就绪的描述符数目;若超时,返回 0;若出错,返回 -1.

1、参数解析

与 select 不同,poll 不是为每个条件(可读性、可写性和异常条件)构造一个描述符集,而是构造一个 pollfd 结构的数组,每个数组元素指定一个描述符 编号以及我们队该描述符感兴趣的条件

    
    
  1. struct pollfd {
  2.                int   fd;         /* file descriptor */
  3.                short events;     /* requested events */
  4.                short revents;    /* returned events */
  5.            };
fds 数组中的元素数由 nfds 指定。


应将每个数组元素的 events 成员设置为上图所示值的一个或几个,通过这些值告诉内核我们关心的是每个描述符的哪些事件。返回时,revnets 成员由内核设置,用于说明每个描述符发生了哪些事件。
注意,poll 没有更改 events 成员。这与 select 不同,select 修改其参数以指示哪个描述符已准备好了。
上图中的前 4 行测试的是可读性,接下来的 3 行测试的是可写性,最后 3 行测试的是异常条件。最后 3 行是由内核在返回时设置的。即使在 events 字段中没有指定这 3 个值,如果是相应条件发生,在 revents 中也会返回它们。
当一个描述符被挂断(POLLHUP)后,就不能再写该描述符,但是有可能仍然可以从该描述符读取到数据。

POLLIN | POLLPRI 等价于 select() 的读事件,POLLOUT |POLLWRBAND 等价于 select() 的写事件。POLLIN 等价于 POLLRDNORM |POLLRDBAND,而 POLLOUT 则等价于 POLLWRNORM。
查看 /usr/include/asm-generic/poll.h 可看到如下定义:

    
    
  1. /* These are specified by iBCS2 */
  2. #define POLLIN  0x0001
  3. #define POLLPRI  0x0002
  4. #define POLLOUT  0x0004
  5. #define POLLERR  0x0008
  6. #define POLLHUP  0x0010
  7. #define POLLNVAL 0x0020
  8. /* The rest seem to be more-or-less nonstandard. Check them! */
  9. #define POLLRDNORM 0x0040
  10. #define POLLRDBAND 0x0080
  11. #ifndef POLLWRNORM
  12. #define POLLWRNORM 0x0100
  13. #endif
  14. #ifndef POLLWRBAND
  15. #define POLLWRBAND 0x0200
  16. #endif

poll 的最后一个参数指定的是我们愿意等待多长时间。如同 select 一样,有 3 种不同的情形。
  timeout == -1
        永远等待。(某些系统在 <stropts.h>中定义了常量 INFTIM,其值通常是 -1)当所指定的描述符中的一个已准备好,或捕捉到一个信号时返回。如果捕捉到一个信号,则 poll 返回 -1,errno 设置为 EINTR。
    timeout == 0
        不等待。测试所有描述符并立即返回。这是一种轮询系统的方法,可以找到多个描述符的状态而不阻塞 poll 函数。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值