Why do we need to call poll_wait in a poll?

In LDD3 I saw these 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; } 

But he says poll_wait will not wait and will return immediately. Then why should we call it? Why can't we get the mask back?

+7
linux linux-kernel system-calls linux-device-driver epoll
source share
3 answers

poll_wait adds your device (represented by a "structure file") to the list of those that may wake up the process.

The idea is that a process can use polling (either select or epoll, etc.) to add a bunch of file descriptors to the list it wants to wait on. A record is requested for each driver. Everyone adds himself (through poll_wait) to the list of waiters.

Then the main core blocks the process in one place. Thus, any of the devices can wake up the process. If you return non-zero bits of the mask, it means that those “ready-made” attributes (readable / writable / etc) are now applied.

So, in pseudo code, it is something 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) 
+11
source share

poll file_operation falls asleep if you return 0

That's what confused me.

When you return a nonzero value, it means that some event has been fired and it wakes up.

As soon as you see this, it’s clear that something must connect the process to the wait queue, and this is a poll_wait thing.

Also remember that a struct file represents a “connection between a process and an open file”, not just a file system file, and as such it contains a pid that is used to identify the process.

Reproduction using a minimal executable example may also help fix the problem: fooobar.com/questions/552185 / ...

+2
source share

poll_wait triggers when an expected event has occurred on any of the fd that it expects, or it expires a timeout.

Check the mask to see what event caused poll_wait. If you do not want poll_wait to fire on such an event, you can configure it when registering a file descriptor for polling fd.

-one
source share

All Articles