how to lock files

Posted on 2005-04-06
Medium Priority
Last Modified: 2013-12-25
I have a working cgi  which read/writes config and status datas from a file, a write
attemp is done in lt 1 sec, I see problems if more then 1 user updates the file at the
same time , how can i avoid that more then 1 user has this file handle open  ?
Very cool would be to know who it is, like a user id, who requests the write operation.
i'm doing http auth. through apache and have the name already in my environ. .

Question by:MKrauss
  • 5
  • 3
  • 3

Author Comment

ID: 13720003
I forgot to stat that i'm using perl on a linux base ....
LVL 51

Expert Comment

ID: 13720187
if your CGI is perl or C, then use flock(), but don't expect that it is 99% safe.
Locking on modern filesystems is known to be a nightmare :-(

The only reliable method, which works on mounted filesystems and with different hosts (think of a load balanced web server working on the same DocumentRoot), is to use another file as semaphore.
This works like this (pseudo code):

  timestamp="`date +%s``uname -n`$$"
  echo $timestamp > /path/to/lock
  sleep 0.5sec
  read=`cat /path/to/lock`
  if ($read == $timestamp) {
    heureca, got the lock
    do my save action
    rm /path/to/lock
  } else {
   someone else kicked me out

Author Comment

ID: 13720475
hmmm, somehow that looks a bit unfashion .... it's perl what i'm using,
it's sure (or i assume) that all write request will be made from a single
cgi script so athe load balanced issue does not affect me, is there a
aternative way due to this facts ?
Lets assume i go for timestamp/lock file solution, wouldnt it be possible
to use the lock file owner somehow to see which user collide with the other ?
I'm just thinking about a way to display who's conflicting with who ...
Many thanks for helping ....
Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

LVL 18

Accepted Solution

kandura earned 400 total points
ID: 13720508
Hi ahoffmann,
> The only reliable method, which works

how do you figure that?
I routinely use flock over nfs (v3) mounts on heavily trafficked web sites, and have never had any problems.

Here's what I use in perl:

    use 5.004;
    use Fcntl qw(:DEFAULT :flock);

    # for reading
        open(FH, "< $filename")  or die "can't open $filename: $!";
        unless (flock(FH, LOCK_SH | LOCK_NB)) {
            $| = 1;
            flock(FH, LOCK_SH)  or die "can't lock $filename: $!";
        local($/) = undef;
        $data = <FH>;
        close(FH) or die "can't close $filename: $!";

    # for writing
        sysopen(FH, "$filename", O_WRONLY | O_CREAT)
            or die "can't open $filename: $!";
        flock(FH, LOCK_EX)
            or die "can't lock $filename: $!";
        truncate(FH, 0)
            or die "can't truncate $filename: $!";
        # now write to FH
        print FH $data or die "can't write to $filename: $!";
        close FH or die "can't close $filename: $!";


Author Comment

ID: 13720555
oh dear, i havent check your recommende flock option, think
that will do it for me, i'll try and come back...
But with flock i have no chance to figure out who it is or do i ?
LVL 18

Expert Comment

ID: 13720628
> But with flock i have no chance to figure out who it is or do i ?

why would you care? this code will make sure that each user gets his turn at writing to the file, so there are no conflicts.

Author Comment

ID: 13720769
my only concern was to try to get who makes what, thats maybe oversized, but you're wright, this shouldnt conflict me,
will try and come back ... thx
LVL 51

Expert Comment

ID: 13722249
> I routinely use flock over nfs (v3) mounts on heavily trafficked web sites, and have never had any problems.
you're lucky ;-)

>  hmmm, somehow that looks a bit unfashion ..

> ..  will be made from a single cgi script ..
that's exactly your problem!
if there is more than one request which starts this CGI, then you have at least 2 of them running, both trying to get the lock
even the flock() function is an atomic one and hopefully not interuptable, you have to beat other dragons
in particular disk caches

You can be very safe if all happens on the same host and with a local filesystem. But if it is NFS, then  you run into problems more or less quickly .. believe me, or simply read
   perldoc -f flock
LVL 18

Expert Comment

ID: 13722349
> you're lucky ;-)

Maybe. But in my experience, nfs v3 comes with an excellent locking daemon, which works fine on multiple platforms, and across multiple mounts.
I even used Microsoft's Interix nfs server in conjunction with several Debian web servers, and that did locking properly as well.
LVL 51

Assisted Solution

ahoffmann earned 400 total points
ID: 13724475
kandura, it's not always NFS itself, just partialy, sometimes, somehow ...
I know that Sun's NFS (wether local or remote) caches data to improve performance, and the SCSI driver caches again ... And Sun does this since SunOS early days :-(( they know that, wrote some "confidential" papers, and never fixed it, just giving the workaround to disable cahing at all which then makes NFS unusable for obvious reason.

I won't complain or say that flock() *always* fails, but want to say that you should be aware of such problems. If someone asks for exclusive locks, (s)he  expects them working reliable, and that's not the case on NFS with flock(), neither perl's nor native libc's.

Author Comment

ID: 13728949
Thanks for the interesting conversation,the  NFS issue does not affect me so i gues it's save
to use flock.

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial will discuss fancy secure registration forms, with AJAX technology support. In this article I assume you already know HTML and some JS. I will write the code using WhizBase Server Pages, so you need to know some basics in WBSP (you mig…
This article will show, step by step, how to integrate R code into a R Sweave document
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses
Course of the Month17 days, 2 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question