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.