threads << Thread.new do
%w(a b).each do |path|
fd = open(path) #until((ret = fd.flock flags)) # this works (deadlock)
until((ret = open(path).flock flags)) # this doesn’t
Thread.pass
end
printf “0 LOCK_EX %s <%s>\n”, path, ret
sleep 0.5
end
end
threads << Thread.new do
%w(b a).each do |path|
fd = open(path) #until((ret = fd.flock flags)) # this works (deadlock)
until((ret = open(path).flock flags)) # this doesn’t
Thread.pass
end
printf “1 LOCK_EX %s <%s>\n”, path, ret
sleep 0.5
end
end
[ahoward@localhost flock]$ ./flock.rb
0 LOCK_EX a <0>
1 LOCK_EX b <0>
0 LOCK_EX b <0>
1 LOCK_EX a <0>
so, two threads are able to obtain exclusive locks on a file? perhaps i am
making an obvious mistake?? shouldn’t each call to open(path).flock be
referring to the same open file table entry and, therefore, not affect the
thread’s ability to obtain an LOCK_EX?
-a
···
====================================
Ara Howard
NOAA Forecast Systems Laboratory
Information and Technology Services
Data Systems Group
R/FST 325 Broadway
Boulder, CO 80305-3328
Email: ara.t.howard@noaa.gov
Phone: 303-497-7238
Fax: 303-497-7259
The difference between art and science is that science is what we understand
well enough to explain to a computer. Art is everything else.
– Donald Knuth, “Discover”
~ > /bin/sh -c ‘for lang in ruby perl; do $lang -e “print "\x3a\x2d\x29\x0a"”; done’
====================================
flocks are durable across processes. i think they are held in the kernel open
file table. on my systems, they even work on nfs between machines! (this is
not normal)
Ruby threads all run in the same process.
which is an even stronger argument for why no two threads (let alone
processes) should EVER be able to obtain an exclusive lock at the same time.
-a
···
On Tue, 7 Oct 2003, Hal Fulton wrote:
Ara Howard
NOAA Forecast Systems Laboratory
Information and Technology Services
Data Systems Group
R/FST 325 Broadway
Boulder, CO 80305-3328
Email: ara.t.howard@noaa.gov
Phone: 303-497-7238
Fax: 303-497-7259
The difference between art and science is that science is what we understand
well enough to explain to a computer. Art is everything else.
– Donald Knuth, “Discover”
~ > /bin/sh -c ‘for lang in ruby perl; do $lang -e “print "\x3a\x2d\x29\x0a"”; done’
====================================
I sure hope not! If the process goes away, its locks better disappear, too!
i think they are held in the kernel open file table.
I’ve seen them in their own tables (SVR4).
Ruby threads all run in the same process.
“Yeah, baby!” (in an Austin Powers dialect).
which is an even stronger argument for why no two threads (let alone
processes) should EVER be able to obtain an exclusive lock at the same time.
No, once a process has an exclusive lock, it’s free to put additional exclusive
locks on it. It “owns” the file now, so to speak.
Also, many locking mechanisms are merely advisory, not mandatory.
In Linux, the filesystem has to be mounted with a special flag to allow
actual exclusive file locks. I know that’s not the issue here; I was just
pointing out a certain case.