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