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: Intermediate: Re: [yapp] Database Unexpected Erasure Problem: Edit Log



gregarios
stranger

May 29, 2002, 2:39 PM


Views: 3594
Re: [yapp] Database Unexpected Erasure Problem

Ok... I made a small program that I'm hoping tested my theory correctly.

Here is my test program:

Code
#!/usr/bin/perl5 -wT  
use strict;
use Fcntl qw(:DEFAULT :flock);
$| = 1;

sysopen(ADDCTX, "ads.dta", O_RDWR|O_EXLOCK) or die "Could not open and lock file: $!";
sysopen(ADTEST, "ads.dta", O_RDWR|O_EXLOCK) or die "Could not open and lock file: $!";

truncate(ADTEST,0) or die "Could not truncate file: $!";
close(ADTEST) or die "Could not close file: $!";
my @ads = <ADDCTX>;
if ($ads[0]) {print "Ad list: @ads"} else {print "Truncated File"};
close(ADDCTX);
exit;


Here are the results. I'll show you the changes to the two lines of code that matter and give you a description of the outcome.

These two examples of code caused an endless wait period... presumably on the "O_EXLOCK" lock. Interestingly enough, in the second set, the second sysopen command shown here wouldn't apply an exclusive lock while the file was previously open, even though the first open did not apply a lock. Niether ever truncated:

Code
sysopen(ADDCTX, "ads.dta", O_RDWR|O_EXLOCK) or die "Could not open and lock file: $!"; 
sysopen(ADTEST, "ads.dta", O_RDWR|O_EXLOCK) or die "Could not open and lock file: $!";

or

Code
sysopen(ADDCTX, "ads.dta", O_RDWR) or die "Could not open and lock file: $!"; 
sysopen(ADTEST, "ads.dta", O_RDWR|O_EXLOCK) or die "Could not open and lock file: $!";


These two examples of code caused the file to be truncated:

Code
sysopen(ADDCTX, "ads.dta", O_RDWR) or die "Could not open and lock file: $!"; 
sysopen(ADTEST, "ads.dta", O_RDWR) or die "Could not open and lock file: $!";

or

Code
sysopen(ADDCTX, "ads.dta", O_RDWR|O_EXLOCK) or die "Could not open and lock file: $!"; 
sysopen(ADTEST, "ads.dta", O_RDWR) or die "Could not open and lock file: $!";


I would be grateful to hear what you have to say about this. Judging from my examples, I would think this is a great way to lock files safely... at least when running in FreeBSD?

Code
sysopen(ADDCTX, "ads.dta", O_RDWR|O_EXLOCK) or die "Could not open and lock file: $!";

Anyone... please give me your opinions on this. Thank you for your support. :-)

Greg J Piper
[url=http://www.macpicks.com]MacPiCkS



(This post was edited by gregarios on May 29, 2002, 2:41 PM)


Edit Log:
Post edited by gregarios (stranger) on May 29, 2002, 2:41 PM


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

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