简体   繁体   English

Windows 系统中是否有无缓冲 I/O?

[英]Is there an un-buffered I/O in Windows system?

I want to find low-level C/C++ APIs, equivalent with "write" in linux systems, that don't have a buffer.我想找到没有缓冲区的低级 C/C++ API,相当于 linux 系统中的“写入”。 Is there one?有吗?

The buffered I/O such as fread, fwrite are not what I wanted. fread、fwrite 等缓冲 I/O 不是我想要的。

Look at CreateFile with the FILE_FLAG_NO_BUFFERING option使用 FILE_FLAG_NO_BUFFERING 选项查看CreateFile

http://www.codeproject.com/Articles/51678/Improve-responsiveness-in-Windows-with-the-FILE_FL http://www.codeproject.com/Articles/51678/Improve-responsiveness-in-Windows-with-the-FILE_FL

The only method to prevent swapping out cache is to open files with the FILE_FLAG_NO_BUFFERING flag.防止换出缓存的唯一方法是使用FILE_FLAG_NO_BUFFERING标志打开文件。 This, however, requires disk I/O requests to have sizes divisible by sector size (512 to 4096 bytes), which would require large rewrites of most applications that rely on being able to request different sizes.然而,这要求磁盘 I/O 请求的大小可以被扇区大小(512 到 4096 字节)整除,这需要对大多数依赖于能够请求不同大小的应用程序进行大量重写。

This project contains a drop-in wrapper that offers the CreateFile_NB() , ReadFile_NB() , WriteFile_NB() , and CloseHandle_NB() functions that take care of queuing and adjusting the file size when closing a file opened for writing.该项目包含一个插入式包装器,该包装器提供CreateFile_NB()ReadFile_NB()WriteFile_NB()CloseHandle_NB()函数,这些函数负责在关闭为写入而打开的文件时进行排队和调整文件大小。

http://msdn.microsoft.com/en-us/library/cc644950(v=vs.85).aspx http://msdn.microsoft.com/en-us/library/cc644950(v=vs.85).aspx

When opening or creating a file with the CreateFile function, the FILE_FLAG_NO_BUFFERING flag can be specified to disable system caching of data being read from or written to the file.使用 CreateFile function 打开或创建文件时,可以指定FILE_FLAG_NO_BUFFERING标志来禁用从文件读取或写入文件的数据的系统缓存。 Although this gives complete and direct control over data I/O buffering, in the case of files and similar devices there are data alignment requirements that must be considered.尽管这可以完全直接地控制数据 I/O 缓冲,但对于文件和类似设备,必须考虑数据 alignment 要求。

The Win32 equivalence of the POSIX write() function is WriteFile() . POSIX write() function 的 Win32 等效项是WriteFile() The documentation recommends using un-buffered file I/O, and recommends this page for further information.该文档建议使用非缓冲文件 I/O,并推荐此页面以获取更多信息。

streams are about as low level as you can get.. and they can be unbuffered.流大约是您可以获得的最低级别..并且它们可以是无缓冲的。

int setvbuf(
   FILE *stream,
   char *buffer,
   int mode,
   size_t size 
);

example例子

  setvbuf(stdout, (char *)NULL, _IONBF, 0); //unbuffered stdout

here is an extract from the vc2008 help document.这是 vc2008 帮助文档的摘录。

The setvbuf function allows the program to control both buffering and buffer size for stream. setvbuf function 允许程序控制 stream 的缓冲和缓冲区大小。 stream must refer to an open file that has not undergone an I/O operation since it was opened. stream 必须引用自打开后未进行 I/O 操作的打开文件。 The array pointed to by buffer is used as the buffer, unless it is NULL, in which case setvbuf uses an automatically allocated buffer of length size/2 * 2 bytes. buffer 指向的数组被用作缓冲区,除非它是 NULL,在这种情况下 setvbuf 使用长度为 size/2 * 2 字节的自动分配的缓冲区。

The mode must be _IOFBF , _IOLBF , or _IONBF .模式必须是_IOFBF_IOLBF_IONBF If mode is _IOFBF or _IOLBF , then size is used as the size of the buffer.如果 mode 是_IOFBF_IOLBF ,则 size 用作缓冲区的大小。 If mode is _IONBF, the stream is unbuffered and size and buffer are ignored.如果模式为 _IONBF,则 stream 无缓冲,大小和缓冲区将被忽略。 Values for mode and their meanings are: mode 的值及其含义是:

_IOFBF Full buffering; _IOFBF全缓冲; that is, buffer is used as the buffer and size is used as the size of the buffer.即buffer作为缓冲区,size作为缓冲区的大小。 If buffer is NULL, an automatically allocated buffer size bytes long is used.如果缓冲区是 NULL,则使用自动分配的缓冲区大小字节长。

_IOLBF For some systems, this provides line buffering. _IOLBF对于某些系统,这提供了行缓冲。 However, for Win32, the behavior is the same as _IOFBF - Full Buffering.但是,对于 Win32,行为与 _IOFBF - Full Buffering 相同。

_IONBF No buffer is used, regardless of buffer or size. _IONBF不使用缓冲区,无论缓冲区或大小如何。

You can use _write MSDN Page Here .您可以使用_write MSDN Page Here

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM