Forum: Ruby file locking

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Mark V. (Guest)
on 2006-01-22 19:47
(Received via mailing list)
I'm trying to understand usage of file.flock(File::LOCK_SH) which
obtains a shared lock. The documentation says it allows other
processes to also obtain a shared lock. What's the point of a lock if
it isn't locking anybody else out? There must be a use case for this
that I'm not seeing.

Also, the documentation for File.flock says that it isn't supported on
all platforms. If you know of platforms that definitely do or do not
support this, I'd like to hear about them. I'm particularly interested
in XP and Fedora Core Linux.
Eero S. (Guest)
on 2006-01-22 21:55
Mark V. wrote:
> I'm trying to understand usage of file.flock(File::LOCK_SH) which
> obtains a shared lock. The documentation says it allows other
> processes to also obtain a shared lock. What's the point of a lock if
> it isn't locking anybody else out? There must be a use case for this
> that I'm not seeing.

A shared lock is usually implemented as a read-lock; in
other words, multiple processes can read from the file
at the same time but none may modify it.

> Also, the documentation for File.flock says that it isn't supported on
> all platforms. If you know of platforms that definitely do or do not
> support this, I'd like to hear about them. I'm particularly interested
> in XP and Fedora Core Linux.

This I do not know.


E
Jeremy H. (Guest)
on 2006-01-24 18:31
(Received via mailing list)
On 2006-01-22, Mark V. <removed_email_address@domain.invalid> wrote:

> I'm trying to understand usage of file.flock(File::LOCK_SH)
> ... What's the point of a lock if it isn't locking anybody else out?

A shared lock locks out anyone who wants an exclusive lock.  Typically
you want an exclusive lock for writing to a file and a shared lock for
reading it.  That's because it's OK for several people to read a file
simultaneously, but if someone wants to write to it then no-one else
should be allowed to read *or* write, otherwise hilarity may ensue.

Cheers,

Jeremy H.
This topic is locked and can not be replied to.