Developer Guide and Reference

ID 767253
Date 10/31/2024
Public
Document Table of Contents

Example for aio_cancel Function

The following example illustrates how the aio_cancel() function can be used.

// icx aio_sample5.c
// aio_sample5.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-4\n";
	
	HANDLE fd = CreateFile("dat",
	  GENERIC_READ | GENERIC_WRITE,
	  FILE_SHARE_READ,
	  NULL,
	  OPEN_ALWAYS,
	  FILE_ATTRIBUTE_NORMAL,
	  NULL);

	printf("AIO_CANCELED=%d AIO_NOTCANCELED=%d\n", AIO_CANCELED,   AIO_NOTCANCELED);
	 
	/* Data initialization and asynchronously writing */
	 
	IC_AIO_DATA_INIT(aio, fd, dat, strlen(dat), 0);
	if (aio_write(&aio) == -1) return errno;
	 
	ret = aio_cancel(fd, &aio);
	if ( ret == AIO_CANCELED ) fprintf(stderr, "1 ERRNO=%d STR=%s\n", ret, strerror(ret));
	else if (ret) return ret;

	ret = aio_cancel(fd, &aio);
	if ( ret == AIO_NOTCANCELED ) {
		fprintf(stderr, "2 ERRNO=%d STR=%s\n", ret, strerror(ret));
		ret = aio_suspend(aio_list, 1, NULL);
		if (ret == -1) return errno;

	}

	return 0;
}

Execution Result

> aio_sample5.exe
AIO_CANCELED=1 AIO_NOTCANCELED=2
1 ERRNO=1 STR=Operation not permitted
> type dat
Hello from Ex-4

See Also