Varför kan jag ändra användningsfiler på Windows som jag kan på Linux och OS X?

Innehållsförteckning:

Video: Varför kan jag ändra användningsfiler på Windows som jag kan på Linux och OS X?

Video: Varför kan jag ändra användningsfiler på Windows som jag kan på Linux och OS X?
Video: How to Run Android Apps natively on Windows 11 2024, Mars
Varför kan jag ändra användningsfiler på Windows som jag kan på Linux och OS X?
Varför kan jag ändra användningsfiler på Windows som jag kan på Linux och OS X?
Anonim
 När du använder Linux och OS X, vann operativsystemet inte stoppa dig från att radera en fil som är i bruk ännu på Windows, du kommer uttryckligen att hindras från att göra det. Vad ger? Varför kan du redigera och ta bort användningsfiler på Unix-härledda system men inte Windows?
När du använder Linux och OS X, vann operativsystemet inte stoppa dig från att radera en fil som är i bruk ännu på Windows, du kommer uttryckligen att hindras från att göra det. Vad ger? Varför kan du redigera och ta bort användningsfiler på Unix-härledda system men inte Windows?

Idag sitter frågor och svar till oss med tillstånd av SuperUser, en delavdelning av Stack Exchange, en community-driven gruppering av Q & A-webbplatser.

Frågan

SuperUser-läsaren the.midget vill veta varför Linux och Windows behandlar användningsfiler på olika sätt:

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.

Så vad händer bakom kulisserna och förhindrar att han vill ha borttagen saker i Windows som han kan på Linux?

Svaret

SuperUser bidragsgivare kasta lite ljus på situationen för the.midget. Förvånad skriver:

När du öppnar eller kör en fil i Windows låser Windows filen på plats (det här är en förenkling, men vanligtvis sant.) En fil som är låst av en process kan inte raderas förrän den processen släpper ut den. Det är därför som Windows måste uppdatera sig behöver du en omstart för att den ska träda i kraft.

Å andra sidan låser Unix-liknande operativsystem som Linux och Mac OS X inte filen utan snarare de underliggande skivbranscherna. Detta kan verka som en trivial differentiering men det betyder att filens register i filsystemets innehållsförteckning kan raderas utan att störa något program som redan har filen öppen. Så du kan ta bort en fil medan den fortfarande körs eller används på annat sätt och den kommer att fortsätta att finnas på disk så länge som en del process har ett öppet handtag för det trots att dess inmatning i filtabellen är borta.

David Schwartz expanderar på idén och belyser hur saker ska vara idealiska och hur de är i praktiken:

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

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

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.

Där har du det: två olika metoder för filhantering ger två olika resultat.

Har du något att lägga till förklaringen? Ljud av i kommentarerna. Vill du läsa mer svar från andra tech-savvy Stack Exchange-användare? Kolla in hela diskussionsgängan här.

Rekommenderad: