0

When using POSIX AIO with a file descriptor, did the file descriptor need to be opened with O_NONBLOCK in open()?

In APUE, I don't find it explicitly say yes or no, but I don't find such a file descriptor is opened with O_NONBLOCK in open() in one example.

Thank.

Tim
  • 1
  • 141
  • 372
  • 590
  • 1
    What makes you think it should be? `O_NONBLOCK` is for non-blocking IO. Btw AIO is not supported by the Linux kernel. –  Aug 31 '18 at 20:23
  • The short answer is “No”. Do you need a long answer? – Jonathan Leffler Aug 31 '18 at 20:30
  • @JonathanLeffler Yes. I am not very clear about their relation. – Tim Aug 31 '18 at 20:36
  • @Ivan - Are you sure about the kernel lacking AIO? I can easily find man pages for it; for example, [AIO(7)](http://man7.org/linux/man-pages/man7/aio.7.html). It is under section 7 Miscellanea rather than section 3 Library Functions, which is a bit unusual. – jww Aug 31 '18 at 20:45
  • 2
    @jww Linux has `io_setup`, `io_getevents` etc. But those do not implement everything that is needed for POSIX AIO. `aio_*` functions are implemented in userspace and because of that they are non-conforming (not async-signal-safe, for example). –  Aug 31 '18 at 20:47
  • Thanks @Ivan. That sounds closer to what I expected after seeing your statement and the man page. – jww Aug 31 '18 at 20:48
  • Possible duplicate of [asynchronous vs non-blocking](https://stackoverflow.com/questions/2625493/asynchronous-vs-non-blocking) – jww Sep 01 '18 at 00:00

1 Answers1

3

Since you comment that you are unclear about the relation and distinction between non-blocking I/O and asynchronous I/O:

  • I/O operations on a file open in non-blocking mode do not block, even if no data can be transferred immediately. If they transfer less data than was requested (or none at all) then it is up to the caller to try again later if they wish. Nothing is queued for later action.

  • The POSIX AIO interface provides for I/O operations to be performed asynchronously with respect to the caller's thread. AIO calls return without waiting for the I/O, while that I/O is being attempted in a different execution context. The caller can arrange to be notified in various ways of the completion of the operation (or not). In the meantime, it can perform whatever other work it wants.

There is no particular relationship between these. Neither the POSIX specifications, such as those for aio_read(), nor the Linux manual for the POSIX AIO interfaces document any requirement that the file on which AIO is performed be in non-blocking mode, nor do they define any error condition for the case that it is in blocking mode. Non-blocking mode is not a requirement.

Indeed, although it is allowed, it would not even be particularly useful to perform AIO on a non-blocking file. If you can rely on your operation not to block, then what do you gain from performing it asynchronously? The main point of AIO is that the caller does not have to wait, but the I/O is performed.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157