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

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

相关文章

/etc/sysctl.conf这个目录主要是配置一些系统信息,/etc/sys...
1.作用 useradd或adduser命令用来建立用户帐号和创建用户的起...
它们都是多模式编辑器,不同的是vim 是vi的升级版本,它不仅...
不管是我们在安装软件还是监测软件的使用性能,我们都要随时...
装好Tomcat7后,发现除了本机能访问外界访问不了,岂有此理。...
修改防火墙配置需要修改 /etc/sysconfig/iptables 这个文件,...