Example for aio_error and aio_return Functions

The following example illustrates how the aio_error() and aio_return() functions can be used.

int aio_ex_3(HANDLE fd)
{
 static struct aiocb aio;
 static struct aiocb *aio_list[] = {&aio};
 int    ret;
 char  *dat = "Hello from Ex-3\n";
  
/* Data initialization and asynchronously writing */
 
 IC_AIO_DATA_INIT(aio, fd, dat, strlen(dat), 0);
 if (aio_write(& aio) == -1) return errno;
 
 ret = aio_error(&aio);
 if ( ret == EINPROGRESS ) {
 fprintf(stderr, "ERRNO=%d STR=%s\n", ret, strerror(ret));
 
 ret = aio_suspend(aio_list, 1, NULL);
 if (ret == -1) return errno;}
 else if (ret)
 return ret;
 
 ret = aio_error(&aio);
 if (ret) return ret;
 
 ret = aio_return(&aio);
 printf("ret=%d\n", ret);
 
 return 0;
}/* aio_ex_3 */

Result upon execution:

-bash-3.00$ ./a.out
ERRNO=115 STR=Operation now in progress
ret=16
-bash-3.00$ cat dat
Hello from Ex-3

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);

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