Perché non riesco a modificare i file in uso su Windows come posso fare su Linux e OS X?

Sommario:

Perché non riesco a modificare i file in uso su Windows come posso fare su Linux e OS X?
Perché non riesco a modificare i file in uso su Windows come posso fare su Linux e OS X?

Video: Perché non riesco a modificare i file in uso su Windows come posso fare su Linux e OS X?

Video: Perché non riesco a modificare i file in uso su Windows come posso fare su Linux e OS X?
Video: Giochi Android su PC con Bluestacks - YouTube 2024, Aprile
Anonim
 Quando utilizzi Linux e OS X, il sistema operativo non ti impedirà di cancellare un file attualmente in uso su Windows, ma ti verrà espressamente vietato farlo. Cosa dà? Perché è possibile modificare ed eliminare file in uso su sistemi derivati da Unix ma non su Windows?
Quando utilizzi Linux e OS X, il sistema operativo non ti impedirà di cancellare un file attualmente in uso su Windows, ma ti verrà espressamente vietato farlo. Cosa dà? Perché è possibile modificare ed eliminare file in uso su sistemi derivati da Unix ma non su Windows?

La sessione di domande e risposte di oggi ci viene fornita per gentile concessione di SuperUser, una suddivisione di Stack Exchange, un raggruppamento di domande e risposte basato sulla comunità.

La domanda

Lettore SuperUser the.midget vuole sapere perché Linux e Windows trattano i file in uso in modo diverso:

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.

Quindi cosa sta succedendo dietro le quinte e impedendogli di cancellare volutamente le cose in Windows come può fare in Linux?

La risposta

I contributori di SuperUser fanno luce sulla situazione di the.midget. Scritto stupito:

Ogni volta che apri o esegui un file in Windows, Windows blocca il file in posizione (questa è una semplificazione, ma in genere vera). Un file bloccato da un processo non può essere eliminato finché tale processo non lo rilascia. Questo è il motivo per cui ogni volta che Windows deve aggiornarsi è necessario un riavvio perché abbia effetto.

D'altra parte, i sistemi operativi di tipo Unix come Linux e Mac OS X non bloccano il file ma piuttosto i settori del disco sottostanti. Questo può sembrare una banale differenziazione, ma significa che il record del file nel sommario del file system può essere cancellato senza disturbare nessun programma che abbia già aperto il file. Quindi puoi cancellare un file mentre è ancora in esecuzione o in altro modo in uso e continuerà a esistere su disco purché alcuni processi abbiano un handle aperto per esso anche se la sua voce nella tabella file è scomparsa.

David Schwartz amplia l'idea e sottolinea come le cose dovrebbero essere idealmente e come sono nella pratica:

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.

Ecco qua: due diversi approcci alla gestione dei file danno due risultati diversi.

Hai qualcosa da aggiungere alla spiegazione? Sound off nei commenti. Vuoi leggere più risposte dagli altri utenti di Stack Exchange esperti di tecnologia? Controlla la discussione completa qui.

Consigliato: