1

When I compile this code in OS X:

#include <unistd.h>
#include <iostream>
#include <memory>
#include <aio.h>
#include <sys/stat.h>
#include <fcntl.h>
using namespace std;

int main(int, char*[])
{
    const int iBufSize = 1<<20;
    unique_ptr<char[]> pBuffer(new char[iBufSize]);
    int iRet;

    int iOpts = fcntl(STDIN_FILENO, F_GETFL);
    cout << "GETFL Before: " << iOpts << endl;
    fcntl(STDIN_FILENO, F_SETFL, iOpts & ~O_NONBLOCK);
    iOpts = fcntl(STDIN_FILENO, F_GETFL);
    cout << "GETFL After: " << iOpts << endl;

    aiocb cb = {0};
    cb.aio_fildes = STDIN_FILENO;
    cb.aio_buf = pBuffer.get();
    cb.aio_nbytes = iBufSize;
    iRet = aio_read(&cb);
    cout << "aio_read retuned " << iRet << endl;
    cout << "errno = " << strerror(errno) << "(" << errno << ")" << endl;
    return 0;
}

compile it with g++, then execute it like so:

echo "Hello" | ./aio_read\ demo

it returns:

GETFL Before: 0
GETFL After: 0
aio_read retuned -1
errno = Resource temporarily unavailable(35)

Why? If I run it without the preceding echo "Hello" | it works as expected. Also, the fcntl calls aren't required to cause the problem but they do show that STDIN does not have O_NONBLOCK set.

pilcrow
  • 56,591
  • 13
  • 94
  • 135
Thendore
  • 11
  • 1
  • What compiler options did you use? I built it and it works without error using: `g++ -Wall -std=c++11 -lrt -o "bin/fcntl" "fcntl.cpp"` – David C. Rankin Jun 28 '14 at 07:12
  • `g++ -std=c++11 fcntl.o -lrt -o fcntl` works for me (as long as you add `#include `) in Linux and behaves as it should but in OS X Mavericks (10.9.3) I use `g++ -std=c++11 fcntl.cpp -o fcntl` and get the error. – Thendore Jun 28 '14 at 16:40

1 Answers1

0

If I run it without the preceding echo "Hello" | it works as expected.

Seven and a half years later, this is a problem for me, too.

Unfortunately, OS X at the time of this writing does not support aio on pipes. Thus, your piped input fails with EAGAIN, but aoi_read on other stdin inputs — a terminal, < /dev/null, plain files — succeeds.

At least one open source project has also run into this limitation.

pilcrow
  • 56,591
  • 13
  • 94
  • 135