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:
File locking and preserving data.

 



DrZed
User

Jun 16, 2000, 12:14 PM

Post #1 of 5 (384 views)
File locking and preserving data. Can't Post

I need to understand what actions risk a loss of data. From what I understand of file locking, a request to lock a file will prevent other 'locked' access to the file, but routines which don't lock the file will still have access.

My question is, what actions risk data loss?

For example: if two applications both try to append the same file at the same time, is there any risk of data loss? Or is it only when the file is being written to that there is risk? If I understand, while I file is being written to, attempts to read the file will result in no data being read (i.e. it read the file as blank). Does the same apply to appending files?

I'm working on a forum script and I would like to make sure my file handling is clean since a bug related to file i/o is difficult to test for without a lot of traffic and I rather not have things go wrong after the program's release.

Dr. Zed



dws
Deleted

Jun 17, 2000, 12:12 PM

Post #2 of 5 (384 views)
Re: File locking and preserving data. [In reply to] Can't Post

Are you asking about the risks if you don't flock?

A particular risk when appending to files is that between the time you seek to the end and the time you actually write, some other process will get a write in ahead of you and increase the file size, leaving you positioned at what has just become a location within the file. Then you get your write in, quite probably corrupting the file.

That's why in (working) examples you'll see a seek to EOF done right after a successful flock.


[This message has been edited by dws (edited 06-17-2000).]


DrZed
User

Jun 17, 2000, 1:04 PM

Post #3 of 5 (384 views)
Re: File locking and preserving data. [In reply to] Can't Post

Thanks for the info.

I've been flocking files that get their data totally rewritten, but I hadn't been flocking files which are only appended. I guess that I should be flocking those files too.

Ok, just to make sure I understand, there are four different flags which can be passed to flock.

1 - Shared lock
2 - Exclusive lock
4 - Nonblocking lock (will fail rather than wait)
8 - Unlock

So, I am assuming that a 'shared lock' is something that would only be used to read a file. Something to make sure that the file isn't exclusivly locked before it tried to read data. Since multiple reads won't be a problem, they 'share'.

Exclusive lock is fairly self-explanitory. It's the classic definition of a 'locked file'.

I am assuming that a non-blocking lock is something that you would combine with either a shared or exclusive lock. For example, 5 would be a shared non-blocking lock and 6 would be an exclusive non-blocking lock.

Unlock is also self-explanitory.

So, in essense, the only valid values I can forsee are 1, 2, 5, 6 and 8.

Is this correct?

Dr. Zed


Cure
User

Jun 17, 2000, 1:26 PM

Post #4 of 5 (384 views)
Re: File locking and preserving data. [In reply to] Can't Post

use the fcntl module

Example

#!/usr/bin/perl
use fcntl ":flock";

open(FH, ">>database.txt") or die $!;
flock(FH, LOCK_EX);
print "Requested Exclusive Lock\n";
flock(FH, LOCK_UN);
close FH;


open(FH, "database.txt") or die $!;
flock(FH, LOCK_SH);
$cure = <FH>;
flock(FH, LOCK_NB);
close FH;


Cure


DrZed
User

Jun 17, 2000, 5:15 PM

Post #5 of 5 (384 views)
Re: File locking and preserving data. [In reply to] Can't Post

First, I've been avoiding using any modules. First, my main reference doesn't give me info on them. Second, I want to learn as much as possible, which means doing things myself rather than relying on modules. I'll use them if I have to, but so far I haven't used any.

From what my reference says, if a system doesn't use flock(), Perl 5.001m and later will emulate it using fcntl(), so I think flock is the best way to go.

Do I need the module to use flock? Or does it just define the $LOCK_SH, _EX, _NB and _UB variables?

If I need it to flock, I'll use it. If it's just for the variables, I can use numeric values and comments.

BTW, in the following:

open(FH, "database.txt") or die $!;
flock(FH, LOCK_SH);
$cure = <FH>;
flock(FH, LOCK_NB);
close FH;

Did you actually mean _NB? I'm assuming you wanted _UN.

BTW, I just read the following:
"OPERATION is one of LOCK_SH, LOCK_EX, or LOCK_UN, possibly combined with LOCK_NB."

I'm assuming that you would only combine _NB with one of the first two. Combining it with LOCK_UN wouldn't make sense to me.

Dr. Zed

 
 


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

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