Chinaunix首页 | 论坛 | 博客
  • 博客访问: 464297
  • 博文数量: 89
  • 博客积分: 1126
  • 博客等级: 少尉
  • 技术积分: 1432
  • 用 户 组: 普通用户
  • 注册时间: 2011-04-11 23:37
文章分类

全部博文(89)

文章存档

2016年(6)

2015年(2)

2014年(1)

2013年(3)

2012年(23)

2011年(54)

分类: 嵌入式

2011-06-26 16:44:43

   在应用中经常调用selec函数实现多路访问,select函数的实现依赖与驱动的 xxx_poll函数。在xxx_poll函数中,核心的函数是poll_wait函数。在网上查了很多资料,发现轮询的操作依赖于select和poll_wait函数的配合。网上对poll函数是这样解释的:
Poll函数原型:
Unsigned int(*poll)(struct file *filp, struct poll_table *wait);

第一个参数为file结构体指针,第二个参数为轮询表指针。这个函数应该进行以下两项工作
1、对可能引起设备文件状态变化的等待队列调用poll_wait()函数,将对应等待队列添加到          poll_table         
2、返回表示是否能对设备进行无阻塞、写访问的掩码
Poll_wait()函数不会引起阻塞。它所做的工作就是把当前进程添加到wait参数指定的等待列表(poll_table)中.

fifo_poll函数:
  1. static unsigned int cfifo_poll(struct file *file, poll_table *wait)
  2. {
  3.     unsigned int mask = 0;
  4.     struct cfifo_t *dev = file->private_data;

  5.     down(&dev->sem);
  6.     poll_wait(file, &dev->r_wait, wait);
  7.     poll_wait(file, &dev->w_wait, wait);

  8.     if(dev->current_len != 0)
  9.         mask |= POLLIN | POLLRDNORM;

  10.     if(dev->current_len != GLOBALMEM_SIZE)
  11.         mask |= POLLOUT | POLLWRNORM;

  12.     up(&dev->sem);
  13.     return mask;
  14. }

问题是:在我们的使用中select 函数经常是要阻塞的,但是在xxx_poll函数中只是通过poll_wait将读写的等待队列加入 poll_table中.并没有阻塞的地方。
poll_wait的实现,总结一下就是:将select的查询的文件描述符的进程等待队列加入到内核的等待队列的维护中,作用就是供select调用查询。
   在select的实现中我们就可以看到这些等待队列的作用了。在内核的fs/select.c中有poll 和select的
相关实现。select函数的核心是:do_select。在这里实现了阻塞,poll_wait()函数也是为它服务的。在poll_wait中加入的等待队列也在这里使用。

  1. int do_select(int n, fd_set_bits *fds, struct timespec *end_time)
  2. {
  3.     ktime_t expire, *to = NULL;
  4.     struct poll_wqueues table;
  5.     poll_table *wait;
  6.     int retval, i, timed_out = 0;
  7.     unsigned long slack = 0;

  8.     rcu_read_lock();
  9.     retval = max_select_fd(n, fds);
  10.     rcu_read_unlock();

  11.     if (retval < 0)
  12.         return retval;
  13.     n = retval;

  14.     poll_initwait(&table);
  15.     wait = &table.pt;
  16.     if (end_time && !end_time->tv_sec && !end_time->tv_nsec) {
  17.         wait = NULL;
  18.         timed_out = 1;
  19.     }

  20.     if (end_time && !timed_out)
  21.         slack = estimate_accuracy(end_time);

  22.     retval = 0;
  23.     for (;;) {                                                                  //
  24.         unsigned long *rinp, *routp, *rexp, *inp, *outp, *exp;

  25.         inp = fds->in; outp = fds->out; exp = fds->ex;
  26.         rinp = fds->res_in; routp = fds->res_out; rexp = fds->res_ex;

  27.         for (i = 0; i < n; ++rinp, ++routp, ++rexp) {
  28.             unsigned long in, out, ex, all_bits, bit = 1, mask, j;
  29.             unsigned long res_in = 0, res_out = 0, res_ex = 0;
  30.             const struct file_operations *f_op = NULL;
  31.             struct file *file = NULL;

  32.             in = *inp++; out = *outp++; ex = *exp++;
  33.             all_bits = in | out | ex;
  34.             if (all_bits == 0) {
  35.                 i += __NFDBITS;
  36.                 continue;
  37.             }

  38.             for (j = 0; j < __NFDBITS; ++j, ++i, bit <<= 1) {
  39.                 int fput_needed;
  40.                 if (i >= n)
  41.                     break;
  42.                 if (!(bit & all_bits))
  43.                     continue;
  44.                 file = fget_light(i, &fput_needed);
  45.                 if (file) {
  46.                     f_op = file->f_op;
  47.                     mask = DEFAULT_POLLMASK;
  48.                     if (f_op && f_op->poll) {
  49.                         wait_key_set(wait, in, out, bit);
  50.                         mask = (*f_op->poll)(file, wait);
  51.                     }
  52.                     fput_light(file, fput_needed);
  53.                     if ((mask & POLLIN_SET) && (in & bit)) {
  54.                         res_in |= bit;
  55.                         retval++;
  56.                         wait = NULL;
  57.                     }
  58.                     if ((mask & POLLOUT_SET) && (out & bit)) {
  59.                         res_out |= bit;
  60.                         retval++;
  61.                         wait = NULL;
  62.                     }
  63.                     if ((mask & POLLEX_SET) && (ex & bit)) {
  64.                         res_ex |= bit;
  65.                         retval++;
  66.                         wait = NULL;
  67.                     }
  68.                 }
  69.             }
  70.             if (res_in)
  71.                 *rinp = res_in;
  72.             if (res_out)
  73.                 *routp = res_out;
  74.             if (res_ex)
  75.                 *rexp = res_ex;
  76.             cond_resched();
  77.         }
  78.         wait = NULL;
  79.         if (retval || timed_out || signal_pending(current)) //如果条件(文件描述符有改变,超时,信号中断)成立 ,break跳出循环
  80.             break;
  81.         if (table.error) {
  82.             retval = table.error;
  83.             break;
  84.         }

  85.         /*
  86.          * If this is the first loop and we have a timeout
  87.          * given, then we convert to ktime_t and set the to
  88.          * pointer to the expiry value.
  89.          */
  90.         if (end_time && !to) {
  91.             expire = timespec_to_ktime(*end_time);
  92.             to = &expire;
  93.         }
  94. //将进程休眠,设置为可被中断状态 可以被read和write 函数中的wake_up_interruptible(&dev->w_wait);wake_up_int//erruptible(&dev->r_wait);函数唤醒这也是poll_wait函数的作用。唤醒后,继续循环,会调用驱动中的xxx_poll函数//,在 if (retval || timed_out || signal_pending(current))完成select调用。

  95.         if (!poll_schedule_timeout(&table, TASK_INTERRUPTIBLE
  96.                      to, slack))
  97.             timed_out = 1;
  98.     }

  99.     poll_freewait(&table);

  100.     return retval;
  101. }

测试代码:
  1. #include
  2. #include
  3. #include
  4. #include
  5. #include
  6. #include
  7. #include
  8. #include

  9. #define FIFO_CLEAR 0x01
  10. #define BUFFER_LEN 20

  11. int main(int argc, char *argv[])
  12. {
  13. int fd;
  14. int i;
  15. fd_set wfds;
  16. fd_set rfds;

  17. //fd = open("/dev/cfifo0", O_RDONLY | O_NONBLOCK);
  18. fd = open("/dev/cfifo0", O_RDONLY | O_WRONLY);
  19. if(fd > 0)
  20. ioctl(fd, FIFO_CLEAR, 0);
  21. else
  22. return -1;

  23.    for(i = 0; i < 3; i++)
  24. {
  25. FD_ZERO(&rfds);
  26. FD_ZERO(&wfds);
  27. FD_SET(fd, &rfds);
  28. // FD_SET(fd, &wfds);
  29. printf("[%s %s %d] select start\n \n", __FUNCTION__, __FILE__, __LINE__);
  30. select(fd+1, &rfds, NULL, NULL, NULL);
  31. printf("[%s %s %d] select end\n \n", __FUNCTION__, __FILE__, __LINE__);

  32. if(FD_ISSET(fd, &rfds) )
  33. {
  34. printf("cfifo can read\n");
  35. }

  36. // if(FD_ISSET(fd, &wfds) )
  37. // {
  38. // printf("cfifo can write\n");
  39. // }
  40. }

  41. return 0;
  42. }

root@wang:/work/wanghuan/drives# ls
bdev.c  cdev.c  cfifo.c  cfifo.ko  cfifo_poll_test.c  Makefile  modules.order  test
root@wang:/work/wanghuan/drives# ./test &
[1] 4457
root@wang:/work/wanghuan/drives# [main cfifo_poll_test.c 51] select start 
//在这里select阻塞 就是在do_select if (!poll_schedule_timeout(&table, TASK_INTERRUPTIBLE, to, slack))
  1.         
root@wang:/work/wanghuan/drives# ls > /dev/cfifo0 
//写入后 cfifo_write的wake_up_interruptible(&dev->r_wait); 唤醒select 进程
[main cfifo_poll_test.c 53] select end
 
cfifo can read
[main cfifo_poll_test.c 51] select start
 
[main cfifo_poll_test.c 53] select end
 
cfifo can read
[main cfifo_poll_test.c 51] select start
 
[main cfifo_poll_test.c 53] select end
 
cfifo can read
[1]+  Done                    ./test

http://cuaib.chinaunix.com/space.php?uid=25014876&do=blog&id=61749这里的一篇文章讲解了poll_wait的实现,

阅读(1246) | 评论(0) | 转发(1) |
0

上一篇:iw工具

下一篇:带参数到内核模块

给主人留下些什么吧!~~