using flock for concurrency control

B

Babu

Hi,
I have an application that maintains its configuration file. This file
is read frequently but written to very rarely. There may be several
processes attempting to read and write to the config file.
The application reads the configuration file into a buffer, inflicts
changes on the buffer and writes the buffer to the file.
The intended logic for writing the buffer into the config file is as
follows

# create a copy of config.txt say as config.txt.bak
open CONFIG_FILE, "config.txt";
flock CONFIG_FILE, LOCK_EX;
eval {
print CONFIG_FILE "blah blah blah";
close CONFIG_FILE;
}
if ($@)
{
#CRITICAL POINT
#move config.txt.bak to config.txt
}

An issue arises when the writer process is at "CRITICAL POINT" and
another reader process attempts to read the config. File. Since the
write operation has failed, the current config. File is corrupt and the
original config. File has not been restored yet. So the reader process
gets into trouble.

Any idea on how to tackle this?

TIA,
Babu
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top