為什麼我不能像在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
  • 閱讀 ( 43 )
  • 分類:網際網路

你可能感興趣的文章

不,linux不需要防病毒或防火牆

... 為什麼linux不需要防病毒軟體 ...

  • 發佈於 2021-03-12 20:15
  • 閲讀 ( 51 )

40多個最常用的linux終端命令

...真正充分利用終端之前,您需要知道如何導航它。這就是為什麼我們認為這些是最基本的Linux命令:如果你不能更改目錄或者在一個你不記得如何使用的命令上獲得幫助,那麼再多的終端知識也幫不了你。 ...

  • 發佈於 2021-03-15 21:40
  • 閲讀 ( 49 )

切換到mac時您會喜歡的7項功能

... 現在我想我明白了為什麼mac能激發使用者如此強烈的品牌忠誠度。我還不會說自己是個鐵桿粉絲,即使是近距離,但我的眼睛已經睜開了,我的思想已經開悟了。 ...

  • 發佈於 2021-03-16 08:35
  • 閲讀 ( 51 )

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

如果你正在尋找一個Windows的替代品,但卻迴避了Linux,那麼MX-Linux可能是你一直在等待的解決方案。 ...

  • 發佈於 2021-03-23 09:16
  • 閲讀 ( 48 )

別放棄麥克!7繼續使用macbooks的重要原因

... 你將不可避免地在硬體上省下一些錢,但代價是什麼?如果你是在玩弄這個想法,那麼首先確定一下你對Mac電腦的喜愛可能會有所幫助。 ...

  • 發佈於 2021-03-25 20:29
  • 閲讀 ( 45 )

為什麼windows仍然使用字母作為驅動器?

...ux不使用字母。Windows可以訪問沒有字母的驅動器,那麼它為什麼要使用它們呢? 驅動器號來自哪裡? 像Windows中的許多東西一樣,它如何使用向後斜槓而不是向前斜槓驅動器號可以追溯到MS-DOS時代(事實上,甚至更早)。這就...

  • 發佈於 2021-04-03 09:21
  • 閲讀 ( 41 )

什麼是副檔名?

...,但是如果您有興趣瞭解更多,請檢視我們的指南,瞭解為什麼Linux和macOS不需要副檔名。 相關:MIME型別解釋:為什麼Linux和MacOSX不需要副檔名 如果更改檔案的副檔名會發生什麼情況? 根據我們在上一節中剛剛討論的內容,當...

  • 發佈於 2021-04-05 07:11
  • 閲讀 ( 69 )

如何從mac中刪除惡意軟體和廣告軟體

...lwarebytes應該能夠自動找到並解除安裝這個junkware,這就是為什麼它如此有用的原因。 完整的mac防病毒程式呢? 相當多的反病毒公司現在也在為macOS建立(並銷售)完整的反病毒程式。這些應用程式類似於它們的Windows等價物,具...

  • 發佈於 2021-04-07 02:38
  • 閲讀 ( 58 )

什麼是linux fstab檔案,它是如何工作的?

...整個過程變得容易很多,而且比你想象的要容易得多。 什麼是fstab公司(fstab)? Fstab是作業系統的檔案系統表。如果你想回顧一下檔案系統,一定要看看我們的另一篇文章,HTG解釋說:你應該選擇哪個Linux檔案系統?在過去,它...

  • 發佈於 2021-04-09 01:37
  • 閲讀 ( 44 )

linux檔案許可權是如何工作的?

...),您可能會遇到“許可權”錯誤。但它們究竟是什麼?為什麼它們是必要的或有用的?讓我們看看裡面。 使用者許可權 在過去,計算機是非常昂貴的大型機器。為了最大限度地利用它們,多個計算機終端被連線起來,允許...

  • 發佈於 2021-04-09 01:58
  • 閲讀 ( 25 )
務必開心丶
務必開心丶

0 篇文章

作家榜

  1. admin 0 文章
  2. 孫小欽 0 文章
  3. JVhby0 0 文章
  4. fvpvzrr 0 文章
  5. 0sus8kksc 0 文章
  6. zsfn1903 0 文章
  7. w91395898 0 文章
  8. SuperQueen123 0 文章

相關推薦