如何使 Windows 文件锁定更像 UNIX 文件锁定?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/546504/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I make Windows file-locking more like UNIX file-locking?
提问by Salim Fadhley
UNIX file-locking is dead-easy: The operating system assumes that you know what you are doing and lets you do what you want:
UNIX 文件锁定非常简单:操作系统假定您知道自己在做什么,并让您做自己想做的事:
For example, if you try to delete a file which another process has opened the operating system will usually let you do it. The original process still keeps it's file-handles until it terminates - at which point the the file-system will quietly re-cycle the disk-resources. No fuss, that's the way I like it.
例如,如果您尝试删除另一个进程打开的文件,操作系统通常会让您这样做。原始进程仍然保留它的文件句柄,直到它终止——此时文件系统将悄悄地回收磁盘资源。不客气,这就是我喜欢的方式。
How different things are on Windows: If I try to delete a file which another process is using I get an Operating-System error. The file is untouchable until the original process releases it's lock on the file. That was great back in the single-user days of MS-DOS when any locking process was likely to be on the same computer that contained the files, however on a network it's a nightmare:
Windows 上有什么不同:如果我尝试删除另一个进程正在使用的文件,我会收到操作系统错误。在原始进程释放它对文件的锁定之前,该文件是不可访问的。这在 MS-DOS 的单用户时代非常棒,当时任何锁定过程都可能在包含文件的同一台计算机上,但是在网络上这是一场噩梦:
Consider what happens when a process hangs while writing to a shared file on a Windows file-server. Before the file can be deleted we have to locate the computer and ID the process on that computer which originally opened the file. Only then can we kill the process and delete our unwanted file.
考虑在写入 Windows 文件服务器上的共享文件时进程挂起时会发生什么。在删除文件之前,我们必须找到计算机并识别最初打开文件的计算机上的进程。只有这样我们才能终止进程并删除我们不需要的文件。
What a nuisance!
真讨厌!
Is there a way to make this better? What I want is for file-locking on Windows to behave a like file-locking in UNIX. I want the operating system to just let me do what I want because I'm in charge and I know what I'm doing...
有没有办法让这个更好?我想要的是 Windows 上的文件锁定行为类似于 UNIX 中的文件锁定。我希望操作系统让我做我想做的事,因为我负责并且我知道我在做什么......
...so can it be done?
……这样可以吗?
采纳答案by bialix
According to MSDN you can specify to CreateFile() 3rd parameter (dwSharedMode) shared mode flag FILE_SHARE_DELETE
which:
根据 MSDN,您可以指定 CreateFile() 第三个参数 (dwSharedMode) 共享模式标志FILE_SHARE_DELETE
,其中:
Enables subsequent open operations on a file or device to request delete access.
Otherwise, other processes cannot open the file or device if they request delete access.
If this flag is not specified, but the file or device has been opened for delete access, the function fails.
Note Delete access allows both delete and rename operations.
启用对文件或设备的后续打开操作以请求删除访问权限。
否则,如果其他进程请求删除访问权限,它们将无法打开文件或设备。
如果未指定此标志,但已打开文件或设备进行删除访问,则该功能失败。
注意删除访问允许删除和重命名操作。
http://msdn.microsoft.com/en-us/library/aa363858(VS.85).aspx
http://msdn.microsoft.com/en-us/library/aa363858(VS.85).aspx
So if you're can control your applications you can use this flag.
所以如果你可以控制你的应用程序,你可以使用这个标志。
回答by Aaron Digulla
No. Windows is designed for the "average user", that is people who don't understand anything about a computer. Therefore, the OS tries to be smart to avoid PEBKACs. To quote Bill Gates: "There are no issues with Windows that any number of people want to be fixed." Of course, he knows that 99.9999% of all Windows users can't tell whether the program just did something odd because of them or the guy who wrote it.
不。Windows 是为“普通用户”设计的,即对计算机一无所知的人。因此,操作系统试图聪明地避免PEBKACs。引用比尔·盖茨的话:“Windows 没有任何人想要修复的问题。” 当然,他知道 99.9999% 的 Windows 用户无法判断程序是否只是因为他们或编写它的人而做了一些奇怪的事情。
Unix was designed when the world was more simple and anyone close enough to a computer to touch it, probably knew how to assemble it from dirty sand. Therefore, the OS usually lets you do what you want because it assumes that you know better (and if you didn't, you will next time).
Unix 是在世界更简单的时候设计的,任何靠近计算机的人都可以触摸它,可能知道如何用肮脏的沙子组装它。因此,操作系统通常会让你做你想做的事,因为它假设你知道得更好(如果你不知道,下次你会知道的)。
Technical answer: Unix allocates an "i-nodes" if you create a file. I-nodes can be shared between processes. If two processes create the same file (that is, two processes call create() with the same path), then you end up with two i-nodes. This is by design. It allows for a fancy security feature: You can create files which no one can open but yourself:
技术答案:如果您创建文件,Unix 会分配一个“i-nodes”。I-节点可以在进程之间共享。如果两个进程创建了同一个文件(也就是说,两个进程使用相同的路径调用 create() ),那么你最终会得到两个 i 节点。这是设计使然。它提供了一种奇特的安全功能:您可以创建只有您自己才能打开的文件:
- Open a file
- Delete it (but keep the file handle)
- Use the file any way you like
- Close the file
- 打开文件
- 删除它(但保留文件句柄)
- 以您喜欢的任何方式使用该文件
- 关闭文件
After step #2, the only process in the universe who can access the file is the one who created it (unless you want to read the hard disk block by block). The OS will keep the data alive until you either close the file or your process dies (at which time Unix will clean up after you).
在第 2 步之后,Universe 中唯一可以访问该文件的进程是创建它的进程(除非您想逐块读取硬盘)。操作系统将保持数据处于活动状态,直到您关闭文件或您的进程终止(此时 Unix 将在您之后进行清理)。
This design is the foundation of all Unix filesystems. The Windows file system NTFS works much the same way but the high level API is different. Many applications open files in exclusive mode (which prevents anyone, even backup programs) to read the file. This is even true for applications which just display information like PDF viewers.
这种设计是所有 Unix 文件系统的基础。Windows 文件系统 NTFS 的工作方式大致相同,但高级 API 不同。许多应用程序以独占模式打开文件(防止任何人,甚至备份程序)读取文件。对于仅显示 PDF 查看器等信息的应用程序来说,情况更是如此。
That means you'll have to fix all the Windows applicationsto achieve the desired effect. If you have access to the source, you can create a file in a shared mode. That would allow other processes to access it at the same time but then, you will have to check before every read/write if the file still exists, whether someone has made changes, etc.
这意味着您必须修复所有 Windows应用程序才能达到预期效果。如果您有权访问源,则可以在共享模式下创建文件。这将允许其他进程同时访问它,但是,您必须在每次读/写之前检查文件是否仍然存在,是否有人进行了更改等。
回答by ShuggyCoUk
Note that Process Explorer allow for force closing of file handles (for processes local to the box on which you are running it) via Handle -> Close Handle.
请注意,Process Explorer 允许通过 Handle -> Close Handle 强制关闭文件句柄(对于运行它的框的本地进程)。
Unlockerpurports to do a lot more, and provides a helpful list of other tools.
Unlocker声称可以做更多的事情,并提供了一个有用的其他工具列表。
Also deleting on reboot is an option (though this sounds like not what you want)
在重启时删除也是一个选项(虽然这听起来不是你想要的)
回答by Rob K
That doesn't really help if the hung process still has the handle open. It won't release the resources until that hung process releases the handle. But anyway, in Windows it is possible to force close a file out from under a process that's using it. Process Explorer from sysinternals.com will let you look at and close handles that a process has open.
如果挂起的进程仍然打开句柄,那并没有真正的帮助。在挂起的进程释放句柄之前,它不会释放资源。但无论如何,在 Windows 中,可以从正在使用它的进程下强制关闭文件。来自 sysinternals.com 的 Process Explorer 将让您查看和关闭进程已打开的句柄。