There IS a way to test for a file lock

S

Sara

After querying this group, perldoc, Camel, and my local Perl
associates, I came up empty on how to deterimine if a file is locked (
with flock() ). Most of the posts in CLPM suggest where is no way to
do it. Happily though, there is. Perhaps this post will prevent others
from spending 2-3 hours going down the same road.

Looking at the Camel v3 doc for "flock", it states it returns 1 for
success 0 otherwise. If you're using Camel V2 however, there is no
discussion about returned value from flock(), so upgrade :)

The PROBLEM is, if one uses:

flock(LOCK, LOCK_EX)

on a locked file, it's blocked until its released. So it's not really
a test since we can't evaluate (blocked indefinitely), particularly
when the lock may be perpetual. A third param, an optional timeout,
would be

The trick is use this:

flock(LOCK, LOCK_EX|LOCK_NB)

which produces exactly what we're after- 0 for a locked file, and 1
for a non-locked file (with the side-effect of actually LOCKING the
file, so if you ONLY wanted to test, you'll need to release the lock).

I hope this assists some with a test for file locking. It would be
nice if stat returned a boolean for file lock, but it doesn't.

All append the usual comments here to save the naysayers the trouble.
Good day!


*****************************************************************************


This is stupid - why post it?

Read Camel idiot!

Did you check perldoc?

This will never work.

There is an easier way using a simple 10 line program with eval and
alarms.



*******************************************************************************


G
 
R

Ron Parker

All append the usual comments here to save the naysayers the trouble.

You missed one: "'All' and 'I'll' may be pronounced the same way in some
dialects, but they're never spelled the same."
 
A

Anno Siegel

Sara said:
After querying this group, perldoc, Camel, and my local Perl
associates, I came up empty on how to deterimine if a file is locked (
with flock() ). Most of the posts in CLPM suggest where is no way to

[use non-blocking flock()]
All append the usual comments here to save the naysayers the trouble.
Good day!


*****************************************************************************


This is stupid - why post it?

Read Camel idiot!

Did you check perldoc?

This will never work.

There is an easier way using a simple 10 line program with eval and
alarms.

Thank you for your confidence. You forgot one:

It's off topic.

However, I wonder how you got the impression that this is hard to do
in the first place. The method you recommend is absolutely standard,
in any language that has flock-based file locking. Apparently no-one
with a modicum of clue has read your question, or bothered to reply.

Another question is why you want to check for locks. If you want
one, request it. If you don't want to hang, do it non-blocking and
check the return value. That's all there's to it. There may be
reasons to check if a file is locked (e.g. to see if a process is
still alive), but in the normal business of requesting and releasing
locks, it is useless.

Anno
 
T

Tassilo v. Parseval

Also sprach Anno Siegel:
Sara said:
After querying this group, perldoc, Camel, and my local Perl
associates, I came up empty on how to deterimine if a file is locked (
with flock() ). Most of the posts in CLPM suggest where is no way to

[use non-blocking flock()]
All append the usual comments here to save the naysayers the trouble.
Good day!


*****************************************************************************


This is stupid - why post it?

Read Camel idiot!

Did you check perldoc?

This will never work.

There is an easier way using a simple 10 line program with eval and
alarms.

Thank you for your confidence. You forgot one:

It's off topic.

However, I wonder how you got the impression that this is hard to do
in the first place. The method you recommend is absolutely standard,
in any language that has flock-based file locking. Apparently no-one
with a modicum of clue has read your question, or bothered to reply.

His question had a somewhat familiar ring to me and indeed it showed up
several times in the past with minor variations. According to
google-groups I have given the non-blocking flock advice four times
between 2003/06/14 and 2004/05/18.

However, I wasn't able to find the posting in which he allegedly asked
this group about detecting a lock. And therefore the comments he claims
to have received from this group can only exist in his imagination.

Tassilo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top