linux中的poll机制分析

来源:互联网 发布:win10 linux 双系统 编辑:程序博客网 时间:2024/06/02 07:01

最近看了一下Linux Poll 机制的实现,看了韦老师的分析文档,总结如下:

int poll(struct pollfd *fds,nfds_t nfds, int timeout);

总的来说,Poll机制会判断fds中的文件是否可读,如果可读则会立即返回,返回的值就是可读fd的数量,如果不可读,那么就进程就会休眠timeout这么长的时间,然后再来判断是否有文件可读,如果有,返回fd的数量,如果没有,则返回0.  

在内核中大致上实现过程:

当应用程序调用poll函数的时候,会调用到系统调用sys_poll函数,该函数最终调用do_poll函数,do_poll函数中有一个死循 环,在里面又会利用do_pollfd函数去调用驱动中的poll函数(fds中每个成员的字符驱动程序都会被扫描到),驱动程序中的Poll函数的工作 有两个,一个就是调用poll_wait 函数,把进程挂到等待队列中去(这个是必须的,你要睡眠,必须要在一个等待队列上面,否则到哪里去唤醒你呢??),另一个是确定相关的fd是否有内容可 读,如果可读,就返回1,否则返回0,如果返回1 ,do_poll函数中的count++,    然后  do_poll函数然后判断三个条件(if (count ||!timeout || signal_pending(current)))如果成立就直接跳出,如果不成立,就睡眠timeout个jiffes这么长的时间(调用schedule_timeout实现睡眠),如果在这段时间内没有其他进程去唤醒它,那么第二次执行判断的时候就会跳出死循环。如果在这段时间内有其他进程唤醒它,那么也可以跳出死循环返回(例如我们可以利用中断处理函数去唤醒它,这样的话一有数据可读,就可以让它立即返回)。

poll机制函数调用如下:

  1. static unsigned int fourth_drv_poll(struct file *file, poll_table *wait)  
  2. {  
  3.     unsigned int mask = 0;  
  4.   
  5.     /* 该函数,只是将进程挂在button_waitq队列上,而不是立即休眠 */  
  6.     poll_wait(file, &button_waitq, wait);  
  7.   
  8.     /* 当没有按键按下时,即不会进入按键中断处理函数,此时ev_press = 0  
  9.      * 当按键按下时,就会进入按键中断处理函数,此时ev_press被设置为1 
  10.      */  
  11.     if(ev_press)  
  12.     {  
  13.         mask |= POLLIN | POLLRDNORM;  /* 表示有数据可读 */  
  14.     }  
  15.   
  16.     /* 如果有按键按下时,mask |= POLLIN | POLLRDNORM,否则mask = 0 */  
  17.     return mask;    
  18. }  

按键驱动测试源码如下:
[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. #include <stdio.h>  
  2. #include <sys/types.h>  
  3. #include <sys/stat.h>  
  4. #include <fcntl.h>  
  5. #include <unistd.h>  
  6. #include <poll.h>  
  7.   
  8. /* fourth_test 
  9.  */   
  10. int main(int argc ,char *argv[])  
  11.   
  12. {  
  13.     int fd;  
  14.     unsigned char key_val;  
  15.     struct pollfd fds;  
  16.     int ret;  
  17.   
  18.     fd = open("/dev/buttons",O_RDWR);  
  19.     if (fd < 0)  
  20.     {  
  21.         printf("open error\n");  
  22.     }  
  23.     fds.fd = fd;  
  24.     fds.events = POLLIN;  
  25.     while(1)  
  26.     {  
  27.         /* A value of 0 indicates  that the call timed out and no file descriptors were ready 
  28.          * poll函数返回0时,表示5s时间到了,而这段时间里,没有事件发生"数据可读" 
  29.          */  
  30.         ret = poll(&fds,1,5000);  
  31.         if(ret == 0)  
  32.         {  
  33.             printf("time out\n");  
  34.         }  
  35.         else    /* 如果没有超时,则读出按键值 */  
  36.         {  
  37.             read(fd,&key_val,1);  
  38.             printf("key_val = 0x%x\n",key_val);  
  39.         }     
  40.     }  
  41.     return 0;  
  42. }  
运行当按键没有被按下时,5秒之后,会显示出time out,表示时间已到,在这5秒时间里,没有按键被按下,即没有数据可读,当按键按下时,立即打印出按下的按键;

0 0
原创粉丝点击