There IS a way to test for a file lock

Discussion in 'Perl Misc' started by Sara, Aug 26, 2004.

  1. Sara

    Sara Guest

    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
     
    Sara, Aug 26, 2004
    #1
    1. Advertising

  2. Sara

    Ron Parker Guest

    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}
     
    Ron Parker, Aug 26, 2004
    #2
    1. Advertising

  3. Sara

    Anno Siegel Guest

    Sara <> 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
     
    Anno Siegel, Aug 26, 2004
    #3
  4. Also sprach Anno Siegel:

    > Sara <> 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$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval
     
    Tassilo v. Parseval, Aug 26, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Fuzzyman
    Replies:
    3
    Views:
    496
    Andrew MacIntyre
    Dec 5, 2003
  2. Robert Brewer
    Replies:
    0
    Views:
    495
    Robert Brewer
    Dec 5, 2003
  3. Evan
    Replies:
    2
    Views:
    276
  4. k3xji
    Replies:
    7
    Views:
    819
    Gabriel Genellina
    Dec 30, 2008
  5. nano2k

    Application.Lock()/UnLock() or lock(Application)

    nano2k, Jul 23, 2007, in forum: ASP .Net Web Services
    Replies:
    2
    Views:
    291
    nano2k
    Aug 9, 2007
Loading...

Share This Page