[tpm] Fast(er) way of deleting X lines from start of a file

Stuart Watt stuart at morungos.com
Thu Oct 1 09:39:27 PDT 2009


Madison Kelly wrote:
> Does perl support file locking? If so, how does an active file handle 
> know when another perl process has locked a file? Will it pause and 
> wait for the file to be unlocked?
Kind of. The flock() function that does the main magic is usually 
available, but it does delegate a lot to the operating system or C 
runtime, and these vary a lot. The answer to the "how does it know" bit 
is at the OS level. Under a Perl handle is an OS handle, and that is 
what is used to ensure exclusive access. Generally Perl can't find the 
other process, but the OS prevents any other process from using the file.

If you're on a UNIX-style system, flock() is usually OK (except on 
shared file systems), but on Windows, I have had problems with the 
emulated versions. Windows has its own API access to its own mutex 
systems, which are reliable but generally unpleasant to look at. I now 
tend to use a SQLite database, which provides exclusive transactions, as 
a way of synchronizing between separate processes, and I share data 
between processes that way too. I have learnt to avoid depending too 
much on Perl file system access on Windows. I have no idea how SQLite 
does what it does internally, but it seems a very simple solution to 
code, and one which is very reliable.

I've also used MLDBM::Sync in the past, but with less success. I prefer 
using higher level modules to using flock() directly, as they often add 
additional logic that boosts portability. My experiences with direct 
flock() coding were not positive or rewarding.

All the best
Stuart
-- 
Stuart Watt
ARM Product Developer
Information Balance
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/toronto-pm/attachments/20091001/e4377fe2/attachment.html>


More information about the toronto-pm mailing list