in reply to Flock Feedback
Secondly your flock makes all of the usual mistakes. When you close it you have no guarantee that when you turn around you will be next to get the file. What is likely to happen sooner or later is that you read, someone else is waiting to read, you close, it gets the lock and reads, closes, you write, it writes. Thereby missing a count. What is worse is if you manage to open for write (wiping out the file) before it reads.
If you must flock the file you are writing, the right way to do it is a non-destructive open for append or read, flock for writing, seek to the beginning, read, seek back, truncate, write, close (without unlocking). The simpler (but still safe) way is to have a sentinel (eg "counter.lock") that you open, flock, access the other file, then close. (Don't ever delete this!)
For a rather heavy version of the sentinel file approach see Simple Locking.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re (tilly) 1: Flock Feedback
by footpad (Abbot) on Feb 25, 2001 at 08:48 UTC | |
by tilly (Archbishop) on Feb 25, 2001 at 09:41 UTC |