A reminder if you want to read/write your file in non blocking mode (in C(++))

Sometimes you might want to read a file non-blocking. It could be /dev/random because waiting for entropy might take very long. Also when you have to read device files on unix you sometimes have to read them non-blocking. A common pattern is to open the file non-blocking using the open method: open(..., O_NONBLOCK, ...)

This however may be a problem. This type of open will typically succeed (because the system is opening the file non-blocking) and when you then start reading (or writing) you get a failure. The failure can be anything from permission problems to unavailable devices. This is how non-blocking I/O works and something you're well aware of if you're using sockets for communication but since "this is files" you might forget to take non-blocking into account.

So there is a really easy way to work around this. Files, unlike network sockets completes the open operation very fast. So making the open call non-blocking generally does not make so much sense. Instead I suggest you open the file in blocking mode and then change it to non-blocking one opened. And this is how you do that:

 
   1:  int fd = open(..., 0, ...); // Filed open in blocking
   2:  int fd_flags = fcntl(fd, F_GETFL);
   3:  if (fd_flags < 0) { // Get any flags set on the fd.
   4:      close(fd);
   5:      return error;
   6:  }
   7:  if (-1 == fcntl(fd, F_SETFL, fd_flags | O_NONBLOCK)) { // Add non blocking to the fd flags
   8:      close(fd);
   9:      return error;
  10:  }

But there is (at least) one situation where a non-blocking open might make some sense. That is when you open a file on a remote server and you cannot afford the waiting of an open call. And in this case you must treat the file descriptor like any other file descriptor used for network communication and useĀ select to make sure the file descriptor is ready before you start using it.