[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: How do you use flock and clean up lock files?

John Carter

8/14/2006 3:57:00 AM

2 Answers

Christopher Brown

8/14/2006 4:12:00 AM

0

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Sorry to jump in on the thread like this...
John are you using flock because it's the easiest & most available
way to sync across processes? If you had easier access to one of the
other sync primitives, would you be using it (i.e. a mutex)?
I'm just wondering what the dominant pattern is, and if we are using
it because something better hasn't come along.

Peace,
Chris


On 14 Aug 2006, at 5:57 AM, John Carter wrote:

> On Mon, 14 Aug 2006, Francis Cianfrocca wrote:
>
>> Don't worry about leaving lock files around. If you really don't
>> like it,
>> put it in /tmp or /dev/shm.
>
> Sigh! Just seems so...messy.
>
> Sigh! I even thought of using whatever "fuser -v" uses, but strace
> tells
> me it scans /proc! And that's gives me worse aesthetic collywobbles.
>
>
> John Carter Phone : (64)(3) 358 6639
> Tait Electronics Fax : (64)(3) 359 4632
> PO Box 1645 Christchurch Email : john.carter@tait.co.nz
> New Zealand
>
> Carter's Clarification of Murphy's Law.
>
> "Things only ever go right so that they may go more spectacularly
> wrong later."
>
> From this principle, all of life and physics may be deduced.
>

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.3 (Darwin)

iD8DBQFE3/fZrOGxDZoCCzURAiyiAJ95KDkPwOh54dSChGvFHEN1Eu8GqwCdGBCM
n5wt4dgBkie9fZIeJhnL0Kg=
=Xxgx
-----END PGP SIGNATURE-----

Mauricio Fernández

8/14/2006 9:59:00 AM

0

On Mon, Aug 14, 2006 at 12:57:09PM +0900, John Carter wrote:
> On Mon, 14 Aug 2006, Francis Cianfrocca wrote:
>
> >Don't worry about leaving lock files around. If you really don't like it,
> >put it in /tmp or /dev/shm.
>
> Sigh! Just seems so...messy.

You can check if the file you got a lock on is the one presently on the FS.
If the lockfile was unlinked, File.stat will fail; if another process created
a file with the same name, the ino will differ. You can detect both situations
and try again:

$ cat lock.rb

def lock(filename)
dest = nil
loop do
dest.close if dest
dest = File.open(filename, "ab")
dest.flock(File::LOCK_EX)
old_stat = dest.stat
new_stat = File.stat(filename) rescue nil
break if new_stat and
old_stat.dev == new_stat.dev and
old_stat.ino == new_stat.ino
end
yield
ensure
File.unlink(filename) rescue nil
dest.close rescue nil
end

LOCKFILE = "lockfile"

$stdout.sync = true

t = Time.new
fork do
lock LOCKFILE do
puts "A"
p File.exist?(LOCKFILE)
puts "Sleeping in A"
sleep 10
end
end

sleep 1
fork do
lock LOCKFILE do
puts "B"
p File.exist?(LOCKFILE)
puts "Sleeping in B"
sleep 10
end
end

sleep 15

puts "Attempting to get lock in C"
lock LOCKFILE do
puts "C"
p File.exist?(LOCKFILE)
end


puts "Total time: #{Time.new - t}"

$ ruby lock.rb
A
true
Sleeping in A
B
true
Sleeping in B
Attempting to get lock in C
C
true
Total time: 20.007932

--
Mauricio Fernandez - http://eige... - singular Ruby