3.8 poll函数的详细分析

下面看看poll函数,个人感觉这个poll函数还是比较重要的:

[cpp]  view plain  copy
  1. static unsigned int mxc_poll(struct file *file, struct poll_table_struct *wait)   
  2. {   
  3.     struct video_device *dev = video_devdata(file);   
  4.     cam_data *cam = video_get_drvdata(dev);   
  5.     wait_queue_head_t *queue = NULL;   
  6.     int res = POLLIN | POLLRDNORM;   
  7.   
  8.     pr_debug("In MVC:mxc_poll\n");   
  9.   
  10.     if (down_interruptible(&cam->busy_lock))   
  11.         return -EINTR;   
  12.   
  13.     queue = &cam->enc_queue;   
  14.     poll_wait(file, queue, wait);   
  15.   
  16.     up(&cam->busy_lock);   
  17.   
  18.     return res;   
  19. }  

在驱动程序中,poll机制非常简单,就是一句代码:

poll_wait(file,queue, wait);

poll_wait所做的工作就是将当前进程添加到wait参数指定的等待列表中。

驱动程序中的poll函数应该返回设备资源的可获取状态,即POLLINPOLLOUTPOLLPRIPOLLERRPOLLNVALD等宏的按位或结果。可以看到,这个驱动程序确实返回了这个结果。


重要的是应用程序中的操作,在应用程序中,如果直接使用轮询操作的话,会大量占用cpu的资源,而使用poll/select操作就会避免这个问题。应用程序中的操作查看man手册就知道了,在这里就不写了。

猜你喜欢

转载自blog.csdn.net/dragon101788/article/details/80660928
3.8