Re: Correct file locking techniques

From: Bob Walton (invalid-email_at_rochester.rr.com)
Date: 06/27/04


Date: Sun, 27 Jun 2004 02:01:17 GMT

Robert TV wrote:

> Hi, I am asking foir advice on which form of code i've written below is the
> correct and safe way to lock a file. I've done some reading on" use Fcntl

I have found that the most portable and least hassle way to lock files
is to use a lock file. This is a file which is used exclusively for the
purpose of establishing a lock -- it can be empty, or have any contents
you want, but the data in the lock file would be unused and unimportant.
   This technique is particularly useful when doing stuff which is a bit
out of the ordinary, such as locking DBM-type files to which you wish to
tie a hash, for example.

If you want to write a file, open the lock file for write (that will, at
least on some systems, wipe out the contents of the lock file), then
establish an exclusive lock on it. Then go do whatever you wanted the
exclusive lock for (write another file, tie a hash to a DBM-type file,
or any other operation requiring exclusive access to a resource). Then,
when you are all done with your lock, close the lock file.

If you want to read a file, open the lock file for reading, and
establish a shared lock on it. Go read your data file or do whatever
you wanted the shared lock for. When done, close the lock file.

To the best of my knowledge, that works flawlessly on at least local
files on every OS that supports locking (that, by the way, rules out
Windoze 95/98/98SE). However, you should carefully and thoroughly read
everything your OS has to say about file locking, particularly if you
are considering locking files accessed via a network.

You will want to note that locking a file does not prevent other
processes from read/writing/deleting/whatevering a file. *All* it does
is establish a lock -- the other processes have to cooperate with the
lock in order for it to work. An exclusive lock means other processes
seeking a lock will block until the exclusive lock is released; a shared
lock means other processes seeking an exclusive lock will block until
all the shared locks are released.

I find it convenient to write a couple of subs to do the locking, like
[untested]:

    sub lockex{
      open LOCK,">lockfile.txt"
        or die "Oops, couldn't open lockfile.txt for write, $!";
      flock(LOCK,LOCK_EX)
        or die "flock bombed out in lockex, $!";
    }
    sub locksh{
      open LOCK,"lockfile.txt"
        or die "Oops, couldn't open lockfile.txt for read, $!";
      flock(LOCK,LOCK_SH)
        or die "flock bombed out in locksh, $!";
    }
    sub unlock{
      close LOCK or die "Oops, couldn't close lockfile.txt, $!";
    }

HTH.

...
> Robert

-- 
Bob Walton
Email: http://bwalton.com/cgi-bin/emailbob.pl


Relevant Pages

  • [UNIX] File Locking Local Denial of Service (Sendmails Impact)
    ... Any application which uses either flockor fcntlstyle locking or ... Since this attack requires a user to use their own account to lock a file, ... process holds an exclusive lock on a file, no other process can obtain an ... File locking is used throughout Sendmail for a variety of files including ...
    (Securiteam)
  • Re: Creating a Shared Lock
    ... A second thread that is called once every second ... needs to lock so the other function does not get called while its running. ... In SQL you would do this by creating a shared lock on the first function ... shared access but once a thread has an exclusive lock any shared locks will ...
    (microsoft.public.dotnet.languages.csharp)
  • flock incorrectly detects deadlock on 7-stable and current
    ... exclusive lock but fails with a deadlock avoided. ... Process 1 requests and gets a shared lock ... Process 2 requests and blocks for an exclusive lock ...
    (freebsd-stable)
  • Re: Quick question re: locks and triggers
    ... Yeah, without an exclusive lock held during the trigger, if you decided to ... held for the length of the transaction in terms of blocking other WRITERS to ...
    (microsoft.public.sqlserver.programming)
  • Re: 54 Processors?
    ... The original problem with MP was lock processing, ... Some locks are exclusive and the response of a processor that needed the ... exclusive lock but could not get it was to spin, i.e. execute CS or CDS in ... The information contained in this communication (including any attachments ...
    (bit.listserv.ibm-main)