title: O_DIRECT vs. O_SYNC on Linux/FreeBSD
body: <p>I'm writing a program that runs on both Linux and FreeBSD, and I want to make sure that the data is actually written to the file on the physical device when each <code>write()</code> returns, so that my data won't get lost by accident (eg, power lost, the process is interrupted unexpected, etc.).</p>
  
  <p>According to OPEN(2) man page, on Linux (higher than 2.6), <code>O_DIRECT</code> is synchronous but may have performance problems; on FreeBSD, <code>O_DIRECT</code> is not guaranteed synchronous and may also has problems.</p>
  
  <p>So, on Linux, either <code>O_DIRECT</code> or <code>O_SYNC</code> guarantees synchronous write, but which one has better performance?</p>
  
  <p>On FreeBSD, to guarantee synchronous write, which option has the best performance: (1) <code>O_DIRECT</code> + <code>fsync()</code> (2) <code>O_DIRECT | O_SYNC</code> or (3)<code>O_SYNC</code> alone?</p>