3

A program may first issues an asynchronous I/O operation with aio_read() or aio_write() and then call exit() to terminate itself.

...
aio_write(aiocbp);
exit(0);

My question is, will the exit() call wait until the async i/o is done or the program will directly terminate?

Naruil
  • 2,300
  • 11
  • 15

1 Answers1

4

I believe the relevant language in the standard is:

Consequences of Process Termination

All of the file descriptors, directory streams, conversion descriptors, and message catalog descriptors open in the calling process shall be closed.

Source: http://pubs.opengroup.org/onlinepubs/9699919799/functions/_Exit.html

and:

When there is an outstanding cancelable asynchronous I/O operation against fildes when close() is called, that I/O operation may be canceled. An I/O operation that is not canceled completes as if the close() operation had not yet occurred. All operations that are not canceled shall complete as if the close() blocked until the operations completed. The close() operation itself need not block awaiting such I/O completion. Whether any I/O operation is canceled, and which I/O operation may be canceled upon close(), is implementation-defined.

Source: http://pubs.opengroup.org/onlinepubs/9699919799/functions/close.html

So, it's unspecified; either the unfinished operations are cancelled, or the operation blocks until they finish.

Community
  • 1
  • 1
R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
  • 1
    If one implemented an `atexit()` handler that explicitly called `close()` on the descriptors with pending async I/O operations, then checked each async I/O status with `aio_error()`, it seems it is always permitted then that some return `ECANCELED` and others return success. I would imagine then, that the standard intends the implementation to do a best effort cancel, but acknowledges a race condition that the implementation need not try to fix. – jxh Jun 04 '13 at 03:21
  • This is a very useful explanation. – Ram Rajamony Jun 04 '13 at 13:24
  • Note that one point of confusion is that the first citation I provided says "shall be closed", not "shall be closed as if by `close`", leaving a bit of ambiguity. If I'm not mistaken, the Austin Group plans to clarify this issue at some point, but I don't have the reference handy. – R.. GitHub STOP HELPING ICE Jun 04 '13 at 14:45