Search code examples
iolinux-kernelposix

Why Posix File I/O is always blocking?


From man pages

O_NONBLOCK or O_NDELAY  
        This flag has no effect for regular files and block
          devices; that is, I/O operations will (briefly) block when
          device activity is required, regardless of whether O_NONBLOCK
          is set.  Since O_NONBLOCK semantics might eventually be
          implemented, applications should not depend upon blocking
          behavior when specifying this flag for regular files and block
          devices.

From my question I had following understanding of the io system.

             Device <-----> Kernel Buffers <----->  Process

So whenever Buffers are full (write) or empty (read), the corresponding command from the process can block or not depending on the flag above. Kernel interacting with the device is not blocking for the process. The kernel might or might not be using DMA for communication with the device.

But looks like my understanding is wrong as I can't see why the regular file descriptors can't be non-blocking. Could somebody help me here?


Solution

  • "Blocking" is defined as waiting for a file to become readable or writable.

    Regular files are always readable and/or writable; in other words, it is always possible to try to start the read/write operation without having to wait for some external event:

    • when reading, the kernel already knows if there are more bytes in the file (if the end of the file has been reached, it is not possible to block to wait for some other process to append more bytes);
    • when writing, the kernel already knows if there is enough space on the disk to write something (if the disk is full, it is not possible to block to wait for some other process to delete some data to free up space).