[英]Reserving Shared Memory with No File Backing (Linux/Windows) (boost::interprocess)
[英]Under Linux boost::interprocess::create_or_open_file change the file type
我正在移植源代码来打开/读取/写入多个进程之间共享的文件。 它在windows下运行良好,因为它主要使用boost :: interprocess(1.44)我没想到太多问题,但我发现了一些奇怪的东西:
//pseudo code
namespace bip = boost::interprocess;
namespace bipd = boost::interprocess::detail;
loop
bip::file_handle_t pFile = bipd::create_or_open_file(filename, bip::read_write);
bipd::acquire_file_lock(pFile);
// try to read bytes from pFile with 'read'
bipd::truncate_file(pFile, 0);
bipd::write_file(pFile, (const void*)(textBuffer)), bufLen);
当代码第一次运行时,它会创建文件并写入文本。 文件模式是ASCII( ASCII text, with very long lines
),我可以读取文本。 但是当循环第二次运行时,文件类型将更改为data
, textBuffer
将作为文件而textBuffer
二进制数据!
我检查了boost/interprocess/details/os_file_functions.hpp
但我没有找到这种行为的原因。
你有个主意吗?
最后,我找到了一个解决方案......如果你在文件的末尾( ::read
之后的文件指针位置),那么在执行`boost :: interprocess ::时使用的::ftruncate
函数detail :: truncate_file'导致错误的行为。
为了在Linux和Windows下保持相同的行为(保持我的文件类型为ASCII文本),我使用了一个简单的::seek(id,0,SEEK_SET)
。
我没有在我读过的所有页面中找到这个技巧!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.