Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

just a question 1

Status
Not open for further replies.

NEVERSLEEP

Programmer
Apr 14, 2002
667
0
0
CA
if i have a cgi that somewhere in the code
open a file , read it , return the value
if two users request my cgi will there be a problem
like file violation or something
if so can some point the route to resolve this ?
thanks
wmail.jpg


someone knowledge ends where
someone else knowledge starts
 
You need to lock the file using Flock. Like:


#!/path/to/perl.exe

use Fcntl qw:)flock);

sub get_lock {
open(FILE, ">$file") || die "Error: $!";
flock(FILE, LOCK_EX) || die "Lock Failed: $!";
}


sub unlock {
close(FILE);
}


Lock Types
LOCK_SH - Requests a shared lock on the file. If another process has an exclusive lock on the file, then flock pauses until the exclusive lock are cleared.

LOCK_EX - This requests an exclusive lock on a file for opening and writing. If other process have a lock (either shared/exclusive), then flock pauses unntil those locks are cleared.

LOCK_UN - This requests a lock. But this simply uses for closing the file writes out any unwritten data and releases the lock. This is rarely needed.

There is no Knowledge that is not power.
E-mail: projectnet01@yahoo.com
Company:(not done yet) :)
Status: Currently working with C++ for game developing. And making a musical band.
-Aaron
 
thanks for the reply !
im not sure i understand..

lets say x users want to access this file through perl
is the best method is using LOCK_SH
wmail.jpg


someone knowledge ends where
someone else knowledge starts
 
Ok,

If you wrote a program for me and if the whole tek-tips members wanna use it, ofcourse more than 1 person will be running the program at the same time so your webserver runs always, your program can overlap.

When you LOCK files, it provides a flock function for locking mechanism. Meaning any programs you write need to access the file must also use flock to make sure that no one else is writing to a file at the same time. When you use the lock, it will waits till the next person get a change. For example, if me and you are running the program at the same time, it will let me use the file, then you and so on.

For more info. There is no Knowledge that is not power.
Age: 16
E-mail: projectnet01@yahoo.com
Company:(not done yet) :)
Status: Currently working with C++ for game developing. And making a musical band.
-Aaron
 
is there a 'timeout' ?
if ( so ) {
how many user/same_time max ?
}

is it a way to do it 'all at the same time'
if ( so ) {
how to please ?
} else {
darn
and thanks for all your help
}

wink

wmail.jpg


someone knowledge ends where
someone else knowledge starts
 
I don't think there, i'm not sure. It's done automatically I suppose but you can use sleep function.

sleep(5);

It will let your program sleep for 5 seconds. Full Details: There is no Knowledge that is not power.
Age: 16
E-mail: projectnet01@yahoo.com
Company:(not done yet) :)
Status: Currently working with C++ for game developing. And making a musical band.
-Aaron
 
so lets the code is
Code:
#.....
# lets say file is small <10k and 2 lines...
$file = &quot;drive://path//to//file.file&quot;;
open(&quot;<$file&quot;, FILE) || die &dead($!);
@storer = <file>;
close(FILE) || die &dead($!);
# etc...

this will return &quot;sharing violation&quot;
and this will be all ok ?

[code}
# ...
use Fcntl qw:)flock);

$file = &quot;drive://path//to//file.file&quot;;
open(&quot;<$file&quot;, FILE) || die &dead($!);
flock(FILE, LOCK_SH) || die &dead($!);
@storer = <file>;
close(FILE) || die &dead($!);
# etc...
[/code]

am i understanding well here ?
wmail.jpg


someone knowledge ends where
someone else knowledge starts
 
I'd just like to make a few comments here. These are my own opinions, and not necessarely correct. You have been warned... :)

First of all, you always want to use flock. Getting sharing violations in your code is sloppy coding (unless you for some reason want to).

Never keep a file open longer than you have to. Doing
Code:
sleep(n);
or other unnecessary stuff while having a file opened is bad coding.

Using LOCK_EX means you have an *exlusive* lock. That means that no other applications can access the file at this time. This is the typical thing to do when doing transactions. (Which is doing several operations, and you don't want others to read the data while you're in the middle of this process, as this will give them inconsistent/wrong data).

Using LOCK_SH means that you have full access to the file, but other applications can still *read* from it. Without waiting for you to release the lock! Not good for transactions, obviously. But using it for reading a web-page counter is a perfect task.

Also, if you don't want to wait for a file to become unlocked, you can also use LOCK_NB. If the file is locked, this will return imidiately with another return status.

See for a complete description.

Hope this helps,
Palooka
 
Here's a warning: flock doesn't work under windows. You'll get an error when the program is compiled just prior to running it. Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
OOO thanks 'tsdragon'
what should i do
my server = winNT4.0

thanks
wmail.jpg


someone knowledge ends where
someone else knowledge starts
 
If the file is ONLY going to be read from, then you do not need to lock the file. This is becuase reading a file does NOT change/modify it. Since the file is not changed, as many people who want to read the file can or at least as many as the operating system will let. If the file will also be written to, then you will most certainly need to lock the file during write operation so that data is not overwritten.

hope this helps.

 
ok lets say i have a &quot;one site cgi&quot;
meaning that many user will want
to read / write / append the file
at the same time

what is the better way do avoid timeouts and violation
under win32 ?

thanks alot ---------------------------------------
wmail.jpg


someone knowledge ends where
someone else knowledge starts
 
I'm not sure why win32 gives an error using flock. Maybe it handles the file locking automatically? I'd have to test that. Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
is there some other language i can use to interac with perl
to be sure ...
'I'd have to test that' how r u going to do so ??? ---------------------------------------
wmail.jpg


someone knowledge ends where
someone else knowledge starts
 
The way I'd test that is to create a program that opened a file for output, and then run two copies of it and see what happens.
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
I think you'll find that flock() doesn't work on win9x but works fine on NT, 2k and XP Mike
______________________________________________________________________
&quot;Experience is the comb that Nature gives us after we are bald.&quot;

Is that a haiku?
I never could get the hang
of writing those things.
 
Unfortunately I have only Win98 to test on. Don't have access to NT, 2K or XP. Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
I was wondering about Win32 file locking as well. Before I switched to MySQL for my current program, I just used flatfiles, and when I set up apache on my local machine, I always got &quot;flock() unimplemented on this platform&quot; errors. It would be interesting to find out if Windows does handle the file locking itself.
 
If you look at the documentation for flock() and perldoc perlport, it lists where flock() works and where it doesn't.

*most* annoying that is doesn't work on all win machines... :-( Mike
______________________________________________________________________
&quot;Experience is the comb that Nature gives us after we are bald.&quot;

Is that a haiku?
I never could get the hang
of writing those things.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top