Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > file locks and a counter

Reply
Thread Tools

file locks and a counter

 
 
Richard Nixon
Guest
Posts: n/a
 
      09-29-2008


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
 
Reply With Quote
 
 
 
 
Ben Morrow
Guest
Posts: n/a
 
      09-29-2008

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
# []
 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      09-29-2008
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
 
Reply With Quote
 
Richard Nixon
Guest
Posts: n/a
 
      09-29-2008
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
 
Reply With Quote
 
xhoster@gmail.com
Guest
Posts: n/a
 
      09-29-2008
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.
 
Reply With Quote
 
Richard Nixon
Guest
Posts: n/a
 
      09-30-2008
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
 
Reply With Quote
 
John Bokma
Guest
Posts: n/a
 
      09-30-2008
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-ex...or-a-gift.html
 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      09-30-2008
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
 
Reply With Quote
 
Jim Gibson
Guest
Posts: n/a
 
      09-30-2008
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
 
Reply With Quote
 
Richard Nixon
Guest
Posts: n/a
 
      09-30-2008
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
 
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
Re: How to get the list of all my open file(descriptor)s and locks? Ismael Farfán Python 0 09-19-2012 08:36 PM
How to get the list of all my open file(descriptor)s and locks? Ismael Farfán Python 0 09-19-2012 05:34 PM
Page File counter and Private Bytes Counter George2 C++ 1 01-31-2008 09:27 AM
Session("counter") vs. ViewState("counter")...a newbie question The Eeediot ASP .Net 3 12-22-2004 09:31 PM
word counter script locks up Mozilla David Javascript 6 02-16-2004 07:38 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57