5

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;
}


Bingoal Lee
  • 61
  • 1
  • 4
  • What if you add a sleep(60) in the middle of the program just after it creates the shared memory and writes to it? Can you see the memory from outside while the program is still alive? – John Zwinck May 07 '19 at 11:11
  • 3
    As far as I'm aware, ipcs reports only System V shared memory. POSIX shared memory is a separate area, and has to be queried separately. On linux systems, check /dev/shm. – Dave S May 07 '19 at 11:46
  • @Dave S, yeah, exactly! i tested the POSIX shared memory in another C example without Boost and ipcs did not report that either. And, I see all the shared memory in /dev/shm, thank you very much! – Bingoal Lee May 08 '19 at 01:35

0 Answers0