[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[afnog] lockf() and open() : Theoretical Questions



Hi all,

I have two questions:

1) Does anyone know of an implementation of mandatory file locking in
   FreeBSD (similar to that provided in linux from the lockf()
   interface)?  I've read the lockf(3), flock(2) and fcntl(2) man pages
   all imply advisory-mode locks.

   Is it possible for a process to lock a file (or a section of one) such
   that no other process may open/read/write to that file in FreeBSD using
   the usual system calls (write(), read() etc...)

2) I was faced with an interesting situation in a program I am writing.  I
   wanted to open a file, lock it, modify it's contents and close it.  I
   decided to open it with open() and flags O_RDWR then lock it with
   lockf(), read it line by line and modify the required lines whilst
   saving the modifications to a temporary file I obtained with tmpfile().

   Now here came the problem, how could I delete the contents of the
   original file without losing the locks for even the shortest possible
   period of time and write back the data from the temporary file.

   Does anyone have any ideas on this?

   I kind of failed to do that so as a work around I decided to just
   open() the first file with flags O_RDONLY in the first case then
   close() it and finally open it again with flags O_RDWR (only *then* do
   I lock it).  I thought this would kind of leave way for race
   conditions (or am I wrong)...

Thanks for any help in advance...

Regards,
Gerald.
__________________________________________________
This is the Africa Network Operators' Group(AfNOG) 
technical discussion list.
The AfNOG website is: <http://www.afnog.org>