目次:


今日のQ&Aセッションは、Q&A Webサイトのコミュニティ主導型のグループであるStack ExchangeのサブユニットであるSuperUserのおかげで行われています。
質問
SuperUserリーダー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のようにWindowsで無駄に削除するのを防ぐのですか?
答え
スーパーユーザの投稿者は、このミジェットの状況を明らかにしました。驚いた書き込み:
Windowsでファイルを開くか実行するたびに、Windowsはファイルを所定の位置にロックします(これは単純化されていますが、通常は真です)。プロセスによってロックされたファイルは、そのプロセスが解放するまで削除できません。これは、Windowsがそれ自体を更新する必要があるときはいつでも、それを有効にするために再起動が必要な理由です。
一方、LinuxやMac OS XなどのUnixライクなオペレーティングシステムでは、ファイルをロックするのではなく、基礎をなすディスクセクタをロックします。これは些細な差異に見えるかもしれませんが、ファイルシステムの目次にあるファイルのレコードは、すでにファイルが開いているプログラムを妨げることなく削除できることを意味します。したがって、ファイルがまだ実行されている間、または使用中にファイルを削除することができます。ファイルテーブルのエントリがなくても、オープンハンドルがあるプロセスであれば、ディスク上にファイルが存在し続けます。
デイビッド・シュワルツ氏は、アイデアを広げ、事柄が理想的であるべきかどうか、そして現実の仕組みを強調します。
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.
ファイル処理には2つの異なる方法があります。
説明に追加するものがありますか?コメントの音が鳴ります。他の技術に精通したStack Exchangeユーザーからの回答をもっとたくさん読んでみたいですか?ディスカッションスレッド全体をチェックしてください。