# Process based locks for files

**URL:** <https://www.ruby-forum.com/t/process-based-locks-for-files/66865>\
**Category:** Ruby\
**Created:** [August 29, 2006, 10:32pm UTC](https://www.ruby-forum.com/t/process-based-locks-for-files/66865 "2006-08-29T22:32:43Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Radu\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ec9cab/32.png) [@Radu\_S](https://www.ruby-forum.com/u/Radu_S)\
**Post date:** [August 29, 2006, 10:32pm UTC](https://www.ruby-forum.com/t/process-based-locks-for-files/66865/1 "2006-08-29T22:32:43Z")

</div>

Hi,

I have two programs accessing the same file. Both programs open and  
close the file multiple times while running (for example reading it  
first, and then writing it back using global functions).

In order to avoid race conditions i was wondering if it’s possible to  
lock the file at the start of the running process and then unlock it at  
the end. During that time the running process should be able to open and  
close the file without problems.

Thanks,  
Radu S.

---

<div class="post-metadata">

**Author:** ![Radu\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ec9cab/32.png) [@Radu\_S](https://www.ruby-forum.com/u/Radu_S)\
**Post date:** [August 29, 2006, 10:44pm UTC](https://www.ruby-forum.com/t/process-based-locks-for-files/66865/2 "2006-08-29T22:44:39Z")

</div>

Radu S. wrote:

> Hi,
> 
> I have two programs accessing the same file. Both programs open and  
> close the file multiple times while running (for example reading it  
> first, and then writing it back using global functions).
> 
> In order to avoid race conditions i was wondering if it’s possible to  
> lock the file at the start of the running process and then unlock it at  
> the end. During that time the running process should be able to open and  
> close the file without problems.

There’s File#flock, but keep in mind that flock is only an advisory lock  
(i.e. it only notifies cooperating processes that try and get a lock  
themselves) and is notoriously flakey on NFS mounts.

---

<div class="post-metadata">

**Author:** ![Radu\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ec9cab/32.png) [@Radu\_S](https://www.ruby-forum.com/u/Radu_S)\
**Post date:** [August 29, 2006, 10:56pm UTC](https://www.ruby-forum.com/t/process-based-locks-for-files/66865/3 "2006-08-29T22:56:46Z")

</div>

On Wed, 30 Aug 2006, Mike F. wrote:

> > close the file without problems.
> 
> There’s File#flock, but keep in mind that flock is only an advisory lock  
> (i.e. it only notifies cooperating processes that try and get a lock  
> themselves) and is notoriously flakey on NFS mounts.

I don’t see a mention of your platform. However, you might consider Ara  
Howard’s lockfile.rb. If is an NFS safe file lock library that works  
well. It just doesn’t work on Windows.

If you need something that can work on both Windows and not-windows,  
email  
me. I have a hacked and a bit simplified version that will autodetect  
whether the primary locking scheme will work or not, and will fall back  
to  
flock based locking. It remembers the result of the autodetection so  
subsequent locks opened in the same process later don’t incur the cost  
of  
detection again.

Ara’s library works well, and I believe he uses it extensively.

My hacked version probably works less well because it is an undertested  
ugly hack, but in my (less extensive) use of it, I haven’t encountered  
any  
problems so far.

Find his lockfile.rb here:

[http://codeforpeople.com/lib/ruby/lockfile/](http://codeforpeople.com/lib/ruby/lockfile/)

Kirk H.

---

<div class="post-metadata">

**Author:** ![Radu\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ec9cab/32.png) [@Radu\_S](https://www.ruby-forum.com/u/Radu_S)\
**Post date:** [August 30, 2006, 12:16am UTC](https://www.ruby-forum.com/t/process-based-locks-for-files/66865/4 "2006-08-30T00:16:57Z")

</div>

Radu S. wrote:

> Hi,
> 
> I have two programs accessing the same file. Both programs open and  
> close the file multiple times while running (for example reading it  
> first, and then writing it back using global functions).
> 
> In order to avoid race conditions i was wondering if it’s possible to  
> lock the file at the start of the running process and then unlock it at  
> the end. During that time the running process should be able to open and  
> close the file without problems.

If both programs are willing to cooperate with each other, why not use a  
flag file, as is often done to lock and unlock various resources in  
Linux?  
This approach has the advantage of being platform-neutral and doesn’t  
require any explicit file-locking facilities.

* * *

#!/usr/bin/ruby -w

flag\_file = “FLAGFILE”

puts “Waiting for access …”  
while FileTest.exists? flag\_file  
sleep 1  
end

puts “Creating flag file …”  
ff = File.open(flag\_file,“w”) {}

puts “Processing …”  
sleep 5

puts “Removing flag file …”  
File.delete flag\_file

* * *

There is an obvious collision possibility in this arrangement, that may  
or  
may not be an issue.

---

<div class="post-metadata">

**Author:** ![Radu\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ec9cab/32.png) [@Radu\_S](https://www.ruby-forum.com/u/Radu_S)\
**Post date:** [August 30, 2006, 2:07am UTC](https://www.ruby-forum.com/t/process-based-locks-for-files/66865/5 "2006-08-30T02:07:09Z")

</div>

Hi ara,

removed\_email\_address@domain.invalid wrote:

> plus, it fails horriblly on nfs. this does all that and much, much more -  
> including preventing stale lock starvation
> 
> [http://codeforpeople.com/lib/ruby/lockfile/lockfile-1.4.0/README](http://codeforpeople.com/lib/ruby/lockfile/lockfile-1.4.0/README)  
> [http://codeforpeople.com/lib/ruby/lockfile/lockfile-1.4.0/](http://codeforpeople.com/lib/ruby/lockfile/lockfile-1.4.0/)  
> [http://rubyforge.org/frs/?group\_id=1024&release\_id=3473](http://rubyforge.org/frs/?group_id=1024&release_id=3473)
> 
> gen install lockfile

I’m looking into this, it seems to be what I am looking for.

Thanks,  
Radu S.

---

<div class="post-metadata">

**Author:** ![Radu\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ec9cab/32.png) [@Radu\_S](https://www.ruby-forum.com/u/Radu_S)\
**Post date:** [August 30, 2006, 12:49am UTC](https://www.ruby-forum.com/t/process-based-locks-for-files/66865/6 "2006-08-30T00:49:00Z")

</div>

On Wed, 30 Aug 2006, Paul L. wrote:

> > the end. During that time the running process should be able to open and
> 
> puts “Processing …”  
> sleep 5
> 
> puts “Removing flag file …”  
> File.delete flag\_file
> 
> * * *
> 
> There is an obvious collision possibility in this arrangement, that may or  
> may not be an issue.

## plus, it fails horriblly on nfs. this does all that and much, much more

including preventing stale lock starvation

[http://codeforpeople.com/lib/ruby/lockfile/lockfile-1.4.0/README](http://codeforpeople.com/lib/ruby/lockfile/lockfile-1.4.0/README)  
[http://codeforpeople.com/lib/ruby/lockfile/lockfile-1.4.0/](http://codeforpeople.com/lib/ruby/lockfile/lockfile-1.4.0/)  
[http://rubyforge.org/frs/?group\_id=1024&release\_id=3473](http://rubyforge.org/frs/?group_id=1024&release_id=3473)

gen install lockfile

regards.

-a
