Updated: April 19, 2023 |
Asynchronously write to a file
#include <aio.h> int aio_write( struct aiocb * aiocbptr ); int aio_write64( struct aiocb64 * aiocbptr );
libc
Use the -l c option to qcc to link against this library. This library is usually included automatically.
The aio_write() function asynchronously writes aiocbptr->aio_nbytes to the file associated with aiocbptr->aio_filedes from the buffer that aiocbptr->aio_buf points to. The function returns when the write request has been initiated or, at a minimum, queued to the file or device. The aio_write64() function is a large-file support version of aio_write().
The asynchronous operation is submitted at the scheduling priority of the thread minus aiocbptr->aio_reqprio.
You can use the aiocbptr argument as an argument to aio_error() and aio_return() to determine the error status and return status of the asynchronous operation while it's in progress.
If O_APPEND isn't set for the file descriptor aio_filedes, then the requested operation takes place at the absolute position in the file as given by aio_offset, as if lseek() were called immediately before the operation with an offset of aio_offset and a whence of SEEK_SET.
If O_APPEND is set for the file descriptor, write operations append to the file in the same order as the calls were made.
If synchronized I/O is enabled on the file associated with aiocbptr->aio_filedes, these functions behave in accordance with the definitions of synchronized I/O data integrity completion and synchronized I/O file integrity completion.
The following conditions may be detected synchronously at the time of the call to aio_write() or aio_write64(), 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:
If aio_write() or aio_write64() 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 write(). In addition, the error status of the asynchronous operation is set to one of the error statuses normally set by write(), or one of the following:
The following condition may be detected synchronously or asynchronously:
0 if the I/O operation was successfully queued, or -1 if an error occurred (errno is set).
aio_write() is POSIX 1003.1; aio_write64() is Large-file support
Safety: | |
---|---|
Cancellation point | No |
Interrupt handler | No |
Signal handler | Yes |
Thread | Yes |
The first time you call an aio_* function, a thread pool is created, making your process multithreaded if it isn't already. Because of this, after a fork() the child can not use any of the aio_*() functions if the parent used any aio_*() functions before the fork(). The thread pool isn't destroyed until your process ends.