gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 00:05:33 27 4
gpt4 key购买 nike

在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 将您的设备(由“结构文件”表示)添加到可以唤醒进程的设备列表中。

这个想法是进程可以使用 poll(或 select 或 epoll 等)将一堆文件描述符添加到它希望等待的列表中。调用每个驱动程序的轮询条目。每个人都将自己(通过 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)

关于linux - 为什么我们需要在poll中调用poll_wait?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30234496/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com