> -----Original Message-----
> From: ilug-admin at linux.ie [mailto:ilug-admin at linux.ie]On Behalf Of
> Kathryn Cassidy
> Sent: 28 February 2000 16:01
> To: ilug at linux.ie> Subject: [ILUG] [OT] perl: file locking with flock
>>> Agh! I've got a wee script here which is meant to put an
> exclusive lock on
> a file, wait for a minute, then release the lock. It goes something like
> this:
>> open LOCKED, '>>oqq.inc';
>> $result=flock(LOCKED, LOCK_EX);
If I change this line to:
flock(LOCKED, LOCK_EX) or die "flock() failed: $!\n"
I get:
flock() failed: Invalid argument
My guess is you haven't imported the 'LOCK_*' constants flock uses, so your
attempt to flock() fails. One other thing. I'm not sure it this is normal
behaviour for the libc version of flock(), but if the file has been locked
already, perl's flock() appears to wait for the first lock to be released
and then locks the file, rather than returning an error.
Try this instead:
use Fcntl ':flock'; # import LOCK_* constants
open LOCKED '>>foo.txt' or die "open() failed: $!\n";
flock(LOCKED, LOCK_EX) or die "flock() failed: $!\n";
# see what time the file was locked at
print localtime(time())."\n";
sleep 60;
flock(LOCKED, LOCK_UN);
close(LOCKED);
John.
Maintained by the ILUG website team. The aim of Linux.ie is to
support and help commercial and private users of Linux in Ireland. You can
display ILUG news in your own webpages, read backend
information to find out how. Networking services kindly provided by HEAnet, server kindly donated by
Dell. Linux is a trademark of Linus Torvalds,
used with permission. No penguins were harmed in the production or maintenance
of this highly praised website. Looking for the
Indian Linux Users' Group? Try here. If you've read all this and aren't a lawyer: you should be!