Dylan Wang
Dylan Wang

Reputation: 111

how to get a shmid from a boost::interprocess::managed_shared_memory object

boost::interprocess will create a shm like this:

boost::interprocess::managed_shared_memory segment(boost::interprocess::open_or_create, "ContainerSharedMemory", 65536);

but how can a watch this shm like this:

/Tool/SHMCache$ ipcs -m

key        shmid      owner      perms      bytes      nattch     status      
0x00005feb 0          root       666        12000      2                       
0x00005fe7 32769      root       666        524288     2                       
0x00005fe8 65538      root       666        2097152    2                       
0x0001c08e 98307      root       777        2072       0                         

Upvotes: 0

Views: 642

Answers (1)

jfly
jfly

Reputation: 8010

managed_shared_memory is for cross-platform use which uses a BasicManagedMemoryImpl pointer to internal implementation on different OSes. For example, it uses basic_managed_windows_shared_memory as backend on Windows. managed_shared_memory doesn't have a method to get shmid for the sake of portability. If you OS supports system V shared memory, you can use basic_managed_xsi_shared_memory which has get_shmid() method and nearly the same interface as basic_managed_shared_memory. A simple example:

#include <boost/interprocess/xsi_shared_memory.hpp>
#include <boost/interprocess/mapped_region.hpp>

using namespace boost::interprocess;

int main(int argc, char *argv[])
{
    //Build XSI key (ftok based)
    xsi_key key(argv[0], 1);
    //Create a shared memory object.
    xsi_shared_memory shm (create_only, key, 1000);
    // TODO Remove if exists
    printf("shmid: %d\n", shm.get_shmid());
}

Then you can see it with ipcs -m if share memory is created successfully.

Upvotes: 1

Related Questions