如何lock/unlock一个文件跨进程?
How to lock/unlock a file across process?
在 Linux 上的单声道上使用 C# 运行,请注意下面的代码在 windows 上运行良好可以跨进程锁定文件,但在 linux 上则不能通过单声道锁定文件( ubuntu14.04)
new FileStream("myfile.lock",FileMode.OpenOrCreate,FileAccess.ReadWrite,FileShare.None);
网上查的,我应该可以advisory lock
FileStream.Lock
但是,它不起作用。在 ubuntu 14.04 上用两个进程进行测试,它们都可以执行 "FileStream.Lock(0, int.MaxValue)"。我希望后者会失败,每个 source code.
例外
有人知道有什么解决办法吗?
从单一邮件列表“http://mono.1490590.n4.nabble.com/File-Locking-td4663839.html”获取帮助“
下面是引用自"Edward Ned Harvey (mono)"
的回答
Kinda sorta. The underlying issue is that OSX, Linux, and Windows all
have different underlying file locking constructs, and then of course,
there's some variability about even which filesystem is being used.
I didn't thoroughly figure out all the answers for every OS or
filesystem, and I don't know under which situations this will be good
enough, but this is what I ended up using, works under the conditions
we needed it to work:
using (var foo = new FileStream(filePath, FileMode.Open,FileAccess.ReadWrite, FileShare.None)) { // must include Write access in order to lock file
foo.Lock(0, 0); // 0,0 has special meaning to lock entire file regardless of length
}
For windows, simply specifying the FileAccess and FileShare is good
enough. For linux, at least ext4, files are concurrently readable
regardless of what you specify for FileAccess and FileShare. The
Lock() method does something of a soft-lock. It's not enforced by the
OS, but at least all the situations we tried, other client apps honor
the lock. Didn't look into it any deeper.
在 Linux 上的单声道上使用 C# 运行,请注意下面的代码在 windows 上运行良好可以跨进程锁定文件,但在 linux 上则不能通过单声道锁定文件( ubuntu14.04)
new FileStream("myfile.lock",FileMode.OpenOrCreate,FileAccess.ReadWrite,FileShare.None);
网上查的,我应该可以advisory lock
FileStream.Lock
但是,它不起作用。在 ubuntu 14.04 上用两个进程进行测试,它们都可以执行 "FileStream.Lock(0, int.MaxValue)"。我希望后者会失败,每个 source code.
例外有人知道有什么解决办法吗?
从单一邮件列表“http://mono.1490590.n4.nabble.com/File-Locking-td4663839.html”获取帮助“
下面是引用自"Edward Ned Harvey (mono)"
的回答Kinda sorta. The underlying issue is that OSX, Linux, and Windows all have different underlying file locking constructs, and then of course, there's some variability about even which filesystem is being used. I didn't thoroughly figure out all the answers for every OS or filesystem, and I don't know under which situations this will be good enough, but this is what I ended up using, works under the conditions we needed it to work:
using (var foo = new FileStream(filePath, FileMode.Open,FileAccess.ReadWrite, FileShare.None)) { // must include Write access in order to lock file
foo.Lock(0, 0); // 0,0 has special meaning to lock entire file regardless of length
}
For windows, simply specifying the FileAccess and FileShare is good enough. For linux, at least ext4, files are concurrently readable regardless of what you specify for FileAccess and FileShare. The Lock() method does something of a soft-lock. It's not enforced by the OS, but at least all the situations we tried, other client apps honor the lock. Didn't look into it any deeper.