i cannot see the shared memory created by the boost::interprocess via shell command ipcs

为君一笑 提交于 2021-02-10 04:32:26

问题


I am testing the shared memory using the boost. Also, i've noticed that i can use the shell command "ipcs -m" to check how many shared memory now in my system. After i ran the code below, the application worked well, but, i cannot see the shared memory created by the application via the command "ipcs -m" Can anyone tell me why? what should i do to check the shared memory except 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;
}


来源:https://stackoverflow.com/questions/56021045/i-cannot-see-the-shared-memory-created-by-the-boostinterprocess-via-shell-comm

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!