使用boost :: iostreams :: mapped_file [英] Using boost::iostreams::mapped_file

查看:407
本文介绍了使用boost :: iostreams :: mapped_file的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对内存映射非常陌生,并试图了解内存映射文件以在我的项目(基于Linux)中使用它们. 我的要求是写&然后从内存映射文件中读取. 我编写了一个示例程序,该程序只能编写并且可以正常工作,但是由于我对内存映射的这一基本知识不甚了解,因此我有一些非常基本的疑问.

I am very new to the memory mapping and trying to understand memory mapped files to use them in my project(linux based). My requirement is to write & then read from memory mapped files. I wrote a sample program which only writes and it works fine but i have a few very basic doubts as i do not understand this funda of memory mapping properly.

#include <unordered_map>
#include <boost/iostreams/device/mapped_file.hpp>
using namespace boost::interprocess;
using namespace std;
typedef unordered_map<int, string> work;
int main()
{
        boost::iostreams::mapped_file_params  params;
        params.path = "map.dat";
        params.new_file_size = 100;
        params.mode = (std::ios_base::out | std::ios_base::in);
        boost::iostreams::mapped_file  mf;
        mf.open(params);
        work w1;
        w1[0] = "abcd";
        w1[1] = "bcde";
        w1[2] = "cdef";

        work* w = static_cast<work*>((void*)mf.data());
        *w = w1;
        mf.close();
        return 0;
}

我在这里有几个问题:

  1. 当我这样做时:mf.open(params),我看到在磁盘上创建了一个大小为100的文件. 现在,当我向它写入* w = w1时,磁盘上文件的内容就会更改. 这是否表示我根本不使用RAM,而是直接写入
    磁盘?

  1. When i do this : mf.open(params) , i see that a file is created on disk with size 100. Now when i write to it i.e *w = w1, the contents of the file on disk changes. Does this mean that i am not using the RAM at all and i am writing straight into the
    disk?

当我执行mf.size()时,它总是给我提供我作为创建输入的尺寸 实际文件.如何找出我实际写入
的数据大小 内存映射文件?

When i do mf.size(), it always give me the size which i gave as the input for creating the actual file. How can i find out the size of the data that i actually wrote into the
memory mapped file?

此外,如果我给params.new_file_size = 10GB,则会在
上创建该大小的文件. 磁盘,但不占用任何磁盘空间.使用df cmd确认.为什么这样? -rwx ------ 1根根10000000000 4月29日14:26 map.dat

Also if i give params.new_file_size = 10GB, the file of that size gets created on the
disk but it does not take up any disk space.Confirmed by using df cmd. Why so? -rwx------. 1 root root 10000000000 Apr 29 14:26 map.dat

我读到关闭文件可以释放映射.这是否意味着收盘后我失去了所有 我写的数据?但这是不正确的,因为我在关闭并有工作代码的情况下 然后再次打开文件并正确阅读.

I read that close file frees the mapping. Does this mean that after close i lose all the data that i wrote? But this is not true as i have the working code where i close and then open the file again and read it correctly.

如何删除使用后创建的内存映射文件?通过使用rm -rf cmd/linux API?

How to delete the memory mapped files created after use? By using rm -rf cmd/linux apis?

推荐答案

  • 当我这样做时:mf.open(params),我看到在磁盘上创建了一个大小为100的文件.现在当我向它写入文件时,即* w = w1,磁盘上文件的内容就会更改.这是否表示我根本不使用RAM,而是直接写入 磁盘?
  • When i do this : mf.open(params) , i see that a file is created on disk with size 100. Now when i write to it i.e *w = w1, the contents of the file on disk changes. Does this mean that i am not using the RAM at all and i am writing straight into the disk?

您正在使用内存映射文件.这意味着两者:您正在写入已映射到您的进程空间中的虚拟内存页面",但实际上是指磁盘块.增长表明页面在写入时已提交.

You're using memory mapped files. This means both: you are writing to 'virtual memory pages' that have been mapped into your process space, but actually refer to disk blocks. The growth indicates that the pages get committed on write.

  • 当我执行mf.size()时,它总是为我提供作为创建实际文件的输入的大小.我如何找出我实际写入的数据大小 内存映射文件?
  • When i do mf.size(), it always give me the size which i gave as the input for creating the actual file. How can i find out the size of the data that i actually wrote into the memory mapped file?

不能.您只能找到使用stat

You can't. You can only find the number of blocks committed with a tool like stat

  • 此外,如果我给params.new_file_size = 10GB,则会在 磁盘,但不占用任何磁盘空间.使用df cmd确认.为什么这样? -rwx ------ 1个根目录10000000000 4月29日14:26 map.dat
  • Also if i give params.new_file_size = 10GB, the file of that size gets created on the disk but it does not take up any disk space.Confirmed by using df cmd. Why so? -rwx------. 1 root root 10000000000 Apr 29 14:26 map.dat

已稀疏分配.例如.在其他平台上使用fallocate或类似版本.

It's sparsely allocated. E.g. using fallocate or similar on other platforms.

  • 我读到关闭文件可以释放映射.这是否意味着收盘后我会丢失所有写入的数据?但这是不正确的,因为我在这里关闭了工作代码,然后再次打开文件并正确读取了文件.
  • I read that close file frees the mapping. Does this mean that after close i lose all the data that i wrote? But this is not true as i have the working code where i close and then open the file again and read it correctly.

不.这意味着映射已释放.也就是说,您的进程空间中的/virtual memory/区域现在可以自由"用于其他用途.

No. It means that the mapping is freed. That is, the /virtual memory/ area in your process space is now 'free' to use for other things.

  • 如何删除使用后创建的内存映射文件?通过使用rm -rf cmd/linux API?
  • How to delete the memory mapped files created after use? By using rm -rf cmd/linux apis?

是的

这篇关于使用boost :: iostreams :: mapped_file的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆