title: Does advisory file locking work with default file descriptors?
body: <p>For example, say I have the following shell command.</p>
  
  <pre><code>~]$ foobar 2&gt;&gt; foobar.log
  </code></pre>
  
  <p>The above command redirects the standard error output (<code>stderr</code>, or file descriptor <code>2</code>) to the file <code>foobar.log</code>, appending the output (the <code>&gt;&gt;</code> rather than just <code>&gt;</code>).</p>
  
  <p>Now, assume that two users are both running the exact same command. In this case, the output to the file is interleaved, making it rather difficult to read.</p>
  
  <p>Programs can utilise "advisory file locking" (via the <code>fcntl()</code> C function) as a operating-system level mutual-exclusion on files, essentially coordinating the multiple process so that only one process writes to the file at any given time. Hence the output of the two processes is no longer interleaved and becomes easier to read.</p>
  
  <p>However, how do shells implement the invocation above? If they use the <code>pipe()</code> system call, advisory file locking will not work. If, on the other hand, they use <code>dup()</code> (or some other variant) before calling <code>fork()/exec()</code>, then advisory file locking should work.</p>
  
  <p>Which is the case, and should advisory file locking work on shell-redirected standard output (<code>stdout</code>, file descriptor 1) and standard error (<code>stderr</code>, file descriptor 2)?</p>