Example for lio_listio Function
The following example illustrates how the
lio_
listio()
function can be used.
int aio_ex_5(HANDLE fd)
{
static struct aiocb aio[2];
static struct aiocb *aio_list[2] = {&aio[0], &aio[1]};
int i, ret;
/*
** Data initialization and Synchronously writing
*/
IC_AIO_DATA_INIT(aio[0], fd, "rec#1\n", strlen("rec#1\n"), 0)
IC_AIO_DATA_INIT(aio[1], fd, "rec#2\n", strlen("rec#2\n"),
aio[0].aio_nbytes)
aio[0].aio_lio_opcode = aio[1].aio_lio_opcode = LIO_WRITE;
ret = lio_listio(LIO_WAIT, aio_list, 2, 0);
if (ret) return ret;
return 0;
}/* aio_ex_5 */
Result upon execution:
-bash-3.00$ ./a.out
-bash-3.00$ cat dat
rec#1
rec#2
-bash-3.00$
Remarks:
- In the example, theIC_AIO_DATA_INITis defined as follows:#define IC_AIO_DATA_INIT(_aio, _fd, _dat, _len, _off)\ {memset(&_aio, 0, sizeof(_aio)); \ _aio.aio_fildes = _fd; \ _aio.aio_buf = _dat; \ _aio.aio_nbytes = _len; \ _aio.aio_offset = _off;}
- The file descriptorfdis obtained as:HANDLE fd = CreateFile("dat", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL/*|FILE_FLAG_OVERLAPPED*/, NULL);
- Theaio_lio_opcoderefers to the field of eachaiocbstructure that specifies the operation to be performed. The supported operations areLIO_READ(do a 'read' operation),LIO_WRITE(do a 'write' operation), andLIO_NOP(do no operation); these symbols are defined in<aio.h>.