file locks and a counter

R

Richard Nixon

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,
 
B

Ben Morrow

Quoth (e-mail address removed):
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
 
R

Richard Nixon

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
 
X

xhoster

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.
 
R

Richard Nixon

(e-mail address removed) wrote:

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?
 
J

John Bokma

Richard Nixon said:
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?
 
J

Jürgen Exner

Richard Nixon said:
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
 
R

Richard Nixon

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
 
T

Tim Greer

Richard said:
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).
 
M

Martien Verbruggen

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
 
R

Richard Nixon

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
 
M

Martien Verbruggen

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
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top