in reply to Using flock() on files

Another way may be to use separate lockfiles to hand-roll your own lock - check for the existence of "myfile.lock" - if none, then create it, open "myfile", do stuff, close it, and delete "myfile.lock" all in one go. (Or maybe even just using chmod and some funky rw permission stuff on the file itself)

I'm sure there must be a few nodes on this lying around the monastery somewhere though, either providing code or trashing the idea (hopefully whilst providing some better solutions) :)

Cheers,
Ben

Replies are listed 'Best First'.
Re: Re: Using flock() on files
by nothingmuch (Priest) on Apr 20, 2003 at 17:25 UTC
    This creates a race condition. You must wrap it in some atomical locking mechanism. Between the time you check and the time you create, even if you ask sysopen for an exclusive create, several processes may think they possess the lock at the same time.

    not a Good Thing™

    -nuffin
    zz zZ Z Z #!perl
      doh! Ta - I knew that there was something vital missing even as I was pressing submit...
      note to self..don't post before 1st coffee - 'specially on a Sun. morning.