Next: Cancel AIO Operations, Previous: Status of AIO Operations, Up: Asynchronous I/O
When dealing with asynchronous operations it is sometimes necessary to get into a consistent state. This would mean for AIO that one wants to know whether a certain request or a group of request were processed. This could be done by waiting for the notification sent by the system after the operation terminated, but this sometimes would mean wasting resources (mainly computation time). Instead POSIX.1b defines two functions which will help with most kinds of consistency.
The aio_fsync and aio_fsync64 functions are only available
if the symbol _POSIX_SYNCHRONIZED_IO is defined in unistd.h.
Calling this function forces all I/O operations operating queued at the time of the function call operating on the file descriptor
aiocbp->aio_fildesinto the synchronized I/O completion state (see Synchronizing I/O). Theaio_fsyncfunction returns immediately but the notification through the method described inaiocbp->aio_sigeventwill happen only after all requests for this file descriptor have terminated and the file is synchronized. This also means that requests for this very same file descriptor which are queued after the synchronization request are not affected.If op is
O_DSYNCthe synchronization happens as with a call tofdatasync. Otherwise op should beO_SYNCand the synchronization happens as withfsync.As long as the synchronization has not happened, a call to
aio_errorwith the reference to the object pointed to by aiocbp returnsEINPROGRESS. Once the synchronization is doneaio_errorreturn 0 if the synchronization was not successful. Otherwise the value returned is the value to which thefsyncorfdatasyncfunction would have set theerrnovariable. In this case nothing can be assumed about the consistency for the data written to this file descriptor.The return value of this function is 0 if the request was successfully enqueued. Otherwise the return value is -1 and
errnois set to one of the following values:
EAGAIN- The request could not be enqueued due to temporary lack of resources.
EBADF- The file descriptor
aiocbp->aio_fildesis not valid or not open for writing.EINVAL- The implementation does not support I/O synchronization or the op parameter is other than
O_DSYNCandO_SYNC.ENOSYS- This function is not implemented.
When the sources are compiled with
_FILE_OFFSET_BITS == 64this function is in factaio_fsync64since the LFS interface transparently replaces the normal implementation.
This function is similar to
aio_fsyncwith the only difference that the argument is a reference to a variable of typestruct aiocb64.When the sources are compiled with
_FILE_OFFSET_BITS == 64this function is available under the nameaio_fsyncand so transparently replaces the interface for small files on 32 bit machines.
Another method of synchronization is to wait until one or more requests of a
specific set terminated. This could be achieved by the aio_*
functions to notify the initiating process about the termination but in
some situations this is not the ideal solution. In a program which
constantly updates clients somehow connected to the server it is not
always the best solution to go round robin since some connections might
be slow. On the other hand letting the aio_* function notify the
caller might also be not the best solution since whenever the process
works on preparing data for on client it makes no sense to be
interrupted by a notification since the new client will not be handled
before the current client is served. For situations like this
aio_suspend should be used.
When calling this function, the calling thread is suspended until at least one of the requests pointed to by the nent elements of the array list has completed. If any of the requests has already completed at the time
aio_suspendis called, the function returns immediately. Whether a request has terminated or not is determined by comparing the error status of the request withEINPROGRESS. If an element of list isNULL, the entry is simply ignored.If no request has finished, the calling process is suspended. If timeout is
NULL, the process is not woken until a request has finished. If timeout is notNULL, the process remains suspended at least as long as specified in timeout. In this case,aio_suspendreturns with an error.The return value of the function is 0 if one or more requests from the list have terminated. Otherwise the function returns -1 and
errnois set to one of the following values:
EAGAIN- None of the requests from the list completed in the time specified by timeout.
EINTR- A signal interrupted the
aio_suspendfunction. This signal might also be sent by the AIO implementation while signalling the termination of one of the requests.ENOSYS- The
aio_suspendfunction is not implemented.When the sources are compiled with
_FILE_OFFSET_BITS == 64this function is in factaio_suspend64since the LFS interface transparently replaces the normal implementation.
This function is similar to
aio_suspendwith the only difference that the argument is a reference to a variable of typestruct aiocb64.When the sources are compiled with
_FILE_OFFSET_BITS == 64this function is available under the nameaio_suspendand so transparently replaces the interface for small files on 32 bit machines.