• using fcntl for record locking

    From Kim Pedersen@21:1/5 to All on Tue Sep 26 08:21:43 2017
    Hi,

    I'm trying to lock a region in a file using fcntl, but it seems that I'm locking the whole file instead of just a range.

    e.g. lock1.rb
    --------------
    require 'fcntl'

    fh = open('testfile', 'w')

    # locking first 10 bytes
    # type, whence, start, len, pid
    lock = [ Fcntl::F_WRLCK, File::SEEK_SET, 0, 10, 0 ].pack('ssqqi')

    fh.fcntl( Fcntl::F_SETLK, lock )

    sleep 20
    -------------

    When locking a different region from a second terminal...

    e.g. lock2.rb
    -------------
    require 'fcntl'

    fh = open('testfile', 'w')

    # locking 10 bytes from offset 20
    lock = [ Fcntl::F_WRLCK, File::SEEK_SET, 20, 10, 0 ].pack('ssqqi')

    fh.fcntl( Fcntl::F_SETLK, lock )

    -----------------------------------
    I get this error.

    locking #<Errno::EAGAIN: Resource temporarily unavailable>

    What is stopping me from locking different regions at the same time?

    I'm using ruby 2.4.1 on RHEL 7.2 linux.

    Kim

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)