aio_write or aio_write64 Subroutine

The aio_write subroutine includes information for the POSIX AIO aio_write subroutine (as defined in the IEEE std 1003.1-2001), and the Legacy AIO aio_write subroutine.

POSIX AIO aio_write Subroutine

Purpose

Asynchronously writes to a file.

Library

Standard C Library (libc.a)

Syntax

#include <aio.h>

int aio_write (aiocbp)
struct aiocb *aiocbp;

Description

The aio_write subroutine writes aio_nbytes to the file associated with aio_fildes from the buffer pointed to by aio_buf. The subroutine returns when the write request has been initiated or queued to the file or device.

The aiocbp parameter may be used as an argument to the aio_error and aio_return subroutines in order to determine the error status and return status, respectively, of the asynchronous operation while it is proceeding.

The aiocbp parameter points to an aiocb structure. If the buffer pointed to by aio_buf or the control block pointed to by aiocbp becomes an illegal address prior to asynchronous I/O completion, the behavior is undefined.

If O_APPEND is not set for the aio_fildes file descriptor, the requested operation takes place at the absolute position in the file as given by aio_offset. This is done as if the lseek subroutine were called immediately prior to the operation with an offset equal to aio_offset and a whence equal to 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. After a successful call to enqueue an asynchronous I/O operation, the value of the file offset for the file is unspecified.

The aio_lio_opcode field is ignored by the aio_write subroutine.

If prioritized I/O is supported for this file, the asynchronous operation is submitted at a priority equal to the scheduling priority of the process minus aiocbp->aio_reqprio.

Simultaneous asynchronous operations using the same aiocbp produce undefined results.

If synchronized I/O is enabled on the file associated with aio_fildes, the behavior of this subroutine is according to the definitions of synchronized I/O data integrity completion, and synchronized I/O file integrity completion.

For any system action that changes the process memory space while an asynchronous I/O is outstanding, the result of that action is undefined.

For regular files, no data transfer occurs past the offset maximum established in the open file description associated with aio_fildes.

If you use the aio_write or aio_write64subroutine with a file descriptor obtained from a call to the shm_open subroutine, it will fail with EINVAL.

Parameters

Item Description
aiocbp Points to the aiocb structure associated with the I/O operation.

aiocb Structure

The aiocb structure is defined in the /usr/include/aio.h file and contains the following members:
int               aio_fildes
off_t             aio_offset
char             *aio_buf
size_t            aio_nbytes
int               aio_reqprio
struct sigevent   aio_sigevent
int               aio_lio_opcode

Execution Environment

The aio_write and aio_write64 subroutines can be called from the process environment only.

Return Values

The aio_write subroutine returns a 0 to the calling process if the I/O operation is successfully queued. Otherwise, a -1 is returned and the errno global variable is set to indicate the error.

Error Codes

Item Description
EAGAIN The requested asynchronous I/O operation was not queued due to system resource limitations.
Each of the following conditions may be detected synchronously at the time of the call to aio_write, or asynchronously. If any of the conditions below are detected synchronously, the aio_write subroutine returns a -1 and sets the errno global variable to the corresponding value. If any of the conditions below 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.
Item Description
EBADF The aio_fildes parameter is not a valid file descriptor open for writing.
EINVAL The file offset value implied by aio_offset is invalid, aio_reqprio is an invalid value, or aio_nbytes is an invalid value. The aio_write or aio_write64 subroutine was used with a file descriptor obtained from a call to the shm_open subroutine.
If the aio_write subroutine successfully queues the I/O operation, the return status of the asynchronous operation is one of the values normally returned by the write subroutine call. If the operation is successfully queued but is subsequently canceled or encounters an error, the error status for the asynchronous operation contains one of the values normally set by the write subroutine call, or one of the following:
Item Description
EBADF The aio_fildes parameter is not a valid file descriptor open for writing.
EINVAL The file offset value implied by aio_offset would be invalid.
ECANCELED The requested I/O was canceled before the I/O completed due to an aio_cancel request.
The following condition may be detected synchronously or asynchronously:
Item Description
EFBIG The file is a regular file, aio_nbytes is greater than 0, and the starting offset in aio_offset is at or beyond the offset maximum in the open file description associated with aio_fildes.

Purpose

Legacy AIO aio_write Subroutine

Writes to a file asynchronously.

Library

Standard C Library (libc.a)

Syntax

#include <aio.h>

int aio_write( FileDescriptor,  aiocbp)
int FileDescriptor;
struct aiocb *aiocbp;

int aio_write64( FileDescriptor,  aiocbp)
int FileDescriptor;
struct aiocb64 *aiocbp;

Description

The aio_write subroutine writes asynchronously to a file. Specifically, the aio_write subroutine writes to the file associated with the FileDescriptor parameter from a buffer. To handle this, the subroutine uses information from the aio control block (aiocb) structure, which is pointed to by the aiocbp parameter. This information includes the following fields:

Item Description
aio_buf Indicates the buffer to use.
aio_nbytes Indicates the number of bytes to write.

The aio_write64 subroutine is similar to the aio_write subroutine except that it takes an aiocb64 reference parameter. This allows the aio_write64 subroutine to specify offsets in excess of OFF_MAX (2 gigbytes minus 1).

In the large file enabled programming environment, aio_read is redefined to be aio_read64.

If you use the aio_write or aio_write64 subroutine with a file descriptor obtained from a call to the shm_open subroutine, it will fail with EINVAL.

When the write request has been queued, the aio_write subroutine updates the file pointer specified by the aio_whence and aio_offset fields in the aiocb structure as if the requested I/O completed. It then returns to the calling program. The aio_whence and aio_offset fields have the same meaning as the whence and offset parameters in the lseek (lseek, llseek or lseek64 Subroutine) subroutine. The subroutine ignores them for file objects that are not capable of seeking.

If an error occurs during the call, the write request is not initiated or queued. To determine the status of a request, use the aio_error (aio_error or aio_error64 Subroutine) subroutine.

To have the calling process receive the SIGIO signal when the I/O operation completes, set the AIO_SIGNAL bit in the aio_flag field in the aiocb structure.

Note: The event structure in the aiocb structure is currently not in use but is included for future compatibility.
Note: The _AIO_AIX_SOURCE macro used in aio.h must be defined when using aio.h to compile an aio application with the Legacy AIO function definitions. The default compile using the aio.h file is for an application with the POSIX AIO definitions. In the source file enter:
#define _AIO_AIX_SOURCE
#include <sys/aio.h>
or, on the command line when compiling enter:
->xlc ... -D_AIO_AIX_SOURCE ... legacy_aio_program.c 

Since prioritized I/O is not supported at this time, the aio_reqprio field of the structure is not presently used.

Parameters

Item Description
FileDescriptor Identifies the object to be written as returned from a call to open.
aiocbp Points to the asynchronous I/O control block structure associated with the I/O operation.

aiocb Structure

The aiocb structure is defined in the /usr/include/aio.h file and contains the following members:
struct aiocb
{
       int                 aio_whence;
       off_t               aio_offset;
       char                *aio_buf;
       ssize_t             aio_return;
       int                 aio_errno;
       size_t              aio_nbytes;
       union {
              int          reqprio;
              struct {
                     int   version:8;
                     int   priority:8;
                     int   cache_hint:16;
              } ext;
       } aio_u1;
       int                 aio_flag;
       int                 aio_iocpfd;
       aio_handle_t        aio_handle;
}

#define aio_reqprio        aio_u1.reqprio
#define aio_version        aio_u1.ext.version
#define aio_priority       aio_u1.ext.priority
#define aio_cache_hint     aio_u1.ext.cache_hint

Execution Environment

The aio_write and aio_write64 subroutines can be called from the process environment only.

Return Values

When the write request queues successfully, the aio_write subroutine returns a value of 0. Otherwise, it returns a value of -1 and sets the errno global variable to identify the error.

Return codes can be set to the following errno values:

Item Description
EAGAIN Indicates that the system resources required to queue the request are not available. Specifically, the transmit queue may be full, or the maximum number of opens may have been reached.
EBADF Indicates that the FileDescriptor parameter is not valid.
EFAULT Indicates that the address specified by the aiocbp parameter is not valid.
EINVAL Indicates that the aio_whence field does not have a valid value or that the resulting pointer is not valid. The aio_write or aio_write64 subroutine was used with a file descriptor obtained from a call to the shm_open subroutine.

When using I/O Completion Ports with AIO Requests, return codes can also be set to the following errno values:

Item Description
EBADF Indicates that the aio_iocpfd field in the aiocb structure is not a valid I/O Completion Port file descriptor.
EINVAL Indicates that an I/O Completion Port service failed when attempting to start the AIO Request.
EPERM Indicates that I/O Completion Port services are not available.

Note: Other error codes defined in the /usr/include/sys/errno.h file may be returned by the aio_error subroutine if an error during the I/O operation is encountered.