CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Locking Files?

 



alex
stranger

Jun 9, 2001, 8:03 AM

Post #1 of 6 (1473 views)
Locking Files? Can't Post

If i have - for example - a web counter script - when i'm accessing a file and incrementing the number within it, would i need to lock the file to prevent multiple users initiating the script and overwriting the data? If so, then would it still register them running the counter?
It's hard to test the scenario really.. and i just wondered if anyone could please help explain the process. Thanks.



randor
User

Jun 9, 2001, 2:21 PM

Post #2 of 6 (1466 views)
Re: Locking Files? [In reply to] Can't Post

alex..

this is how i incriment files...


&oops('CART_ID') unless (open CART_ID, "+<dir/to/info.dat");
&oops('FLOCK') unless (flock(CART_ID, LOCK_EX));
chomp ($number) = <CART_ID>;
$number++;
seek(CART_ID, 0, 0);
print CART_ID "$number\\n";
close CART_ID;


I Hope this helps...

perl programmers don't die.. they just start writing a new script.


randor
User

Jun 9, 2001, 2:38 PM

Post #3 of 6 (1466 views)
Re: Locking Files? [In reply to] Can't Post

sorry.. the &oops refers to a sub routine that catches the error and shows it to me..

I Hope this helps...

perl programmers don't die.. they just start writing a new script.


Mortimer
journeyman

Jun 9, 2001, 4:17 PM

Post #4 of 6 (1464 views)
Re: Locking Files? [In reply to] Can't Post

> If i have - for example - a web counter script - when
> i'm accessing a file and incrementing the number within
> it, would i need to lock the file to prevent multiple
> users initiating the script and overwriting the data?

Definitely.

> If so, then would it still register them running the
> counter?

Yes. Place an exclusive lock on the file. This ensures that only one process at a time can write to the file.
Any other process(es) coming along wanting to modify the file have to wait until the lock is released. You can
read more here...
http://www.dmscripts.com/quickhacks/no_flock_lock.html

randor, your code fails because...

Code
 chomp ($number) = <CART_ID>;

Can't modify scalar safe chop in scalar assignment...etc.

You need to...

Code
 chomp( $number = <CART_ID> );

...which will give $number a chance to be assigned a value before you chomp() it.

Also, you're escaping the escape for the newline when you print the number back to the file, so the actual characters \n are written instead.

Cheers,
Dave.
www.dmscripts.com
davemortimer@bigpond.com



alex
stranger

Jun 10, 2001, 2:04 AM

Post #5 of 6 (1456 views)
Re: Locking Files? [In reply to] Can't Post

Thanks very much to both of you!
That's exactly the explanation i was looking for :)
I wasn't sure if locking the file would completely block out other users..but now i know it puts them in a que, that's what i wanted to hear, heh.



randor
User

Jun 10, 2001, 7:47 AM

Post #6 of 6 (1450 views)
Re: Locking Files? [In reply to] Can't Post

yeap.. a couple crammical errors.. thats what i get for not testing it before posting.. thank you for pointing them out to me:)

I Hope this helps...

perl programmers don't die.. they just start writing a new script.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives