内核poll()函数返回超时



在LDD3的scul_p_p_poll函数中,如果我理解正确,如果poll_wait没有被唤醒并且发生超时,poll将返回零。

static unsigned int scull_p_poll(struct file *filp, poll_table *wait)
{
    struct scull_pipe *dev = filp->private_data;
    unsigned int mask = 0;
    /*
     * The buffer is circular; it is considered full
     * if "wp" is right behind "rp" and empty if the
     * two are equal.
     */
    down(&dev->sem);
    poll_wait(filp, &dev->inq,  wait);
    poll_wait(filp, &dev->outq, wait);
    if (dev->rp != dev->wp)
        mask |= POLLIN | POLLRDNORM;    /* readable */
    if (spacefree(dev))
        mask |= POLLOUT | POLLWRNORM;   /* writable */
    up(&dev->sem);
    return mask;
}

关于poll_wait将如何工作,这是一个正确的假设吗?这就是我从《为什么我们需要在民意调查中调用poll_wait?以及如何在内核模块代码中添加轮询功能?

正如我看到的所有例子一样,如果不存在有效的POLLIN或POLLRDNORM状态,则返回零,我认为零是正确的超时返回。有人能澄清这一点吗?或者给我指一下显示这一点的文件?我没有读过比poll.h 更深的东西

在给定的例子中,假设您有一个用户空间应用程序像下面这样轮询您的驱动程序以获取读取情况。

   struct pollfd pofd;
   pofd.fd = open("/dev/scull", O_RDONLY | O_NONBLOCK);
   pofd.events = POLLIN | POLLRDNORM;
   pofd.revents = 0;
   /* Notice no timeout given. */
   ret = poll(&pofd, 1, -1);
   if (pofd.revents | POLLIN) {
      printf("POLLIN done, reading from the device.n");
      ret = read(pofd.fd, receive, BUFFER_LENGTH);
      ......
   }

一旦完成数据准备条件,您需要唤醒内核空间设备驱动程序中的等待队列,如:

wake_up_interruptible(&dev->inq);

最新更新