views:

49

answers:

2

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?

+1  A: 

Your question isn't very clear, but if all you want to do is use stdio without buffering, then setbuf(file, NULL); will solve your problem. A better solution might be to avoid stdio entirely and use lower-level io primitives read, 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).

R..
It seem like lower-level io will buffer stream too.Thanks also.
shuitu
That's a separate layer of buffering which you generally should not try to avoid, although some misguided application vendors go to great lengths to do it... most infamously, Oracle.
R..
+4  A: 

stdio doesn't support that.

For *NIX: man open for O_DIRECT, man fadvise and man madvise.

For Windows, check the CreateFile for FILE_FLAG_NO_BUFFERING. Probably a good idea to dig the CreateFileMapping too.

Dummy00001
It is what i want .Thanks!
shuitu