Skip to content

Non-blocking I/O in Drivers

Purpose

Non-blocking I/O allows user space to request that a file operation return immediately instead of sleeping.

Driver Check

if (queue_empty(dev)) {
    if (file->f_flags & O_NONBLOCK)
        return -EAGAIN;

    return wait_event_interruptible(dev->wq, !queue_empty(dev));
}

Expected Behavior

Mode No Data Available
Blocking Sleep until data is available or interrupted
Non-blocking Return -EAGAIN

Common Pitfalls

Warning

Do not return 0 for temporary no-data conditions. User space may interpret it as EOF.

Warning

Keep .poll readiness and .read availability checks consistent.