aio (7) - Linux Manuals
aio: POSIX asynchronous I/O overview
NAME
aio - POSIX asynchronous I/O overview
DESCRIPTION
The POSIX asynchronous I/O (AIO) interface allows applications to initiate one or more I/O operations that are performed asynchronously (i.e., in the background). The application can elect to be notified of completion of the I/O operation in a variety of ways: by delivery of a signal, by instantiation of a thread, or no notification at all.The POSIX AIO interface consists of the following functions:
- aio_read(3)
- Enqueue a read request. This is the asynchronous analog of read(2).
- aio_write(3)
- Enqueue a write request. This is the asynchronous analog of write(2).
- aio_fsync(3)
- Enqueue a sync request for the I/O operations on a file descriptor. This is the asynchronous analog of fsync(2) and fdatasync(2).
- aio_error(3)
- Obtain the error status of an enqueued I/O request.
- aio_return(3)
- Obtain the return status of a completed I/O request.
- aio_suspend(3)
- Suspend the caller until one or more of a specified set of I/O requests completes.
- aio_cancel(3)
- Attempt to cancel outstanding I/O requests on a specified file descriptor.
- lio_listio(3)
- Enqueue multiple I/O requests using a single function call.
The aiocb ("asynchronous I/O control block") structure defines parameters that control an I/O operation. An argument of this type is employed with all of the functions listed above. This structure has the following form:
#include <aiocb.h>
struct aiocb {
/* Operation codes for 'aio_lio_opcode': */
enum { LIO_READ, LIO_WRITE, LIO_NOP };
The fields of this structure are as follows:
In addition to the standard functions listed above,
the GNU C library provides the following extension to the POSIX AIO API:
Simultaneous asynchronous read or write operations using the same
aiocb
structure yield undefined results.
The current Linux POSIX AIO implementation is provided in user space by glibc.
This has a number of limitations, most notably that maintaining multiple
threads to perform I/O operations is expensive and scales poorly.
Work has been in progress for some time on a kernel
state-machine-based implementation of asynchronous I/O
(see
io_submit(2),
io_setup(2),
io_cancel(2),
io_destroy(2),
io_getevents(2)),
but this implementation hasn't yet matured to the point where
the POSIX AIO implementation can be completely
reimplemented using the kernel system calls.
The
SIGQUIT
signal (generated by typing control-\) causes the program to request
cancellation of each of the outstanding requests using
aio_cancel(3).
Here is an example of what we might see when running this program.
In this example, the program queues two requests to standard input,
and these are satisfied by two lines of input containing
"abc" and "x".
$ ./a.out /dev/stdin /dev/stdin
opened /dev/stdin on descriptor 3
opened /dev/stdin on descriptor 4
aio_error():
#define BUF_SIZE 20 /* Size of buffers for read operations */
#define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); } while (0)
struct ioRequest { /* Application-defined structure for tracking
static volatile sig_atomic_t gotSIGQUIT = 0;
ERRORS
VERSIONS
The POSIX AIO interfaces are provided by glibc since version 2.1.
CONFORMING TO
POSIX.1-2001, POSIX.1-2008.
NOTES
It is a good idea to zero out the control block buffer before use (see
memset(3)).
The control block buffer and the buffer pointed to by
aio_buf
must not be changed while the I/O operation is in progress.
These buffers must remain valid until the I/O operation completes.
EXAMPLES
The program below opens each of the files named in its command-line
arguments and queues a request on the resulting file descriptor using
aio_read(3).
The program then loops,
periodically monitoring each of the I/O operations
that is still in progress using
aio_error(3).
Each of the I/O requests is set up to provide notification by delivery
of a signal.
After all I/O requests have completed,
the program retrieves their status using
aio_return(3).
Program source
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <aio.h>
#include <signal.h>