Asynchronously read from a file
#include <aio.h> int aio_read( struct aiocb * const aiocbptr ); int aio_read64( struct aiocb64 * const aiocbptr );
- A pointer to an asynchronous I/O control block of type aiocb or aiocb64 that defines how much data to read, and from where.
Use the -l c option to qcc to link against this library. This library is usually included automatically.
The aio_read() and aio_read64() functions asynchronously read aiocbptr->aio_nbytes from the file associated with aiocbptr->aio_fildes into the buffer pointed to by aiocbptr->aio_buf. The functions return when the read request has been initiated or queued to the file or device (even when the data can't be delivered immediately).
The asynchronous operation is submitted at the scheduling priority of the thread minus aiocbp->aio_reqprio.
You can pass the aiocbptr argument to aio_error() and aio_return() , to determine the error status and return status of the asynchronous operation while it's proceeding. If an error condition is encountered during queuing, the functions return without having initiated or queued the request. The requested operation takes place at the absolute position in the file as given by aio_offset, as if lseek() were called immediately prior to the operation with an offset equal to aio_offset and a whence of SEEK_SET. After a successful call to enqueue an asynchronous I/O operation, the value of the file offset for the file is unspecified.
These functions ignore the aiocbptr->aio_lio_opcode field.
If the buffer pointed to by aiocbptr->aio_buf or the control block pointed to by aiocbptr becomes an illegal address before the asynchronous I/O is completed, then the behavior is undefined.
Simultaneous asynchronous operations using the same aiocbptr produce undefined results.
If synchronized I/O is enabled on the file associated with aiocbptr->aio_fildes, these functions behave in accordance with the definitions of synchronized I/O data integrity completion and synchronized I/O file integrity completion.
If a system action changes the process memory space while an asynchronous I/O is outstanding to the address range being changed, the results of that action are undefined.
The following conditions may be detected synchronously at the time of the call to aio_read() or aio_read64(), or asynchronously. If any of these conditions are detected synchronously, these functions return -1 and set errno to the corresponding value. If any of these conditions are detected asynchronously, the return status of the asynchronous operation is set to -1, and the error status of the asynchronous operation is set to the corresponding value:
- The aiocbptr->aio_fildes argument isn't a valid file descriptor that's open for reading.
- The file offset value implied by aiocbptr->aio_offset would be invalid, aiocbptr->aio_reqprio isn't a valid value, or aiocbptr->aio_nbytes is invalid.
If aio_read() or aio_read64() successfully queues the I/O operation, but the operation is subsequently canceled or encounters an error, the return status of the asynchronous operation is one of the values normally returned by read() . In addition, the error status of the asynchronous operation is set to one of the error statuses normally set read(), or one of the following:
- The aiocbptr->aio_fildes isn't a valid file descriptor that's open for reading.
- The requested I/O was canceled before the I/O was completed, due to an explicit aio_cancel() , request.
- The file offset value implied by aiocbptr->aio_offset would be invalid.
The following condition may be detected synchronously or asynchronously:
- The file is a regular file, aiocbptr->aio_nbytes is greater than 0, and the starting offset in aiocbptr->aio_offset is before the end of the file and is at or beyond the offset maximum in the open file description associated with aiocbptr->aio_fildes.
0 if the I/O operation was successfully queued, or -1 if an error occurred (errno is set).
- The requested asynchronous I/O operation wasn't queued because of system resource limitations.
The first time you call an aio_* function, a thread pool is created, making your process multithreaded if it isn't already. The thread pool isn't destroyed until your process ends.