title: How atomic are lockfiles in PHP?
body: <p>I have a PHP page that should be accessible only by one user at a time. It's a kind of "poor man's cron": A "Javascript" file, that is requested in the background. At the moment I'm thinking of creating a lock file like this:</p>
  
  <pre><code>if(file_exists($lockfile) &amp;&amp; filemtime($lockfile) + EXPIRES_AFTER &gt; time() ) {
      die("// Page is locked.");
  }
  
  touch($lockfile);
  // do stuff
  unlink($lockfile);
  echo "// Cron stuff was run";
  </code></pre>
  
  <p>But I'm not sure if there could be a very short window of opportunity between the <code>file_exists</code> and the <code>touch</code> call where another page request could check for the file existence and see that it isn't there. We're probably talking microseconds here so I would like top know at which amount of requests I really need to start worrying.</p>