Subject: Re: File Lock in TCL - DN [1]


Jean-Claude Wippler <jcw@equi4.com> - 03 Jun 1999 - comp.lang.tcl

 Tony Metters wrote:
 >
 > Is it possible to obtain a lock on a file using TCL ?

 Here's a mechanism I once got from Alexandre Ferrieux:

         Lock:   open $file {CREAT EXCL WRONLY} ;# you can even
                                                 # write info into it
         Unlock: file delete $file

 It works well in the most common cases.  It gets (a lot) hairier for
 solutions which do not use a separate lock file.

 Some Unix systems use flock, others lockf, others ioctl, and Windows has
 yet another library function for it (sopen).  When used with NFS, you
 have to accept that:

   - some systems will ignore locks, or ignore only across the network
   - some systems have no advisory locks, others only advisory locks
   - some systems can fatally lock up multiple machines (trhough lockd)

 So I guess the answer is: in pure Tcl, you can use the lines shown above
 for basic locking, but the more genreal case is very complex (and not a
 Tcl-specific problem).

 I ended up doing different things on Windows, Linux, and Solaris, and
 accepting the fact that locks over NFS were impossible to get right.
 The code I came up with is buried in this source file, around line 300:
     http://www.equi4.com/metakit/mk4tcl/mk4tcl.cpp

 Some good locking reference material I found on the net a while back,
 were the sources of IMAP (http://www.washington.edu/imap/) and procmail
 (ftp://ftp.informatik.rwth-aachen.de/pub/packages/procmail/).

 -- Jean-Claude

 P.S. My server is being moved, it/I may be inaccessible for a day or so.

Last modified
1999-09-27

(195.108.246.50)

Note: you are looking at
the snapshot of an old wiki
- much of this information
is likely to be very outdated