ptg10805159
514 Advanced I/O Chapter 14
We use asynchronous I/O when we have other processing to do and we don’t want
to block while performing the I/O operation. However,when we have completed the
processing and find that we still have asynchronous operations outstanding, we can call
theaio_suspendfunction to block until an operation completes.
#include <aio.h>
int aio_suspend(const struct aiocb *constlist[], intnent,
const struct timespec *timeout);
Returns: 0 if OK,−1 on error
One of three things can causeaio_suspendto return. If we areinterrupted by a
signal, it returns−1witherrnoset toEINTR.Ifthe time limit specified by the optional
timeout argument expires without any of the I/O operations completing, then
aio_suspendreturns−1witherrnoset toEAGAIN(we can pass a null pointer for the
timeoutargument if we want to block without a time limit). If any of the I/O operations
complete,aio_suspendreturns 0. If all asynchronous I/O operations arecomplete
when we callaio_suspend,thenaio_suspendwill return without blocking.
Thelistargument is a pointer to an array of AIO control blocks and thenent
argument indicates the number of entries in the array.Null pointers in the array are
skipped; the other entries must point to AIO control blocks that have been used to
initiate asynchronous I/O operations.
When we have pending asynchronous I/O operations that we no longer want to
complete, we can attempt to cancel them with theaio_cancelfunction.
#include <aio.h>
int aio_cancel(intfd,struct aiocb *aiocb);
Returns: (see following)
Thefdargument specifies the file descriptor with the outstanding asynchronous I/O
operations. If theaiocb argument isNULL,then the system attempts to cancel all
outstanding asynchronous I/O operations on the file. Otherwise, the system attempts
to cancel the single asynchronous I/O operation described by the AIO control block.
We say that the system ‘‘attempts’’ to cancel the operations, because there is no
guarantee that the system will be able to cancel any operations that are in progress.
Theaio_cancelfunction can return one of four values:
AIO_ALLDONE All of the operations completed beforethe attempt to cancel
them.
AIO_CANCELED All of the requested operations have been canceled.
AIO_NOTCANCELED At least one of the requested operations could not be
canceled.
−1The call toaio_cancelfailed. The error code will be stored
inerrno.