in reply to Re: flock LOCK_EX not locking exclusively
in thread flock LOCK_EX not locking exclusively

Checking if the file exists after the flock is not good enough. Assume the check is done with a stat.
child 1   child 2   child 3
open
          open
flock                         (works for obvious reasons)
stat                          (works for obvious reasons)
unlink
close
                    open      (creates a new file)
                    flock     (works, no contention on new file)
                    stat      (works, new file exists)
          flock               (works, no contention on old file)
          stat                (works, new file exists)
  • Comment on Re^2: flock LOCK_EX not locking exclusively

Replies are listed 'Best First'.
Re^3: flock LOCK_EX not locking exclusively
by pc88mxer (Vicar) on Jan 03, 2008 at 15:51 UTC
    Well, if your open can create files, then obviously you'll have a problem. I was assuming that open would be used with mode <.

    In general, if file names can never reappear after being deleted you can use this method. That's why I say that flock generally doesn't work well with unlink.