file locks and a counter

Discussion in 'Perl Misc' started by Richard Nixon, Sep 29, 2008.

  1. Hello newsgroup,

    Fortran is my syntax of choice for my avocation in numerical analysis, but
    perl is the much better tool for the net. For one of the better fortran
    sites, they have this counter:

    http://www.fortranlib.com/flcounter.perl

    If the open statement in this:

    sub check_lock {
    $time = $_[0];

    for ($i = 1;$i <= $time; $i++) {
    if (-e "$data_dir$lock_file") {
    sleep 1;
    }
    else {
    open(LOCK,">$data_dir$lock_file");
    print LOCK "0";
    close(LOCK);
    last;
    }
    }
    }

    is changed to this:

    sysopen(FH, $path, O_WRONLY|O_EXCL|O_CREAT) or die $!;

    do they have a better program?

    Thanks and cheers,

    --
    Richard Milhous Nixon

    Denial ain't just a river in Egypt.
    ~~ Mark Twain
     
    Richard Nixon, Sep 29, 2008
    #1
    1. Advertising

  2. Richard Nixon

    Ben Morrow Guest

    Quoth :
    >
    > If the open statement in this:
    >
    > sub check_lock {
    > $time = $_[0];
    >
    > for ($i = 1;$i <= $time; $i++) {
    > if (-e "$data_dir$lock_file") {
    > sleep 1;
    > }
    > else {
    > open(LOCK,">$data_dir$lock_file");
    > print LOCK "0";
    > close(LOCK);
    > last;
    > }
    > }
    > }
    >
    > is changed to this:
    >
    > sysopen(FH, $path, O_WRONLY|O_EXCL|O_CREAT) or die $!;
    >
    > do they have a better program?


    Yes. The program above has a race condition between the -e and the open;
    using O_EXCL avoids this.

    Ben

    --
    #!/bin/sh
    quine="echo 'eval \$quine' >> \$0; echo quined"
    eval $quine
    # []
     
    Ben Morrow, Sep 29, 2008
    #2
    1. Advertising

  3. Richard Nixon <> wrote:
    >perl is the much better tool for the net. For one of the better fortran
    >sites, they have this counter:


    perldoc -q increment

    jue
     
    Jürgen Exner, Sep 29, 2008
    #3
  4. On Mon, 29 Sep 2008 13:07:38 -0700, Jürgen Exner wrote:

    > Richard Nixon <> wrote:
    >>perl is the much better tool for the net. For one of the better fortran
    >>sites, they have this counter:

    >
    > perldoc -q increment
    >
    > jue


    I thought I might do just that, but I don't seem to have anything perl on
    my machine right now:

    F:\gfortran\source>perl anything
    'perl' is not recognized as an internal or external command,
    operable program or batch file.

    F:\gfortran\source>

    I'm downloading Activestate's msi for x86 windows. I've got it stored on
    disk somewhere, but at 17 megs, it's no biggie to get a fresh copy.

    What I really want to do is have my perl stuff and my fortran stuff
    together on F, which is a memory stick, which I can easily transport to my
    ladyfriend's house, where I can then program on her laptop, which is what
    insomniacs do.

    Any suggestions about choices for the activestate install?
    --
    Richard Milhous Nixon

    Always acknowledge a fault frankly. This will throw those in authority off
    guard and allow you opportunity to commit more.
    ~~ Mark Twain
     
    Richard Nixon, Sep 29, 2008
    #4
  5. Richard Nixon

    Guest

    wrote:
    > Hello newsgroup,
    >
    > Fortran is my syntax of choice for my avocation in numerical analysis,
    > but perl is the much better tool for the net. For one of the better
    > fortran sites, they have this counter:
    >
    > http://www.fortranlib.com/flcounter.perl
    >
    > If the open statement in this:
    >
    > sub check_lock {
    > $time = $_[0];
    >
    > for ($i = 1;$i <= $time; $i++) {
    > if (-e "$data_dir$lock_file") {
    > sleep 1;
    > }
    > else {
    > open(LOCK,">$data_dir$lock_file");
    > print LOCK "0";
    > close(LOCK);
    > last;
    > }
    > }
    > }
    >
    > is changed to this:
    >
    > sysopen(FH, $path, O_WRONLY|O_EXCL|O_CREAT) or die $!;
    >
    > do they have a better program?


    That depends. I tend to think that all sub-values of "broken" are
    equal, but sometimes some things can be more broken than others.

    The current code has a race condition. The change detects the race
    condition, but upon detecting it it aborts rather than recovering.
    Maybe:

    sysopen(FH, $path, O_WRONLY|O_EXCL|O_CREAT) or
    ($!{EEXIST} and redo) or die $!;

    However, that still seems to have the problem that if the lock attempt
    times out, it proceeds as if the lock was obtained even though it hasn't
    been.

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    The costs of publication of this article were defrayed in part by the
    payment of page charges. This article must therefore be hereby marked
    advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
    this fact.
     
    , Sep 29, 2008
    #5
  6. On 29 Sep 2008 21:46:19 GMT, wrote:

    > wrote:


    >> sysopen(FH, $path, O_WRONLY|O_EXCL|O_CREAT) or die $!;
    >>
    >> do they have a better program?

    >
    > That depends. I tend to think that all sub-values of "broken" are
    > equal, but sometimes some things can be more broken than others.
    >
    > The current code has a race condition. The change detects the race
    > condition, but upon detecting it it aborts rather than recovering.
    > Maybe:
    >
    > sysopen(FH, $path, O_WRONLY|O_EXCL|O_CREAT) or
    > ($!{EEXIST} and redo) or die $!;
    >
    > However, that still seems to have the problem that if the lock attempt
    > times out, it proceeds as if the lock was obtained even though it hasn't
    > been.
    >
    > Xho


    Thanks, Xho

    Is it the difference between between having the occasional one go uncounted
    as opposed to screwing up the whole thing?

    I don't think this is a script that I can get far with with my own machine:

    F:\gfortran\source>perl gary1.pl
    Content-type: text/html

    No such file or directory at gary1.pl line 230.

    How do you keep track of line numbers with a longer perl script? What I
    did before was put a line somewhere like
    some where;
    and then the compiler would tell me which line had an error. Then I would
    move it closer to line 230. Is there a better way?

    --
    Richard Milhous Nixon

    All kings is mostly rapscallions.
    ~~ Mark Twain
     
    Richard Nixon, Sep 30, 2008
    #6
  7. Richard Nixon

    John Bokma Guest

    Richard Nixon <> wrote:

    > How do you keep track of line numbers with a longer perl script? What
    > I did before was put a line somewhere like
    > some where;
    > and then the compiler would tell me which line had an error. Then I
    > would move it closer to line 230. Is there a better way?


    How about an editor that let you jump to line 230? (TextPad for example),
    or an editor that captures the messages from Perl and let you jump to the
    line with the (supposed) error via the captured messages?

    --
    John http://johnbokma.com/ - Hacking & Hiking in Mexico

    Perl help in exchange for a gift:
    http://johnbokma.com/perl/help-in-exchange-for-a-gift.html
     
    John Bokma, Sep 30, 2008
    #7
  8. Richard Nixon <> wrote:
    >How do you keep track of line numbers with a longer perl script? What I
    >did before was put a line somewhere like
    >some where;
    >and then the compiler would tell me which line had an error. Then I would
    >move it closer to line 230. Is there a better way?


    What about just jumping to line 230?
    - In EMACS M-x goto-line 230. Besides, the current line number is always
    indicated in the status line.
    - I vi I believe it's :230 but my vi is _very_ rusty.
    - Heck, even Notepad has a Goto Line functionality.

    What editor are you using that it doesn't know about line numbers?

    jue
     
    Jürgen Exner, Sep 30, 2008
    #8
  9. Richard Nixon

    Jim Gibson Guest

    In article <>, Jürgen Exner
    <> wrote:

    > Richard Nixon <> wrote:
    > >How do you keep track of line numbers with a longer perl script? What I
    > >did before was put a line somewhere like
    > >some where;
    > >and then the compiler would tell me which line had an error. Then I would
    > >move it closer to line 230. Is there a better way?

    >
    > What about just jumping to line 230?


    > - I vi I believe it's :230 but my vi is _very_ rusty.


    230G

    --
    Jim Gibson
     
    Jim Gibson, Sep 30, 2008
    #9
  10. On Mon, 29 Sep 2008 18:21:59 -0700, Jürgen Exner wrote:

    > Richard Nixon <> wrote:
    >>How do you keep track of line numbers with a longer perl script? What I
    >>did before was put a line somewhere like
    >>some where;
    >>and then the compiler would tell me which line had an error. Then I would
    >>move it closer to line 230. Is there a better way?

    >
    > What about just jumping to line 230?
    > - In EMACS M-x goto-line 230. Besides, the current line number is always
    > indicated in the status line.
    > - I vi I believe it's :230 but my vi is _very_ rusty.
    > - Heck, even Notepad has a Goto Line functionality.
    >
    > What editor are you using that it doesn't know about line numbers?
    >
    > jue


    Gosh, juergen, I was unaware that notepad had that functionality.

    While I am a "windows guy," I've never really developed any continuing
    education with it because I haven't ever been in a place on usenet where
    The Topic can be bothered for a practical user concern.

    Line 230 is the sysopen line here:

    sub check_lock {
    $time = $_[0];

    for ($i = 1;$i <= $time; $i++) {
    if (-e "$data_dir$lock_file") {
    sleep 1;
    }
    else {
    sysopen(FH, $path, O_WRONLY|O_EXCL|O_CREAT) or die $!;
    print LOCK "0";
    close(LOCK);
    last;
    }
    }
    }

    There's no great surprise here, as I've got no file for it to open, so I
    think I'm at the level where I can't pursue this further.

    Does this address the file lock issue adequately?
    --
    Richard Milhous Nixon

    A man came into the the office one day and said he was a sailor. We cured
    him of that.
    ~~ Mark Twain
     
    Richard Nixon, Sep 30, 2008
    #10
  11. Richard Nixon

    Tim Greer Guest

    Richard Nixon wrote:

    >
    >
    > Hello newsgroup,
    >
    > Fortran is my syntax of choice for my avocation in numerical analysis,
    > but
    > perl is the much better tool for the net. For one of the better
    > fortran sites, they have this counter:
    >
    > http://www.fortranlib.com/flcounter.perl
    >
    > If the open statement in this:
    >
    > sub check_lock {
    > $time = $_[0];
    >
    > for ($i = 1;$i <= $time; $i++) {
    > if (-e "$data_dir$lock_file") {
    > sleep 1;
    > }
    > else {
    > open(LOCK,">$data_dir$lock_file");
    > print LOCK "0";
    > close(LOCK);
    > last;
    > }
    > }
    > }
    >
    > is changed to this:
    >
    > sysopen(FH, $path, O_WRONLY|O_EXCL|O_CREAT) or die $!;
    >
    > do they have a better program?
    >
    > Thanks and cheers,
    >


    Matt Wright's scripts were horrible in their day, and in this day and
    age, have no place to be used. Even Matt himself recommends people use
    better coded scripts ( http://www.scriptarchive.com/nms.html ). A lot
    of his scripts have race conditions and the above locking and test
    offers that very thing (a race condition).

    It doesn't make sense to use a physical $filename.lock file and to check
    its existence to see if it's locked and not use flock (file locking).
    Please, don't use any of his scripts. View them for historical reasons
    and not production ready. They are not stable or secure. Instead, use
    the NMS (No Matt Script) version. You can get the counter here:

    http://nms-cgi.sourceforge.net/scripts.shtml

    Look for "Text Counter". You also have a lot of other scripts that are
    superior to Matt's that are there, which you should use instead of his
    (if you use any of the other ones).
    --
    Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
    Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
    and Custom Hosting. 24/7 support, 30 day guarantee, secure servers.
    Industry's most experienced staff! -- Web Hosting With Muscle!
     
    Tim Greer, Sep 30, 2008
    #11
  12. On Tue, 30 Sep 2008 13:22:56 -0600,
    Richard Nixon <> wrote:

    > Line 230 is the sysopen line here:
    >
    > sub check_lock {
    > $time = $_[0];
    >
    > for ($i = 1;$i <= $time; $i++) {
    > if (-e "$data_dir$lock_file") {
    > sleep 1;
    > }
    > else {
    > sysopen(FH, $path, O_WRONLY|O_EXCL|O_CREAT) or die $!;
    > print LOCK "0";
    > close(LOCK);
    > last;
    > }
    > }
    > }


    And earlier, the error message:

    > F:\gfortran\source>perl gary1.pl
    > Content-type: text/html
    >
    > No such file or directory at gary1.pl line 230.


    The O_CREAT flag in sysopen means that it will create the file if it
    doesn't exist. The perlfunc documentation gives you a full description
    of sysopen. it also uses O_EXCL, which means it should fail if the file
    already exists, which seems odd. This means that the 'lock file' would
    cause a program to exit, instead of wait for the lock to be released. I
    would assume that trying to deal with the error would be better than to
    die.

    Anyway, the error message you got was "No such file or directory", which
    most likely means that the $path variable was set to create a file in a
    place that doesn't exist. For example, if $path = "/tmp/foo/bar.lock",
    and /tmp/foo does not exist, then you get that error.

    If the file already exists, the error message will be something like
    "File exists", and the program will exit, instead of trying again later
    or waiting.

    > There's no great surprise here, as I've got no file for it to open, so I
    > think I'm at the level where I can't pursue this further.


    You do not need a file for it to open. On the contrary, you need the
    file not to exist yet when this code runs.

    > Does this address the file lock issue adequately?


    I am not sure what you mean by 'file lock issue', and I haven't read
    the full thread leading up to this. I assume you have already been
    pointed to the FAQ entry "How can I lock a file?" and the "File Locking"
    section in the perlopentut documentation?


    Martien
    --
    |
    Martien Verbruggen | Blessed are the Fundamentalists, for they
    | shall inhibit the earth.
    |
     
    Martien Verbruggen, Sep 30, 2008
    #12
  13. Richard Nixon <> wrote:


    > for ($i = 1;$i <= $time; $i++) {



    foreach $i ( 1 .. $time ) { # does the same thing

    or, better:

    foreach my $i ( 1 .. $time ) { # scope your variables


    --
    Tad McClellan
    email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
     
    Tad J McClellan, Sep 30, 2008
    #13
  14. On Wed, 1 Oct 2008 08:55:34 +1000, Martien Verbruggen wrote:

    >> Does this address the file lock issue adequately?

    >
    > I am not sure what you mean by 'file lock issue', and I haven't read
    > the full thread leading up to this. I assume you have already been
    > pointed to the FAQ entry "How can I lock a file?" and the "File Locking"
    > section in the perlopentut documentation?




    Martin--

    Die voellige Pfade kann ich auch nicht rechnen, ohne engere Beziehungen.
    Ich bin nicht, cowboy an keyboard, eines anderen Servers.

    Deswegen wird yefraagt.

    >
    >
    > Martien
    > --
    > |
    > Martien Verbruggen | Blessed are the Fundamentalists, for they
    > | shall inhibit the earth.
    > |



    Yesegnet wuerden die fundies, falls diese Typen dem Herrn yefielen.


    --
    Richard Milhous Nixon

    But who prays for Satan? Who, in eighteen centuries, has had the common
    humanity to pray for the one sinner that needed it most?
    ~~ Mark Twain
     
    Richard Nixon, Oct 1, 2008
    #14
  15. On 1 Oct 2008 08:09:54 GMT,
    Richard Nixon <> wrote:
    > On Wed, 1 Oct 2008 08:55:34 +1000, Martien Verbruggen wrote:
    >
    >>> Does this address the file lock issue adequately?

    >>
    >> I am not sure what you mean by 'file lock issue', and I haven't read
    >> the full thread leading up to this. I assume you have already been
    >> pointed to the FAQ entry "How can I lock a file?" and the "File Locking"
    >> section in the perlopentut documentation?

    >
    >
    >
    > Martin--
    >
    > Die voellige Pfade kann ich auch nicht rechnen, ohne engere Beziehungen.
    > Ich bin nicht, cowboy an keyboard, eines anderen Servers.
    >
    > Deswegen wird yefraagt.


    I don't know what language this is. It vaguely looks like German, but it
    isn't. I have no clue why you decided to switch to this.

    I think you're trying to say that you're not a cowboy, and that you
    can't count on the full path anyway. if I assume you mean you can't
    control the directory structure, then I fail to see how that has
    anything to do with what I wrote, especially the quoted bit. You don't
    need to control that. All you need to do is change the variable in your
    program so that it points to a place where you can write the file.

    I have just skimmed further upthread; none of the original code would
    work either without a correct path. Whatever you do, you need to fix
    that anyway.

    My post directly addressed your error message, and the code that was
    related to it.

    Martien
    --
    |
    Martien Verbruggen | prepBut nI vrbLike adjHungarian! qWhat's
    | artThe adjBig nProblem? -- Alec Flett
    |
     
    Martien Verbruggen, Oct 1, 2008
    #15
    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. The Eeediot
    Replies:
    3
    Views:
    2,240
    =?Utf-8?B?UnVsaW4gSG9uZw==?=
    Dec 22, 2004
  2. George2
    Replies:
    1
    Views:
    804
    Alf P. Steinbach
    Jan 31, 2008
  3. Randy Webb

    word counter script locks up Mozilla

    Randy Webb, Feb 15, 2004, in forum: Javascript
    Replies:
    6
    Views:
    129
    David
    Feb 16, 2004
  4. Ismael Farfán
    Replies:
    0
    Views:
    217
    Ismael Farfán
    Sep 19, 2012
  5. Ismael Farfán
    Replies:
    0
    Views:
    217
    Ismael Farfán
    Sep 19, 2012
Loading...

Share This Page