AIO_READ(2)
π AIO_READ(2) π β asynchronous read from a file (REALTIME)
okay bestie, welcome to the System Calls Manual β¨ this one is about aio_read, aio_read2, and aio_readv β asynchronous reads, REALTIME energy, letβs get into it π
π NAME
aio_read, aio_read2, aio_readv β asynchronous read from a file (REALTIME)
πΈ LIBRARY
Standard C Library (libc, -lc)
π¦ SYNOPSIS
#include <aio.h>
int
aio_read(struct aiocb *iocb);
int
aio_read2(struct aiocb *iocb, int flags);
#include <sys/uio.h>
int
aio_readv(struct aiocb *iocb);
β¨ DESCRIPTION
The aio_read(), aio_read2(), and aio_readv() system calls let the calling process read from the descriptor iocb->aio_fildes π«Ά The syscalls return immediately after the read request has been enqueued to the descriptor; the read may or may not have completed at the time the call returns. Itβs giving βI sent the request, donβt wait upβ energy.
For the aio_read() and aio_readv() calls, the read begins at the offset iocb->aio_offset.
The aio_read() call will read iocb->aio_nbytes into the buffer pointed to by iocb->aio_buf, whereas aio_readv() reads the data into the iocb->aio_iovcnt buffers specified by the members of the iocb->aio_iov array. For aio_readv() the iovec structure is defined in readv(2) π¦
The aio_read2() call takes the flags argument. If flags is passed as zero, the call behaves identically to aio_read(). The following flags can be specified by logical or:
AIO_OP2_FOFFSETβ the read occurs at the file descriptor offset, which is advanced by the operation as done by theread(2)syscall. Theiocb->aio_offsetfield is ignored πAIO_OP2_VECTOREDβ similar toaio_readv(), the read buffers are specified by theaiocb->aio_iovarray.
The iocb pointer may be subsequently used as an argument to aio_return() and aio_error() in order to determine return or error status for the enqueued operation while it is in progress.
If the request could not be enqueued (generally due to invalid arguments), then the call returns without having enqueued the request.
If the request is successfully enqueued, the value of iocb->aio_offset can be modified during the request as context, so this value must not be referenced after the request is enqueued. No cap, donβt touch it after enqueueing π
The iocb->aio_sigevent structure can be used to request notification of the operationβs completion as described in aio(4).
π RESTRICTIONS
Bestie, these are the hard rules β the vibe check is strict:
- The Asynchronous I/O Control Block structure pointed to by
iocband the buffer that theiocb->aio_bufmember of that structure references must remain valid until the operation has completed. - The asynchronous I/O control buffer
iocbshould be zeroed before the system calls to avoid passing bogus context information to the kernel. - Modifications of the Asynchronous I/O Control Block structure or the buffer contents are not allowed while the request is queued.
- If the file offset in
iocb->aio_offsetis past the offset maximum foriocb->aio_fildes, no I/O will occur.
π«Ά RETURN VALUES
The aio_read(), aio_read2(), and aio_readv() functions return the value 0 if successful; otherwise the value -1 is returned and the global variable errno is set to indicate the error.
π DIAGNOSTICS
None.
Thatβs it. No diagnostics. The section is literally just None π
π ERRORS
The aio_read(), aio_read2(), and aio_readv() system calls will fail if:
[EAGAIN]β the request was not queued because of system resource limitations.[EFAULT]β part ofaio_iovpoints outside the processβs allocated address space.[EINVAL]β the asynchronous notification method iniocb->aio_sigevent.sigev_notifyis invalid or not supported.[EOPNOTSUPP]β asynchronous read operations on the file descriptoriocb->aio_fildesare unsafe and unsafe asynchronous I/O operations are disabled.
The following conditions may be synchronously detected when the aio_read() or aio_readv() system call is made, or asynchronously, at any time thereafter. If they are detected at call time, the calls return -1 and set errno appropriately; otherwise the aio_return() system call must be called, and will return -1, and aio_error() must be called to determine the actual value that would have been returned in errno.
[EBADF]β theiocb->aio_fildesargument is invalid.[EINVAL]β the offsetiocb->aio_offsetis not valid, the priority specified byiocb->aio_reqpriois not a valid priority, or the number of bytes specified byiocb->aio_nbytesis not valid.[EOVERFLOW]β the file is a regular file,iocb->aio_nbytesis greater than zero, the starting offset iniocb->aio_offsetis before the end of the file, but is at or beyond theiocb->aio_fildesoffset maximum.
If the request is successfully enqueued, but subsequently cancelled or an error occurs, the value returned by the aio_return() system call is per the read(2) system call, and the value returned by the aio_error() system call is either one of the error returns from the read(2) system call, or one of:
[EBADF]β theiocb->aio_fildesargument is invalid for reading.[ECANCELED]β the request was explicitly cancelled via a call toaio_cancel().[EINVAL]β the offsetiocb->aio_offsetwould be invalid.
π₯ SEE ALSO
aio_cancel(2)aio_error(2)aio_return(2)aio_suspend(2)aio_waitcomplete(2)aio_write(2)sigevent(3)siginfo(3)aio(4)
π STANDARDS
The aio_read() system call is expected to conform to the IEEE Std 1003.1 (βPOSIX.1β) standard. The aio_read2() and aio_readv() system calls are FreeBSD extensions, and should not be used in portable code. Periodt β keep those out of your portable stuff π
π HISTORY
The aio_read() system call first appeared in FreeBSD 3.0. The aio_readv() system call first appeared in FreeBSD 13.0. The aio_read2() system call first appeared in FreeBSD 14.1. Iconic timeline, honestly β¨
π AUTHORS
This manual page was written by Terry Lambert <terry@whistle.com>.
π§ BUGS
Invalid information in iocb->_aiocb_private may confuse the kernel. Like, the kernel sees nonsense and goes ??? π
Sign-off vibe check: this page is from Debian, dated March 27, 2025, and itβs the AIO_READ(2) manual page. Byeee, go read asynchronously πβ¨
π¦ where this page comes from
- freebsd 14.5: base 14.5
- freebsd 15.1: base 15.1
- Copyright (c) 1998 Terry Lambert
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms