【问题标题】:kernel poll() function return for timeout内核 poll() 函数返回超时
【发布时间】:2016-02-11 16:39:07
【问题描述】:

在LDD3的scull_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 将如何工作的正确假设吗?这就是我从 Why do we need to call poll_wait in poll?How to add poll function to the kernel module code? 那里得到的东西

如果不存在有效的 POLLIN 或 POLLRDNORM 状态,我看到的所有示例都返回零,我假设零是正确的超时返回。任何人都可以澄清这一点或指向我显示这一点的文档吗?我没有比poll.h更深入的阅读

【问题讨论】:

  • 你是不是没看懂answer第一个链接的问题? poll_wait 根本不等待。掩码,返回 scull_p_pollselect/poll 系统调用中请求的掩码进行 AND 运算,并将结果掩码与 0 进行比较。如果结果掩码非零,则设备被视为 ready i>,系统调用返回。否则,设备被视为未就绪,系统调用等待(在scull_p_poll! 之外)。 poll 相关系统调用的实际实现在fs/select.c
  • 啊,感谢您将我指向 fs/select.c - 我实际上并没有理解它,但是您重复的“poll_wait 根本不等待”让我印象深刻。我现在明白了

标签: kernel driver


【解决方案1】:

在给定的示例中,假设您有一个用户空间应用程序轮询您的驱动程序,如下所示。

   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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-02
    • 2022-08-07
    • 2010-10-01
    • 2019-02-20
    • 1970-01-01
    • 2012-07-17
    • 2011-02-06
    • 1970-01-01
    相关资源
    最近更新 更多