in reply to Re: (OT?) File Locking
in thread (OT?) File Locking
If you intent to append to a file, you need to handle the small, but very real timing window between opening the file and acquiring the lock. Within that window, a different process can write the the file, leaving you with a bogus seek pointer. Best case, you'll lose the last record(s) written. Worse case, you'll write into the midst of them, leaving corrupted records.
To mitigate the timing window, add the following line
use Fcntl ':flock'; ... open FH, ">>$file" or die "Can't open $file: $!"; flock FH, LOCK_EX; # lock it +++ seek FH, 0, 2; # in case eof moved # ... print() to file here flock FH, LOCK_UN; close FH;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Re: (OT?) File Locking
by no_slogan (Deacon) on Apr 14, 2002 at 18:37 UTC | |
by dws (Chancellor) on Apr 14, 2002 at 19:26 UTC | |
by tye (Sage) on Apr 14, 2002 at 20:16 UTC |