in reply to File locking, lock files, and how it all sucks

The problem isn't in Perl. Your mutual exclusion is incorrect. (Obviously ;-)

It's OK to allow programs to read the contents of a file with shared locks, but writers must take exclusive locks -- which you're doing. However, if your exclusive lock attempt fails, you need to re-read the file. If you can't take an exclusive lock, someone else is writing to the file, and your in-memory copy is no good.</p<

In your snippet above, when Process 2 fails to get an exclusive lock, it can't just wait to get the lock, then clobber it anyway. It needs to start from scratch because when the lock fails, it has no idea what the state of the file is.

HTH

Update
Forgot what I was going to suggest strongest of all: if you have the time/resources, ditch the files in favor of a relational DBMS with transaction support....

  • Comment on Re: File locking, lock files, and how it all sucks

Replies are listed 'Best First'.
Re: Re: File locking, lock files, and how it all sucks
by tocie (Novice) on Aug 22, 2001 at 00:08 UTC

    Unfortunately, this system has to run "everywhere" on stock Perl and stock modules. No RDMS for us. :( :( :(

    There's no code in the system that would timeout - it's a straight open, flock, write, unflock, close, everywhere. If flocking is working as it should, the script SHOULD be waiting at the flock step until the other exclusives have released and it can get an exclusive.

    What APPEARS to be happening is TWO instances of the script open the file at the same time, but one gets the exclusive lock first. It will do its thing, then release the lock. The second script will then write out ITS data to the start of the file (and/or is resetting the filehandle to the start - I'm not sure if the file cursor is based on what the O/S reports or where that particular script reports), and release its lock.

    In other words, the flocking isn't doing the job well enough. I need to block before I even open the file.

    Thank you for your continued thoughts. It's VERY appreciated!

      Anytime your process is going to change a file you must exclusively lock, read, update, write, unlock.

      Only readers who will never write the data can use shared locking.