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:

  1. In the example, the IC_AIO_DATA_INIT is 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;}
  2. The file descriptor fd is obtained as:
    HANDLE fd = CreateFile("dat",
       GENERIC_READ | GENERIC_WRITE,
       FILE_SHARE_READ,
       NULL,
       OPEN_ALWAYS,
       FILE_ATTRIBUTE_NORMAL/*|FILE_FLAG_OVERLAPPED*/,
       NULL);
  3. The aio_lio_opcode refers to the field of each aiocb structure that specifies the operation to be performed. The supported operations are LIO_READ (do a 'read' operation), LIO_WRITE (do a 'write' operation), and LIO_NOP (do no operation); these symbols are defined in <aio.h>.

See Also

Para obtener información más completa sobre las optimizaciones del compilador, consulte nuestro Aviso de optimización.