Understanding Blocking vs Non‑Blocking I/O in Linux Device Drivers
This article explains the concepts of blocking and non‑blocking I/O in Linux device drivers, compares their implementations, demonstrates how select and poll are used for asynchronous access, and provides sample driver code illustrating both approaches.
In Linux device drivers, blocking I/O suspends the calling process until the requested resource becomes available, while non‑blocking I/O returns immediately, allowing the application to poll or use select/poll system calls to check readiness.
Basic Concepts
Blocking operation : The process is put to sleep and removed from the scheduler until the condition is satisfied.
Non‑blocking operation : The call returns immediately; the application may repeatedly query the device or use select/poll to wait for readiness.
Polling Operations
Blocking read example:
char buf;
fd = open("/dev/ttyS1", O_RDWR);
...;
res = read(fd, &buf, 1); // returns only when input is available, otherwise the process sleeps
if (res == 1) {
printf("%c
", buf);
}Non‑blocking read example:
char buf;
fd = open("/dev/ttyS1", O_RDWR | O_NONBLOCK); // O_NONBLOCK flag
...;
while (read(fd, &buf, 1) != 1) {
// keep looping until data arrives
}
printf("%c
", buf);Non‑blocking I/O is usually driven by select() or poll() in user space, which eventually invoke the driver’s poll() method.
select() Prototype
int select(int numfds, fd_set *readfds, fd_set *writefds, fd_set *exceptionfds, struct timeval *timeout);Typical usage involves setting up file descriptor sets, calling select(), and handling the ready descriptors.
Driver poll() Function
The driver’s poll() function adds the device’s wait queues to the poll table and returns a mask indicating readable or writable status.
static unsigned int poll(struct file *file, struct socket *sock, poll_table *wait) {
unsigned int mask = 0;
struct xxx_dev *dev = file->private_data;
poll_wait(file, &dev->r_wait, wait); // add read queue
poll_wait(file, &dev->w_wait, wait); // add write queue
if (/* readable condition */)
mask |= POLLIN | POLLRDNORM;
if (/* writable condition */)
mask |= POLLOUT | POLLWRNORM;
return mask;
}globalfifo Driver poll() Example
static unsigned int globalfifo_poll(struct file *filp, poll_table *wait) {
unsigned int mask = 0;
struct globalfifo_dev *dev = filp->private_data;
down(&dev->sem);
poll_wait(filp, &dev->r_wait, wait);
poll_wait(filp, &dev->w_wait, wait);
if (dev->current_len != 0)
mask |= POLLIN | POLLRDNORM;
if (dev->current_len != GLOBALFIFO_SIZE)
mask |= POLLOUT | POLLWRNORM;
up(&dev->sem);
return mask;
}Summary
Blocking I/O uses wait queues and puts the process to sleep until the operation can proceed.
Non‑blocking I/O relies on polling mechanisms such as select() or poll(), which add the device’s wait queues to a poll table without blocking.
The driver’s poll() method is the bridge that reports readiness to user space.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
