Mastering Linux Asynchronous I/O: Signals, AIO APIs, and Driver Integration
This article explains how Linux device drivers use signal‑driven asynchronous I/O to notify applications, details the required driver and user‑space code, and walks through the POSIX AIO API functions and their integration with kernel drivers.
Asynchronous Notification in Device Drivers
In device drivers, asynchronous notification lets the driver actively inform the application when a device becomes accessible, eliminating the need for polling. This mechanism works like a hardware interrupt and is often called "signal‑driven asynchronous I/O".
Linux Signal‑Based Async Notification
Linux implements async notification using signals. The prototype of signal is:
void (*signal(int signum, void (*handler))(int));It can be simplified to the typedef: typedef void (*sighandler_t)(int); and used as:
sighandler_t signal(int signum, sighandler_t handler);Implementing fasync in Drivers
To support async notification a driver must:
Include an fasync_struct * pointer in its device structure.
Implement a fasync method that forwards its arguments to fasync_helper:
static int xxx_fasync(int fd, struct file *filp, int mode) {
struct xxx_dev *dev = filp->private_data;
return fasync_helper(fd, filp, mode, &dev->async_queue);
}When the device becomes ready, call kill_fasync(&dev->async_queue, SIGIO, POLL_IN) (or POLL_OUT for write readiness). Finally, remove the file from the async list in the driver’s release method:
int xxx_release(struct inode *inode, struct file *filp) {
xxx_fasync(-1, filp, 0);
return 0;
}Linux 2.6 Asynchronous I/O (AIO)
Linux traditionally uses synchronous I/O, where a request blocks the calling process until completion. POSIX AIO provides non‑blocking operations that can overlap with other processes.
Key AIO functions:
aio_read – queue an asynchronous read.
aio_write – queue an asynchronous write.
aio_error – check the status of a request.
aio_return – obtain the return value after completion.
aio_suspend – block until one of a set of requests finishes.
aio_cancel – cancel pending requests.
lio_listio – launch multiple I/O operations with a single system call.
Using Signals for AIO Notification
Set up a signal handler with sigaction, configure the aiocb structure, and set aio_sigevent.sigev_notify = SIGEV_SIGNAL and sigev_signo = SIGIO. The handler checks aio_error and retrieves the result with aio_return.
void aio_completion_handler(int signo, siginfo_t *info, void *context) {
if (info->si_signo == SIGIO) {
struct aiocb *req = (struct aiocb *)info->si_value.sival_ptr;
if (aio_error(req) == 0) {
ssize_t ret = aio_return(req);
/* process ret */
}
}
}Using Callback Functions for AIO Notification
Alternatively, set aio_sigevent.sigev_notify = SIGEV_THREAD and provide a notify_function. The callback receives a sigval_t containing the aiocb pointer.
void aio_completion_handler(sigval_t sigval) {
struct aiocb *req = (struct aiocb *)sigval.sival_ptr;
if (aio_error(req) == 0) {
ssize_t ret = aio_return(req);
/* process ret */
}
}AIO Integration with Device Drivers
In the kernel each I/O request is represented by a kiocb. Character device drivers that support AIO must implement the following members in file_operations:
ssize_t (*aio_read)(struct kiocb *iocb, char *buffer, size_t count, loff_t offset);
ssize_t (*aio_write)(struct kiocb *iocb, const char *buffer, size_t count, loff_t offset);
int (*aio_fsync)(struct kiocb *iocb, int datasync);Block and network devices are inherently asynchronous, while character devices need explicit AIO support.
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.
