Boost管理的映射文件:在OSX和Linux之间不兼容

Boost managed mapped files: not compatible between OSX and Linux?

本文关键字:OSX Linux 之间 不兼容 管理 映射 文件 Boost      更新时间:2023-10-16

下面的代码创建并读取一个名为MyMappedFile的文件。如果我在Linux(Ubuntu 14.04)上运行写代码,我可以将这个文件复制到其他Linux机器上,它读起来很好。如果我把它转移到OSX并尝试在上面运行读取代码,我会得到一个错误:

libc++abi.dylib: terminating with uncaught exception of type boost::interprocess::lock_exception: boost::interprocess::lock_exception

相反,在OSX上编写的文件可以在OSX中读取。但试图在Linux下阅读它们给了我一个类似的信息:

terminate called after throwing an instance of 'boost::interprocess::lock_exception'
  what():  boost::interprocess::lock_exception

生成的文件不同。

我用这个代码写作:

#include <boost/interprocess/managed_mapped_file.hpp>
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/containers/string.hpp>
namespace bip=boost::interprocess;
typedef bip::managed_shared_memory::segment_manager segment_manager_t;
typedef bip::basic_string<char, std::char_traits<char>, bip::allocator<char, segment_manager_t> > shared_string;
int main() {
  bip::managed_mapped_file *outfile = new bip::managed_mapped_file(bip::create_only, "MyMappedFile", 1000);
  outfile->find_or_construct<shared_string>("mystring")("bubza", outfile->get_segment_manager());
  outfile->flush();
}

阅读:

#include <boost/interprocess/managed_mapped_file.hpp>
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/containers/string.hpp>
namespace bip=boost::interprocess;
typedef bip::managed_shared_memory::segment_manager segment_manager_t;
typedef bip::basic_string<char, std::char_traits<char>, bip::allocator<char, segment_manager_t> > shared_string;
int main() {
  bip::managed_mapped_file *infile = new bip::managed_mapped_file(bip::open_only, "MyMappedFile");
  shared_string *ptr = infile->find_or_construct<shared_string>("mystring")(infile->get_segment_manager());
  std::cout << "I got " << *ptr << std::endl;
}

这两台机器都使用GCC 4.8(Linux上为4.8.4,OSX上为4.8.5)和Boost 1.55。

为什么这些文件在这些平台之间不兼容?

请注意,我说的是由编写代码生成的MyMappedFile数据文件。编译代码生成的可执行文件不会在机器之间传输:Linux代码在Linux机器上编译,OSX代码在OSX机器上编译。

RE:为什么这些平台之间的文件不兼容

OSX和Linux使用不同的内核和ABI,因此它们以不同的方式编译代码。Linux通常使用ELF文件格式,而OS X使用Mach-O,这是特定于平台的。

编辑:不知道为什么您的输出不同(未共享)。