Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.ruby > #7372

using fcntl for record locking

Newsgroups comp.lang.ruby
Date 2017-09-26 08:21 -0700
Message-ID <c6176013-d3ee-4c23-aeaa-a123a6bba7ff@googlegroups.com> (permalink)
Subject using fcntl for record locking
From Kim Pedersen <kimersen@gmail.com>

Show all headers | View raw


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

Back to comp.lang.ruby | Previous | Next | Find similar


Thread

using fcntl for record locking Kim Pedersen <kimersen@gmail.com> - 2017-09-26 08:21 -0700

csiph-web