为什么我不能像在linux和OSX上那样在windows上更改正在使用的文件?

今天的问答环节是由SuperUser提供的,SuperUser是Stack Exchange的一个分支,是一个由社区驱动的问答网站分组。...

为什么我不能像在linux和OSX上那样在windows上更改正在使用的文件? When you’re using Linux and OS X, the operating system won’t stop you from deleting a file currently in use yet on Windows you’ll be expressly barred from doing so. What gives? Why can you edit and delete in-use files on Unix-derived systems but not Windows?

今天的问答环节是由SuperUser提供的,SuperUser是Stack Exchange的一个分支,是一个由社区驱动的问答网站分组。

问题

超级用户阅读器侏儒想知道为什么Linux和Windows对在用文件的处理方式不同:

One of the things that has puzzled me ever since I started using Linux is the fact that it allows you to change the name of a file or even delete it while it is being read. An example is how I accidentally tried to delete a video while it was playing. I succeeded, and was surprised as I learnt that you can change just about anything in a file without caring if it’s being used at the moment or not.

那么,在幕后发生了什么,阻止他像在Linux中那样在Windows中肆意删除东西呢?

答案

超级用户的贡献者为我们提供了一些信息侏儒. 他写道:

每当您在Windows中打开或执行一个文件时,Windows都会将该文件锁定在适当的位置(这是一种简化,但通常是正确的)。被进程锁定的文件在该进程释放之前不能被删除。这就是为什么每当Windows必须自我更新时,需要重新启动才能生效。

另一方面,类似Unix的操作系统(如Linux和macosx)不会锁定文件,而是锁定底层磁盘扇区。这似乎是一个微不足道的区别,但这意味着可以删除文件系统目录中的文件记录,而不会干扰任何已打开该文件的程序。因此,您可以在文件仍在执行或以其他方式使用时删除该文件,并且只要某个进程对其具有打开的句柄(即使其在文件表中的条目已消失),该文件仍将继续存在于磁盘上。

David Schwartz进一步阐述了这一观点,并强调了理想情况下的情况以及实际情况:

Windows defaults to automatic, mandatory file locking. UNIXes default to manual, cooperative file locking. In both cases, the defaults can be overriden, but in both cases they usually aren’t.

A lot of old Windows code uses the C/C++ API (functi*** like fopen) rather than the native API (functi*** like CreateFile). The C/C++ API gives you no way to specify how mandatory locking will work, so you get the defaults. The default “share mode” tends to prohibit “conflicting” operati***. If you open a file for writing, writes are assumed to conflict, even if you never actually write to the file. Ditto for renames.

And, here’s where it gets worse. Other than opening for read or write, the C/C++ API provides no way to specify what you intend to do with the file. So the API has to assume you are going to perform any legal operation. Since the locking is mandatory, an open that allows a conflicting operation will be refused, even if the code never intended to perform the conflicting operation but was just opening the file for another purpose.

So if code uses the C/C++ API, or uses the native API without specifically thinking about these issues, they will wind up preventing the maximum set of possible operati*** for every file they open and being unable to open a file unless every possible operation they could perform on it once opened is unconflicted.

In my opinion, the Windows method would work much better than the UNIX method if every program chose its share modes and open modes wisely and sanely handled failure cases. The UNIX method, however, works better if code doesn’t bother to think about these issues. Unfortunately, the basic C/C++ API doesn’t map well onto the Windows file API in a way that handles share modes and conflicting opens well. So the net result is a bit messy.

这就是问题所在:两种不同的文件处理方**产生两种不同的结果。


有什么要补充的解释吗?在评论中发出声音。想从其他精通技术的Stack Exchange用户那里了解更多答案吗?在这里查看完整的讨论主题。

  • 发表于 2021-04-12 00:07
  • 阅读 ( 166 )
  • 分类:互联网

你可能感兴趣的文章

你的生活100%免费和开源的完整指南

... 为什么源代码很重要 ...

  • 发布于 2021-03-13 18:00
  • 阅读 ( 252 )

切换到mac时您会喜欢的7项功能

... 现在我想我明白了为什么mac能激发用户如此强烈的品牌忠诚度。我还不会说自己是个铁杆粉丝,即使是近距离,但我的眼睛已经睁开了,我的思想已经开悟了。 ...

  • 发布于 2021-03-16 08:35
  • 阅读 ( 199 )

为什么mx-linux是您一直在等待的windows替代品

如果你正在寻找一个Windows的替代品,但却回避了Linux,那么MX-Linux可能是你一直在等待的解决方案。 ...

  • 发布于 2021-03-23 09:16
  • 阅读 ( 447 )

什么是文件扩展名?

...,但是如果您有兴趣了解更多,请查看我们的指南,了解为什么Linux和macOS不需要文件扩展名。 相关:MIME类型解释:为什么Linux和MacOSX不需要文件扩展名 如果更改文件的扩展名会发生什么情况? 根据我们在上一节中刚刚讨论的...

  • 发布于 2021-04-05 07:11
  • 阅读 ( 174 )

什么是linux fstab文件,它是如何工作的?

...分配一个通用的唯一标识符,并将其带到坟墓中。因为它不能更改,所以这是选择要装载的文件系统的理想方法,特别是对于重要的文件系统。假设您的/home分区位于第二个硬盘驱动器上,并且最终将其移动到外部硬盘驱动器上...

  • 发布于 2021-04-09 01:37
  • 阅读 ( 267 )

linux文件权限是如何工作的?

...OSX),您可能会遇到“权限”错误。但它们究竟是什么?为什么它们是必要的或有用的?让我们看看里面。 用户权限 在过去,计算机是非常昂贵的大型机器。为了最大限度地利用它们,多个计算机终端被连接起来,允许许多...

  • 发布于 2021-04-09 01:58
  • 阅读 ( 170 )

为什么windows要如此频繁地重新启动?

如果说几乎所有人都抱怨Windows,那就是它想频繁重启。无论是针对Windows更新,还是在安装、卸载或更新软件时,Windows都会经常要求重新启动。 Windows通常必须重新启动,因为在使用系统文件时无法修改它们。这些文件被锁定,...

  • 发布于 2021-04-09 04:11
  • 阅读 ( 188 )

有关在linux上观看drm媒体的所有信息

...而且它不一定适用于所有光盘。 下载的视频文件 相关:为什么Ubuntu不支持mp3、Flash和其他多媒体格式 你可能想举手,完全避免所有的DRM,只需翻录或下载视频文件并在电脑上播放。但即使在这里,你也会遇到一些麻烦-如果不...

  • 发布于 2021-04-10 00:02
  • 阅读 ( 227 )

如何在OSX中立即转到位置和文件夹

...文件夹及其子文件夹是一种方法,但是当你可以跳跃时,为什么要跨一步呢?注意“转到文件夹…”选项,可以使用“Shift+Command+G”执行。 现在你可以输入或粘贴到你的目的地,点击“Go”,你马上就到了。 这在遵循操作指南...

  • 发布于 2021-04-10 22:38
  • 阅读 ( 153 )

MacOSX不再安全:垃圾软件/恶意软件的流行已经开始

...的YTD有一个Mac版本。他们还捆绑龙头。想说点什么吗?你为什么不从他们的网站上下载uTorrent?似乎人们喜欢用它。哦。 当你试图用你最喜欢的搜索引擎搜索免费软件时,问题变得越来越严重。值得一提的是,谷歌最近刚刚开...

  • 发布于 2021-04-10 23:55
  • 阅读 ( 291 )
务必开心丶
务必开心丶

0 篇文章

相关推荐