简体   繁体   English

在接收管道数据的应用程序中使用mmap()?

[英]Use mmap() in application receiving piped data?

Is it possible to use mmap() in a C++ application on Linux which must receive piped input from a file and if so, is this faster than reading individual/page_amount bytes from the stream? 是否可以在Linux上的C ++应用程序中使用mmap(),该应用程序必须从文件接收管道输入,如果这样,这比从流中读取单个/ page_amount字节快吗?

I'm not sure mmap() is possible because piping involves a stream whereas wouldnt mmap() require all of the data availale immediately? 我不确定mmap()是否可行,因为管道涉及流,而mmap()是否会立即要求所有数据可用?

You can not mmap a stream because data in a stream buffer is intended to be read only once. 您不能mmap流,因为流缓冲区中的数据只能读取一次。 Also the length of receivable data is indeterminable at runtime. 此外,在运行时无法确定可接收数据的长度。 Your best bet is to mmap a sufficient amount of memory and then use it as a receiving buffer. 最好的选择是mmap足够的内存量,然后将其用作接收缓冲区。

If you are using stdio functions to read data form your stream then the chunk size won't matter. 如果您正在使用stdio函数读取流中的数据,则块大小将无关紧要。 The library will automatically choose the most optimal one (often one page at a time). 图书馆将自动选择最合适的一页(一次通常一页)。 But if you intend to use read then reading in chunks of pages will give you the best performance. 但是,如果您打算使用read则分页阅读将为您提供最佳性能。

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

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