Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > There IS a way to test for a file lock

Reply
Thread Tools

There IS a way to test for a file lock

 
 
Sara
Guest
Posts: n/a
 
      08-26-2004
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
 
Reply With Quote
 
 
 
 
Ron Parker
Guest
Posts: n/a
 
      08-26-2004
On 26 Aug 2004 07:28:49 -0700, Sara wrote:
> 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."

--
#macro R(L P)sphere{L F}cylinder{L P F}#end#macro P(V)merge{R(z+a z)R(-z a-z)R(a
-z-z-z a+z)torus{1F clipped_by{plane{a 0}}}translate V}#end#macro Z(a F T)merge{
P(z+a)P(z-a)R(-z-z-x a)pigment{rgbt 1}hollow interior{media{emission T}}finish{
reflection.1}}#end Z(-x-x.2y)Z(-x-x.4x)camera{location z*-10rotate x*90}
 
Reply With Quote
 
 
 
 
Anno Siegel
Guest
Posts: n/a
 
      08-26-2004
Sara <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> 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
 
Reply With Quote
 
Tassilo v. Parseval
Guest
Posts: n/a
 
      08-26-2004
Also sprach Anno Siegel:

> Sara <(E-Mail Removed)> wrote in comp.lang.perl.misc:
>> 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
--
$_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus}) !JAPH!qq(rehtona{tsuJbus#;
$_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexi ixesixeseg;y~\n~~dddd;eval
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Read-Write Lock vs primitive Lock() k3xji Python 7 12-30-2008 10:19 PM
Is there a good way to implement file updating? such as lock, update,reload... Evan Python 2 07-07-2008 01:40 PM
Threading - Why Not Lock Objects Rather than lock the interpreter Fuzzyman Python 3 12-05-2003 10:43 PM
RE: Threading - Why Not Lock Objects Rather than lock theinterpreter Robert Brewer Python 0 12-05-2003 05:33 PM
test test test test test test test Computer Support 2 07-02-2003 06:02 PM



Advertisments