# File locking

**URL:** <https://www.ruby-forum.com/t/file-locking/96040>\
**Category:** Ruby\
**Created:** [January 22, 2006, 6:47pm UTC](https://www.ruby-forum.com/t/file-locking/96040 "2006-01-22T18:47:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![markcasio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9bcb4/32.png) [@markcasio](https://www.ruby-forum.com/u/markcasio)\
**Post date:** [January 22, 2006, 6:47pm UTC](https://www.ruby-forum.com/t/file-locking/96040/1 "2006-01-22T18:47:30Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![markcasio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9bcb4/32.png) [@markcasio](https://www.ruby-forum.com/u/markcasio)\
**Post date:** [January 22, 2006, 8:55pm UTC](https://www.ruby-forum.com/t/file-locking/96040/2 "2006-01-22T20:55:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![markcasio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9bcb4/32.png) [@markcasio](https://www.ruby-forum.com/u/markcasio)\
**Post date:** [January 24, 2006, 5:31pm UTC](https://www.ruby-forum.com/t/file-locking/96040/3 "2006-01-24T17:31:19Z")

</div>

On 2006-01-22, Mark V. [removed\_email\_address@domain.invalid](mailto: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.
