title: flock(): removing locked file without race condition?
body: <p>I'm using flock() for inter-process named mutexes (i.e. some process can decide to hold a lock on "some_name", which is implemented by locking a file named "some_name" in a temp directory:</p>
  
  <pre><code>lockfile = "/tmp/some_name.lock";
  fd = open(lockfile, O_CREAT);
  flock(fd, LOCK_EX);
  
  do_something();
  
  unlink(lockfile);
  flock(fd, LOCK_UN);
  </code></pre>
  
  <p>The lock file should be removed at some point, to avoid filling the temp directory with hundreds of files.</p>
  
  <p>However, there is an obvious race condition in this code; example with processes A, B and C:</p>
  
  <pre><code>A opens file
  A locks file
  B opens file
  A unlinks file
  A unlocks file
  B locks file (B holds a lock on the deleted file)
  C opens file (a new file one is created)
  C locks file (two processes hold the same named mutex !)
  </code></pre>
  
  <p>Is there a way to remove the lock file at some point without introducing this race condition ?</p>