Gossamer Forum
Home : General : Perl Programming :

Lock File - Advice needed

Quote Reply
Lock File - Advice needed
Hi,

Need some advice if I am doing this OK ?

I made a little news-script in perl which is

accessed with an SSI call for every page that is served

Currently this is about 50,000 times a day

but will surely double in the following months

in 70% of the cases the file (of about 20Kb) is

only accessed for read only (CASE-A)

In the other 30% of times, the file is read in

memory, then updated and written back (CASE B)

I am new at file-locking and have copied the

file-locking procedure as here under.

Could some Perl-specialists tell me if I am

doing this OK or if there are better ways to

lock a file that is very very frequently

accessed ??????

####### Here the code I made (Ehhh..copied)

#!/usr/bin/perl

$use_flock = 1;

$news_file = "other/news/news.db";

# CASE-A Read file and insert in page

open (NEWSR, "$news_file");

if ($use_flock == 1){flock(NEWSR,2);}

@news = <NEWSR>;

close(NEWSR);

@news = (); # Flush memory

# Here I process the news and send to page

# This is called with a SSI tag

######################

# CASE-B Insert or delete a line in file

open (NEWSD, "$news_file");

if ($use_flock == 1){flock(NEWSD,2);}

@newsd = <NEWSD>;

close(NEWSD);

@newsd = (); # Flush memory

# Then I Add or Delete a line from the file

# Then I construct the new-file in string "$back_news"

# Now I write "$back_news" as the new file

open (NEWSB, ">$news_file");

if ($use_flock == 1){flock(NEWSB,2);}

print NEWSB "$back_news";

close(NEWSB);

$back_news = ""; # Flush memory

############

Thanks