为什么我们需要在民意调查中调用poll_wait? [英] Why do we need to call poll_wait in poll?
问题描述
在LDD3中,我看到了这样的代码
In LDD3, i saw such codes
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不会等待,将立即返回.那我们为什么要调用它呢?为什么我们不能只返回面具?
But it says poll_wait won't wait and will return immediately. Then why do we need to call it? Why can't we just return mask?
推荐答案
poll_wait将您的设备(由结构文件"表示)添加到可以唤醒进程的设备列表中.
poll_wait adds your device (represented by the "struct file") to the list of those that can wake the process up.
这个想法是,该进程可以使用民意调查(或select或epoll等)将一堆文件描述符添加到希望等待的列表中.每个驱动程序的轮询条目都将被调用.每个人(通过poll_wait)将自己添加到服务员列表中.
The idea is that the process can use poll (or select or epoll etc) to add a bunch of file descriptors to the list on which it wishes to wait. The poll entry for each driver gets called. Each one adds itself (via poll_wait) to the waiter list.
然后,核心内核将进程阻塞在一处.这样,任何一台设备都可以唤醒该过程.如果返回非零掩码位,则意味着那些就绪"属性(可读/可写等)适用于 now .
Then the core kernel blocks the process in one place. That way, any one of the devices can wake up the process. If you return non-zero mask bits, that means those "ready" attributes (readable/writable/etc) apply now.
因此,在伪代码中,大致是这样的:
So, in pseudo-code, it's roughly like this:
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)
这篇关于为什么我们需要在民意调查中调用poll_wait?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!