Confused about locking a file via file.flock(File::LOCK_EX)

Discussion in 'Ruby' started by Ludwigi Beethoven, Jul 25, 2003.

  1. I am writing a ruby appl under AIX where I need to
    update the /etc/hosts table. I would like to make sure
    that during my update nobody else can update that
    file. I thought I found a solution to my problem with
    the flock method. However, it does not appear to do
    what I thought it will do. To test it, I wrote a
    simple ruby piece of code that, first locks the file,
    second spin-loop forever while I open another window
    and vi the "locked" file. Well, I am able to vi and
    change the file that is supposed to be locked.
    ruby -v returns: ruby 1.6.7 (2002-03-01)
    [powerpc-aix4.3.3.0]

    Any ideas??

    Thank you

    __________________________________
    Do you Yahoo!?
    Yahoo! SiteBuilder - Free, easy-to-use web site design software
    http://sitebuilder.yahoo.com
    Ludwigi Beethoven, Jul 25, 2003
    #1
    1. Advertising

  2. Flock only works within the same process. For example, if you had
    2 concurrent versions of your program running, the second version
    would wait for the first version to release the lock before it opened
    the file. To my knowledge, there isn't any guaranteed way to lock
    all other processes out of a file, but I could be wrong.

    HTH,
    Travis Whitton <>

    In article <>, Ludwigi Beethoven wrote:
    > I am writing a ruby appl under AIX where I need to
    > update the /etc/hosts table. I would like to make sure
    > that during my update nobody else can update that
    > file. I thought I found a solution to my problem with
    > the flock method. However, it does not appear to do
    > what I thought it will do. To test it, I wrote a
    > simple ruby piece of code that, first locks the file,
    > second spin-loop forever while I open another window
    > and vi the "locked" file. Well, I am able to vi and
    > change the file that is supposed to be locked.
    > ruby -v returns: ruby 1.6.7 (2002-03-01)
    > [powerpc-aix4.3.3.0]
    >
    > Any ideas??
    >
    > Thank you
    >
    > __________________________________
    > Do you Yahoo!?
    > Yahoo! SiteBuilder - Free, easy-to-use web site design software
    > http://sitebuilder.yahoo.com
    >
    Travis Whitton, Jul 25, 2003
    #2
    1. Advertising

  3. Ludwigi Beethoven

    ts Guest

    >>>>> "L" == Ludwigi Beethoven <> writes:

    L> what I thought it will do. To test it, I wrote a
    L> simple ruby piece of code that, first locks the file,
    L> second spin-loop forever while I open another window
    L> and vi the "locked" file. Well, I am able to vi and
    L> change the file that is supposed to be locked.

    Well, to understand the problem

    svg% cat b.rb
    #!/usr/bin/ruby
    f = File.new("aa", "w")
    f.flock(File::LOCK_EX)
    puts "file locked #{Process.pid}"

    fork do
    g = File.new("aa", "w")
    if !g.flock(File::LOCK_SH|File::LOCK_NB)
    puts "error : file locked #{Process.pid}"
    end
    end
    Process.wait

    fork do
    g = File.new("aa", "w")
    g.puts "aa"
    end
    Process.wait
    svg%

    svg% cat aa
    cat: aa: No such file or directory
    svg%

    svg% b.rb
    file locked 25340
    error : file locked 25341
    svg%

    svg% cat aa
    aa
    svg%

    a lock work *only* if the process try to acquire a lock before writing in
    the file.

    In my example, the first fork test if the file is locked and give an error
    if it can't have the lock.

    The second fork, don't test and write directly in the file (it bypass the
    lock)

    In your case, vi do like my second fork : it don't try to acquire a lock
    and write in the file


    Guy Decoux
    ts, Jul 25, 2003
    #3
  4. Ludwigi Beethoven

    Mark J. Reed Guest

    On Fri, Jul 25, 2003 at 03:20:16PM +0000, Travis Whitton wrote:
    > Flock only works within the same process.


    Not true. You can have many different programs in many different
    languages running as independent processes all using flock to
    manage access to one or more files. However, flock is only an
    *advisory* lock. It locks files so that no other process can
    access them, IF that other process is bothering to use flock.

    There is no *mandatory* file locking mechanism on UNIX.
    It's always possible to just ignore any locks and open a file
    for writing.

    -Mark
    Mark J. Reed, Jul 25, 2003
    #4
  5. On Sat, Jul 26, 2003 at 12:40:51AM +0900, Travis Whitton wrote:
    > Flock only works within the same process.


    Locking works between processes, but in general locks are "advisory". That
    means that process A and process B must both agree to use the locking
    protocol. If process A locks a file, that doesn't actually prevent process B
    going ahead and writing to the file, if it doesn't bother to get a lock
    first.

    It gets confusing when there are about 3 different locking mechanisms
    (flock, lockf, and some ioctl-locking) but I don't have my Stevens book with
    me so I can't give you the gory details :)

    Cheers,

    Brian.
    Brian Candler, Jul 25, 2003
    #5
  6. Ludwigi Beethoven

    Mike Hall Guest

    Mark J. Reed wrote:

    >There is no *mandatory* file locking mechanism on UNIX.
    >It's always possible to just ignore any locks and open a file
    >for writing.


    I have to disagree. SVR2 had it way back when,
    and Linux used to, last time I checked a few years ago.
    As I recall, I could put a mandatory lock on a file, and even 'cat' would object.

    * Mandatory locking o kernel enforces lock requests o access to records is controlled by the kernel o open, close, read, write system calls honor the locks o requires SGID bit set, with group-execute bit cleared + chmod u+rw,g+rws-x,o+rw file o in Linux, requires a MAND_LOCK mounted filesystem + mount -o remount,mand /filesystem + mount -o remount,nomand /filesystem o a write-lock prevents _any_ process from reading that section

    I have no idea what AIX supports in this regard.
    Mike Hall, Jul 26, 2003
    #6
    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. Timasmith
    Replies:
    4
    Views:
    443
    Bjorn Borud
    Nov 1, 2006
  2. Matt
    Replies:
    5
    Views:
    113
    ara.t.howard
    Jul 16, 2007
  3. John Smith
    Replies:
    2
    Views:
    283
    Brian McCauley
    Dec 1, 2003
  4. flock not locking

    , Aug 10, 2006, in forum: Perl Misc
    Replies:
    5
    Views:
    290
    Ben Morrow
    Aug 11, 2006
  5. Bill H

    File locking (Not Flock)

    Bill H, Aug 28, 2007, in forum: Perl Misc
    Replies:
    6
    Views:
    121
    Mumia W.
    Aug 29, 2007
Loading...

Share This Page