[Mono-dev] Equivalent to LockFileEx on Linux
Jonathan Pryor
jonpryor at vt.edu
Tue Mar 2 16:28:15 EST 2010
On Tue, 2010-03-02 at 12:29 -0800, Daniel Morgan wrote:
> Is there an equivalent to LockFileEx on Linux? What would it be?
No. There's flock(2) and fcntl(2), but these are advisory in nature
only. (Meaning that any other process with access rights to the file
can come along, ignore the advisory lock, and change the file underneath
you.)
In managed code, there's System.IO.FileStream.Lock(long, long) and
System.IO.FileStream.Unlock(long, long), but these use fcntl(2)'s
F_SETLK functionality.
Mandatory locking (according to fcntl(2)) requires that the file system
opt-in (via the '-o mand' option to mount(8)) AND enabled on the file by
disabling group execute permission and enabling the set-group-ID
permission bit. None of my systems have '-o mand' specified, and I
doubt that very many systems do.
> How would you lock a file that will work on .net on windows and mono
> on linux and mono on windows and mono on mac os x, etc?
You don't. Bwa-ha-ha-ha-ha.
Sorry.
Related question: why do you need this? Linux and Unix seem to get
along just fine without system-wide mandatory locking...
Perhaps as an alternative you should read/write to a temporary file,
flush the file, then rename the file (assuming file renames are atomic,
which is *usually* the case). You might lose data if two processes
attempt to overwrite the same file at the same time, but at least your
DB will be consistent and not corrupted...
- Jon
More information about the Mono-devel-list
mailing list