in reply to How to make sure that non-Perl programs will respect Perl's file locking?

From my understanding, flock is truely a syscall to the OS you are running the application on. So, in *nix, for example, if you man flock, you will get the manual for this specific function call.

One thing I've always done, therefore avoiding any direct system calls, is create a 'lock file'. Creating a simple file, where nothing is allowed to do 'anything' until the file is removed.

For example:
sleep( 0.001 ) while ( -r '.lock' ); # You made it here, the file is gone
Naturally, it's always easy to say something like this, however you may be stuck with using flock. In that case, I would research the man pages of the OS you are running to find out the exact behavior of the syscall you're using.

---hA||ta----
print map{$_.' '}grep{/\w+/}@{[reverse(qw{Perl Code})]} or die while ( 'trying' );

Replies are listed 'Best First'.
Re^2: How to make sure that non-Perl programs will respect Perl's file locking?
by kscaldef (Pilgrim) on Jan 11, 2006 at 18:02 UTC
    Don't write code like that. Polling when there's a perfectly good blocking system call is a huge waste of CPU and I/O resources. If you actually had an application with any non-trivial lock contention, this sort of code will probably cripple your server.