View from Userspace:
- process can check if one or more files can be read from or
written to without blocking
- this allows to work with multiple file descriptors
- example: telnet: reads from socket and terminal, can't block on either!
- But why? We've got:
-
O_NONBLOCK, we can poll? -> yes, but this wastes CPU time!
- threads -> yes, but threads are considered problematic for many reasons
View from Kernelspace:
- driver:
- call
poll_wait on one or more waitqueues that inform of
change of poll=/=select status (typically read and write
queue)
- return a bitmask describing what operations can be performed
without blocking.
- kernel: if a requested operation is possible the kernel
returns, otherwise sets process to sleep on waitqueues and handles
timeouts.
API: poll
header and poll operation
#include <linux/poll.h>
unsigned int poll(struct file *filp, poll_table *wait);
step 1: add waitqueues
void poll_wait(struct file*, wait_queue_head_t *, poll_table *);
step 2: return appropriate status (more in asm/poll.h)
/* readable */
return (POLLIN | POLLRDNORM);
/* writeable */
return (POLLOUT | POLLWRNORM);