加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

linux – 为什么我们需要在poll中调用poll_wait?

发布时间:2020-12-14 02:21:28 所属栏目:Linux 来源:网络整理
导读:在LDD3中,我看到了这样的代码 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 behi
在LDD3中,我看到了这样的代码
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将您的设备(由“struct file”表示)添加到可以唤醒进程的列表中.

想法是该进程可以使用poll(或select或epoll等)将一堆文件描述符添加到它希望等待的列表中.调用每个驱动程序的poll条目.每个人都将自己(通过poll_wait)添加到服务员列表中.

然后核心内核在一个地方阻止进程.这样,任何一个设备都可以唤醒进程.如果返回非零掩码位,则表示现在适用那些“就绪”属性(可读/可写/等).

所以,在伪代码中,它大致是这样的:

foreach fd:
    find device corresponding to fd
    call device poll function to setup wait queues (with poll_wait) and to collect its "ready-now" mask

while time remaining in timeout and no devices are ready:
    sleep

return from system call (either due to timeout or to ready devices)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读