title: How performance of reads/writes to regular file varies when linux kernel memory load becomes high?
body: <p>It seems that writes/reads to regular files can't not be made non-blocking. I found the following references for support:</p>
  
  <p>from The Linux Programming Interface: A Linux and UNIX System Programming Handbook:</p>
  
  <blockquote>
    <p>"--- Nonblocking mode can be used with devices (e.g., terminals and pseudoterminals), pipes, FIFOs, and sockets. (Because file descriptors for pipes and sockets are not obtained using open(), we must enable this flag using the fcntl() F_SETFL operation described in Section 5.3.) O_NONBLOCK is generally ignored for regular files, because the kernel buffer cache ensures that I/O on regular files does not block, as described in Section 13.1. However, O_NONBLOCK does have an effect for regular files when mandatory file locking is employed (Section 55.4). ---"</p>
  </blockquote>
  
  <p>from Advanced Programming in the UNIX Environment 2nd Ed:</p>
  
  <blockquote>
    <p>"--- We also said that system calls related to disk I/O are not considered slow, even though the read or write of a disk file can block the caller temporarily. ---"</p>
  </blockquote>
  
  <p>from <a href="http://www.remlab.net/op/nonblock.shtml" rel="nofollow">http://www.remlab.net/op/nonblock.shtml</a>:</p>
  
  <blockquote>
    <p>"--- Regular files are always readable and they are also always writeable. This is clearly stated in the relevant POSIX specifications. I cannot stress this enough. Putting a regular file in non-blocking has ABSOLUTELY no effects other than changing one bit in the file flags. Reading from a regular file might take a long time. For instance, if it is located on a busy disk, the I/O scheduler might take so much time that the user will notice the application is frozen. Nevertheless, non-blocking mode will not work. It simply will not work. Checking a file for readability or writeability always succeeds immediately. If the system needs time to perform the I/O operation, it will put the task in non-interruptible sleep from the read or write system call. ---"</p>
  </blockquote>
  
  <p>When memory is adequately available, reads/writes is performed through kernel buffering.</p>
  
  <p>My question is: is there a scenario that the kernel is out of memory that buffering is not usable immediately? If yes, what will kernel do? Simply returns an error or do some amazing trick?</p>
  
  <p>Thanks guys!</p>