Confused about locking a file via file.flock(File::LOCK_EX)

L

Ludwigi Beethoven

I am writing a ruby appl under AIX where I need to
update the /etc/hosts table. I would like to make sure
that during my update nobody else can update that
file. I thought I found a solution to my problem with
the flock method. However, it does not appear to do
what I thought it will do. To test it, I wrote a
simple ruby piece of code that, first locks the file,
second spin-loop forever while I open another window
and vi the "locked" file. Well, I am able to vi and
change the file that is supposed to be locked.
ruby -v returns: ruby 1.6.7 (2002-03-01)
[powerpc-aix4.3.3.0]

Any ideas??

Thank you

__________________________________
Do you Yahoo!?
Yahoo! SiteBuilder - Free, easy-to-use web site design software
http://sitebuilder.yahoo.com
 
T

Travis Whitton

Flock only works within the same process. For example, if you had
2 concurrent versions of your program running, the second version
would wait for the first version to release the lock before it opened
the file. To my knowledge, there isn't any guaranteed way to lock
all other processes out of a file, but I could be wrong.

HTH,
Travis Whitton <[email protected]>
 
T

ts

L> what I thought it will do. To test it, I wrote a
L> simple ruby piece of code that, first locks the file,
L> second spin-loop forever while I open another window
L> and vi the "locked" file. Well, I am able to vi and
L> change the file that is supposed to be locked.

Well, to understand the problem

svg% cat b.rb
#!/usr/bin/ruby
f = File.new("aa", "w")
f.flock(File::LOCK_EX)
puts "file locked #{Process.pid}"

fork do
g = File.new("aa", "w")
if !g.flock(File::LOCK_SH|File::LOCK_NB)
puts "error : file locked #{Process.pid}"
end
end
Process.wait

fork do
g = File.new("aa", "w")
g.puts "aa"
end
Process.wait
svg%

svg% cat aa
cat: aa: No such file or directory
svg%

svg% b.rb
file locked 25340
error : file locked 25341
svg%

svg% cat aa
aa
svg%

a lock work *only* if the process try to acquire a lock before writing in
the file.

In my example, the first fork test if the file is locked and give an error
if it can't have the lock.

The second fork, don't test and write directly in the file (it bypass the
lock)

In your case, vi do like my second fork : it don't try to acquire a lock
and write in the file


Guy Decoux
 
M

Mark J. Reed

Flock only works within the same process.

Not true. You can have many different programs in many different
languages running as independent processes all using flock to
manage access to one or more files. However, flock is only an
*advisory* lock. It locks files so that no other process can
access them, IF that other process is bothering to use flock.

There is no *mandatory* file locking mechanism on UNIX.
It's always possible to just ignore any locks and open a file
for writing.

-Mark
 
B

Brian Candler

Flock only works within the same process.

Locking works between processes, but in general locks are "advisory". That
means that process A and process B must both agree to use the locking
protocol. If process A locks a file, that doesn't actually prevent process B
going ahead and writing to the file, if it doesn't bother to get a lock
first.

It gets confusing when there are about 3 different locking mechanisms
(flock, lockf, and some ioctl-locking) but I don't have my Stevens book with
me so I can't give you the gory details :)

Cheers,

Brian.
 
M

Mike Hall

Mark said:
There is no *mandatory* file locking mechanism on UNIX.
It's always possible to just ignore any locks and open a file
for writing.

I have to disagree. SVR2 had it way back when,
and Linux used to, last time I checked a few years ago.
As I recall, I could put a mandatory lock on a file, and even 'cat' would object.

* Mandatory locking o kernel enforces lock requests o access to records is controlled by the kernel o open, close, read, write system calls honor the locks o requires SGID bit set, with group-execute bit cleared + chmod u+rw,g+rws-x,o+rw file o in Linux, requires a MAND_LOCK mounted filesystem + mount -o remount,mand /filesystem + mount -o remount,nomand /filesystem o a write-lock prevents _any_ process from reading that section

I have no idea what AIX supports in this regard.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top