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?

当您使用Linux和OS X时,操作系统不会阻止您删除当前正在使用的文件,但在Windows上,您将被明确禁止这样做。 是什么赋予了? 为什么要在Unix衍生的系统上而不是Windows上编辑和删除使用中的文件?

Today’s Question & Answer session comes to us courtesy of SuperUser—a subdivision of Stack Exchange, a community-driven grouping of Q&A web sites.

今天的“问答”环节由SuperUser提供,它是Stack Exchange的一个分支,该社区是由社区驱动的Q&A网站分组。

问题 (The Question)

SuperUser reader the.midget wants to know why Linux and Windows treat in-use files differently:

超级用户阅读器the.midget想知道为什么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以来,困扰我的一件事就是它允许您更改文件名,甚至在读取文件时将其删除。 一个例子是我在播放视频时不小心尝试删除它。 我成功了,当我得知您可以更改文件中的任何内容而无需关心当前是否在使用它们时,感到惊讶。

So what’s happening behind the scenes and preventing him from wantonly deleting things in Windows like he can in Linux?

那么,幕后发生的事情又是如何阻止他像在Linux中那样肆意删除Windows中的内容呢?

答案 (The Answer)

SuperUser contributors shed some light on the situation for the.midget. Amazed writes:

超级用户贡献者对.midget的情况有所了解。 惊奇写道:

Whenever you open or execute a file in Windows, Windows locks the file in place (this is a simplification, but usually true.) A file which is locked by a process cannot be deleted until that process releases it. This is why whenever Windows has to update itself you need a reboot for it to take effect.

每当您在Windows中打开或执行文件时,Windows都会将文件锁定在适当的位置(这是一种简化,但通常是正确的。)被进程锁定的文件只有在该进程将其释放后才能删除。 这就是Windows每次必须更新自身时都需要重新启动才能生效的原因。

On the other hand, Unix-like operating systems like Linux and Mac OS X don’t lock the file but rather the underlying disk sectors. This may seem a trivial differentiation but it means that the file’s record in the filesystem table of contents can be deleted without disturbing any program that already has the file open. So you can delete a file while it is still executing or otherwise in use and it will continue to exist on disk as long as some process has an open handle for it even though its entry in the file table is gone.

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

David Schwartz expands on the idea and highlights how things should be ideally and how they are in practice:

大卫·施瓦兹(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.

Windows默认为自动强制性文件锁定。 UNIX默认使用手动的协作文件锁定。 在这两种情况下,都可以覆盖默认值,但在两种情况下通常都不能。

A lot of old Windows code uses the C/C++ API (functions like fopen) rather than the native API (functions 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” operations. If you open a file for writing, writes are assumed to conflict, even if you never actually write to the file. Ditto for renames.

许多旧的Windows代码使用C / C ++ API(如fopen之类的功能)而不是本机API(如CreateFile之类的功能)。 C / C ++ API无法让您指定强制性锁定的工作方式,因此您会获得默认值。 默认的“共享模式”倾向于禁止“冲突”操作。 如果打开文件进行写入,则即使您从未实际写入文件,也会认为写入冲突。 同名重命名。

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.

而且,这是情况变得更糟的地方。 除了可以进行读取或写入操作外,C / C ++ API无法提供指定文件用途的方法。 因此,API必须假定您将执行任何合法操作。 由于锁定是强制性的,因此即使代码从未打算执行冲突操作而只是出于其他目的而打开文件,也将拒绝允许冲突操作的打开。

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 operations 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.

因此,如果代码使用C / C ++ API或使用本机API而没有特别考虑这些问题,则它们最终会阻止它们打开的每个文件的最大可能操作集,并且无法打开文件,除非它们进行了每个可能的操作打开后可以在其上执行的操作没有冲突。

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.

在我看来,如果每个程序明智地选择并明智地处理故障情况,则Windows方法将比UNIX方法更好。 但是,如果代码不必考虑这些问题,则UNIX方法会更好。 不幸的是,基本的C / C ++ API无法以一种处理共享模式的方式很好地映射到Windows文件API,并且冲突打开得很好。 因此,最终结果有点混乱。

There you have it: two different approaches to file handling yield two different results.

有了它:两种不同的文件处理方法会产生两种不同的结果。



Have something to add to the explanation? Sound off in the the comments. Want to read more answers from other tech-savvy Stack Exchange users? Check out the full discussion thread here.

有什么补充说明吗? 在评论中听起来不对。 是否想从其他精通Stack Exchange的用户那里获得更多答案? 在此处查看完整的讨论线程。

翻译自: https://www.howtogeek.com/141393/why-cant-i-alter-in-use-files-on-windows-like-i-can-on-linux-and-os-x/

为什么我不能像在Linux和OS X上那样在Windows上更改使用中的文件?相关推荐

  1. ac3165 linux驱动_为什么Linux系统没有类似Windows上的还原精灵和影子系统?

    很多从Windows转Linux的用户,面临的最大一个问题,就是Linux上没有还原精灵这种软件.众所周知,作为一个操作系统要想普及,绝不可能只针对懂IT的少部分年轻人,而是需要让不同年龄段,不同职业 ...

  2. 【samba】Wodows同步Linux文件|搭建共享文件服务器——在windows上映射网络驱动器

    目录 Linux做samba服务器 Linux上的操作 windows上的操作 windows做samba服务器 windows上的操作 linux上的操作 错误记录 Linux Samba服务主配文 ...

  3. 【samba】Wodows同步Linux文件|搭建samba服务器共享文件——在windows上映射网络驱动器...

    目录 Linux做samba服务器 Linux上的操作 windows上的操作 windows做samba服务器 windows上的操作 linux上的操作 错误记录 Linux Samba服务主配文 ...

  4. 在Windows上使用终端模拟程序连接操作Linux以及上传下载文件

    在Windows上使用终端模拟程序连接操作Linux以及上传下载文件 [很简单,就是一个工具的使用而已,放这里是做个笔记.] 刚买的云主机,或者是虚拟机里安装的Linux系统,可能会涉及到在windo ...

  5. linux 怎么更新 8021q模块,在ubuntu14.04上实现收发VLAN(802.1Q)帧

    关键词 : AVB, 802.1Q, VLAN, scapy,python,pycharm,uBuntu 目的:测试中需要测试7个网卡,独立收.发802.1q的帧. 平台:不限(目前在Ubuntu 1 ...

  6. ActiveState Komodo IDE v5.2.1.34168 最新版for Linux/Mac OS/Windows 全5大平台

    ActiveState Komodo IDE v5.2.1.34168 最新版for Linux/Mac OS/Windows 全5大平台 转载于:https://www.cnblogs.com/ga ...

  7. Linux 查询 OS、CPU、内存、硬盘信息

    文章目录 Linux 查询 OS.CPU.内存.硬盘信息 一.前言 二.关于服务器基本配置 2.1 操作系统基本配置查询 2.2 CPU基本配置查询 2.3 内存基本配置查询 2.4 硬盘基本配置查询 ...

  8. cp linux 显示进度条_Unix/Linux/Mac os下 文件互传

    Unix/Linux/Mac os下 文件互传 说起文件互传,就不得不提命令scp. 他是Secure copy的缩写,使用ssh连接和加密方式, 如果两台机器之间配置了ssh免密登录, 那在使用sc ...

  9. linux unix mac windows,文件路径-windows上的反斜杠和Mac OS/Linux/Unix上的正斜杠,Windows,倒,以及,macOSLinuxUNIX...

    文件路径 - Windows 上的倒斜杠以及 macOS / Linux / UNIX 上的正斜杠 1. 正斜杠 (斜杠 / ) 和反斜杠 (倒斜杠 \ ) Windows - 正斜杠,左斜杠,斜杠符 ...

  10. mac安装rstudio_在Windows / Linux / Mac OS上安装R和RStudio入门

    mac安装rstudio 在Windows上安装R (Installing R on Windows) Go to r-project.org on your internet browser.在您的 ...

最新文章

  1. SAP中关于物料主数据里物料类型的修改
  2. Boost:glob测试程序
  3. strtotime()加半个小时_炖羊肉,必须加这4种料,缺一味就不香!很多人做错,难怪不好吃...
  4. gevent+django并发资料调研
  5. 推荐几篇开源论文,包含人脸、目标检测跟踪、分割、去噪、超分辨率等
  6. 蚂蚁金服刘伟光:我们为什么要科技开放
  7. [转帖]Dockerfile设置默认时区
  8. mysql网游单机架设_网游单机架设直观教程终结版.doc
  9. 华为交换机一端口网线一直拔插,导致端口被关闭锁死,网线接了灯不会亮
  10. Netgear wndr3700v2 路由器刷OpenWrt打造全能服务器(一)序章
  11. 来自一个工作一年零九个月java程序员的自我介绍
  12. 计算机用户删除怎么找回,电脑不小心删除的数据怎么找回
  13. 微信小程序 数据在缓存中的存储和获取
  14. 点到直线(超平面)的距离公式
  15. 铜护套氧化镁矿物质绝缘电缆
  16. 如何快速提高博客排名?CSDN排名如何突破
  17. 中国人寿保险软件开发机试题 java实现
  18. IDEA安装教程(图文详解,一步搞定)
  19. 几点减几点怎么列算式_连加、连减
  20. GPON与EPON之比较(无源光网络技术)

热门文章

  1. 【算法学习】 位运算中的奇巧淫记
  2. 示波器20M硬件带宽限制与数字滤波高低通功能
  3. colorAccent,colorPrimary,colorPrimaryDark……来这里你就明白了
  4. 1米6农村放牛娃的奋斗历程:从同济、港理工,到清华、伯克利大学!
  5. 什么是服务器安全性?
  6. 阿里云服务器安全组放行宝塔端口8888|888|80|443|20|21教程
  7. xshell 批量创建.xsh会话文件
  8. SOX命令:音频位深度、采样率以及码率
  9. android use-feature和market策略
  10. 服务器装win7找不到硬盘驱动,Windows 7安装问题时找不到硬盘驱动器怎么办