using flock for concurrency control

Discussion in 'Perl' started by Babu, Mar 1, 2005.

  1. Babu

    Babu Guest

    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
    Babu, Mar 1, 2005
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. thakadu
    Replies:
    3
    Views:
    516
    thakadu
    Nov 4, 2005
  2. Ckid

    flock() working mechanism..

    Ckid, Mar 7, 2005, in forum: C Programming
    Replies:
    1
    Views:
    345
    Peter Nilsson
    Mar 7, 2005
  3. Time Waster

    flock usage

    Time Waster, Aug 31, 2007, in forum: C Programming
    Replies:
    4
    Views:
    5,551
  4. Replies:
    1
    Views:
    1,817
    Jens Henrik Leonhard Jensen
    Jun 22, 2008
  5. Seb

    flock trouble

    Seb, Sep 8, 2008, in forum: Python
    Replies:
    1
    Views:
    233
    Antoon Pardon
    Sep 22, 2008
Loading...

Share This Page