Intel® oneAPI DPC++/C++ Compiler Developer Guide and Reference

ID 767253
Date 3/22/2024
Public
Document Table of Contents

Example for aio_error and aio_return Functions

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

// icx aio_sample4.c 
// aio_sample4.exe

#include <aio.h>
#include <stdio.h>
typedef struct aiocb  aiocb_t;
aiocb_t               my_aio;

#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;}

int main()
{
	static struct aiocb aio;
	static struct aiocb *aio_list[] = {&aio};
	int    ret;
	char  *dat = "Hello from Ex-3\n";
	 
	
	HANDLE fd  = CreateFile("dat",
	  GENERIC_READ | GENERIC_WRITE,
	  FILE_SHARE_READ,
	  NULL,
	  OPEN_ALWAYS,
	  FILE_ATTRIBUTE_NORMAL,
	  NULL);
	  
	/* Data initialization and asynchronously writing */
	IC_AIO_DATA_INIT(aio, fd, dat, strlen(dat), 0);
	 aio_write(&aio) ;

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

Execution Result

> ./a.out
ERRNO=996 STR=Unknown error
>  type dat
Hello from Ex-3