如何避免FileSystem的缓冲机制
以VirtualBox的虚拟磁盘为例:如果VirtualBox没有避开主机操作系统中文件系统的缓冲机制,则来宾操作系统中的文件系统会将数据从内存移动到内存。
事实上,我想在用户空间中编写一个文件系统(将所有目录和文件放在一个大文件中)。但是如果我使用c api,例如fread和fwrite,操作系统中的文件系统会缓冲My UserSpace-FileSystem读取、写入的数据。但是My UserSpace-FileSystem已经实现了自己的缓冲机制。如果我没有避免操作系统中文件系统的缓冲机制,我的UserSpace-FileSystem会将数据从内存移动到内存。这太糟糕了。
有谁知道如何解决这个问题?
Take VirtualBox's virtual disk as example:if VirtualBox didn't avoid the buffer mechanism from FileSystem in host os,the FileSystem in guest os would move data from memory to meory.
In fact ,I want to write a filesystem in user space(put all directorys and files in a single big file). But if I use c api such fread and fwrite ,the FileSystem in os would buffer the data that My UserSpace-FileSystem read、write.But My UserSpace-FileSystem has implement a buffer mechanism by itself.If i didn't avoid the buffer mechanism from FileSystem in os,My UserSpace-FileSystem would move data from memory to memory.It's so bad .
Dose anyone know how to solve this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
stdio
不支持这一点。对于 *NIX:
man open
对于 O_DIRECT,man fadvise
和man madvise
。对于 Windows,请检查
CreateFile
为FILE_FLAG_NO_BUFFERING
。挖掘可能是个好主意CreateFileMapping 也是如此。
stdio
doesn't support that.For *NIX:
man open
for O_DIRECT,man fadvise
andman madvise
.For Windows, check the
CreateFile
forFILE_FLAG_NO_BUFFERING
. Probably a good idea to dig theCreateFileMapping
too.您的问题不是很清楚,但如果您只想使用
stdio
而无需缓冲,那么setbuf(file, NULL);
将解决您的问题。更好的解决方案可能是完全避免stdio
并使用较低级别的 io 原语read
、write
等(不是普通 C 的一部分,而是由 POSIX 指定,并且在大多数非 POSIX 系统上也提供了几乎等效的版本)。Your question isn't very clear, but if all you want to do is use
stdio
without buffering, thensetbuf(file, NULL);
will solve your problem. A better solution might be to avoidstdio
entirely and use lower-level io primitivesread
,write
, etc. (not part of plain C but specified by POSIX, and with nearly-equivalent versions of them available on most non-POSIX systems too).