我无法通过shell命令ipcs看到boost :: interprocess创建的共享内存

时间:2019-05-07 10:56:55

标签: c++ boost

我正在使用boost测试共享内存。另外,我注意到我可以使用shell命令“ ipcs -m”检查系统中现在有多少个共享内存。 在运行下面的代码之后,该应用程序运行良好,但是,我无法通过命令“ ipcs -m”看到该应用程序创建的共享内存 谁能告诉我为什么?我应该怎么做才能检查除ipcs之外的共享内存?

#include <boost/interprocess/shared_memory_object.hpp>
#include <boost/interprocess/mapped_region.hpp>
#include <iostream>

int main(int argc, char** argv) 
{
    boost::interprocess::shared_memory_object shdmem(boost::interprocess::open_or_create,
        "shm_data", boost::interprocess::read_write);
    shdmem.truncate(1024);
    std::cout << "created shm: " << shdmem.get_name() << std::endl;
    boost::interprocess::offset_t size;
    if (shdmem.get_size(size)) 
    {
        std::cout << "shm size: " << size << std::endl;
    }
    boost::interprocess::mapped_region region(shdmem, boost::interprocess::read_write);
    char *s = static_cast<char*>(region.get_address());
    strcpy(s, "Hello, world!");
    std::cout << "write shm finished." << std::endl;
    boost::interprocess::mapped_region region2(shdmem, boost::interprocess::read_only);
    char *i2 = static_cast<char*>(region2.get_address());
    std::cout << "read shm finished, value: " << i2 << std::endl;
    return 0;
}


0 个答案:

没有答案