我正在使用 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;
}
Run Code Online (Sandbox Code Playgroud)