[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

process based locks for files

Radu Spineanu

8/29/2006 8:32:00 PM

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 Spineanu


5 Answers

Mike Fletcher

8/29/2006 8:45:00 PM

0

Radu Spineanu 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.

--
Posted via http://www.ruby-....

khaines

8/29/2006 8:55:00 PM

0

Paul Lutus

8/29/2006 10:15:00 PM

0

Radu Spineanu 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.

--
Paul Lutus
http://www.ara...

Ara.T.Howard

8/29/2006 10:48:00 PM

0

Radu Spineanu

8/30/2006 12:07:00 AM

0

Hi ara,

ara.t.howard@noaa.gov 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....
> http://codeforpeople.com/lib/ruby/lockfile/lockf...
> http://rubyforge.org/frs/?group_id=1024&relea...
>
> gen install lockfile
>

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

Thanks,
Radu Spineanu