aio.h - Linux


aio.h defines functions and types for asynchronous input and output (AIO) operations. AIO allows applications to perform I/O operations without blocking, allowing for efficient handling of multiple and/or large I/O tasks.


#include <aio.h>


aio_mode (flags):

  • O_RDONLY: Open for reading only.
  • O_WRONLY: Open for writing only.
  • O_RDWR: Open for both reading and writing.
  • O_APPEND: Append data to the end of the file.
  • O_CREAT: Create a new file if it doesn’t exist.
  • O_TRUNC: Truncate the file to zero length.
  • O_EXCL: Fail if the file already exists.


Open a file for reading and perform an asynchronous read operation:

#include <stdio.h>
#include <aio.h>

int main() {
    aiocb aio;
    char buffer[1024];

    int fd = open("file.txt", O_RDONLY);
    if (fd == -1) {
        return 1;

    aio.aio_fildes = fd;
    aio.aio_buf = buffer;
    aio.aio_nbytes = 1024;
    aio.aio_offset = 0;

    int ret = aio_read(&aio);
    if (ret == -1) {
        return 1;

    // Check the status of the operation using aio_error()
    while (aio_error(&aio) == EINPROGRESS);

    if (aio_return(&aio) == -1) {
        return 1;

    // Read completed successfully, data is in the buffer
    return 0;

Common Issues

  • AIO operations can be interrupted: If the process is interrupted before an AIO operation completes, the operation will be canceled.
  • AIO operations can fail: If an error occurs during an AIO operation, the operation will be completed with an error status.


aio.h can be combined with other Linux commands and tools for advanced tasks. For example, it can be used with the flock() function to perform locking operations on files before performing AIO operations.

Related Commands