覆盖文件时写入的扇区?
假设有一个大小为 5 MB 的文件。我用 C 语言以写入模式打开它,然后用恰好 5 MB 的垃圾数据填充它。先前使用的相同磁盘扇区是否会被覆盖,或者操作系统是否会为新数据选择新的磁盘扇区?
Imagine there is a file of size 5 MB. I open it in write mode in C and then fill it up with junk data of exactly 5 MB. Will the same disk sectors previously used be overwritten or will the OS select new disk sectors for the new data?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这取决于文件系统。
传统上,答案是“是的,相同的扇区将被新数据覆盖”。
对于日志文件系统,答案可能有所不同。对于闪存驱动器系统,答案几乎肯定是“不”;将写入新扇区以避免磨损当前写入的扇区。
It depends on the file system.
Classically, the answer would be 'yes, the same sectors would be overwritten with the new data'.
With journalled file systems, the answer might be different. With flash drive systems, the answer would almost certainly be 'no; new sectors will be written to avoid wearing out the currently written sectors'.
文件系统可以做任何它想做的事。但任何真实的文件系统都会将数据写回相同的扇区。
没有的话就上图吧每次写入文件时,文件系统都必须找到一个新的空闲扇区,写入该扇区,然后更新文件的文件系统元数据以指向新扇区。这也会导致可怕的文件碎片,因为在连续的 5MB 文件中间写入单个扇区会导致它产生碎片。因此,写回同一扇区要容易得多。
我能想到的唯一例外是 JFFS2 因为它旨在支持闪存上的磨损均衡。
现在文件系统将写入同一扇区,但磁盘硬件可以写入任何它想要的位置。事实上,在SSD/闪存驱动器硬件上,要处理磨损均衡,几乎可以保证写入数据到不同的部门。但这对于操作系统/文件系统来说是透明的。 (由于 扇区备用)
The filesystem could do anything it wishes. But any real file system will write the data back to the same sectors.
Image if it didn't. Every time your wrote to a file the file system would have to find a new free sector, write to that sector, then update the file system meta data for the file to point to the new sector. This would also cause horrible file fragmentation, because writing a single sector in the middle of your contiguous 5MB file would cause it to fragment. So it's much easier to just write back to the same sector.
The only exception I can think of is JFFS2 because it was designed to support wear leveling on flash.
Now the file system will write to the same sector, but the disk hardware could write anywhere it wants. In fact on SSD/flash drives the hardware, to handle wear leveling, is almost guaranteed to write the data to a different sector. But that is transparent to the OS/file system. (It's possible on hard drives as well due to sector sparing)