Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/160.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在OSX上从x86读取共享内存到x64,反之亦然_C++_Macos_Boost_Shared Memory_Boost Interprocess - Fatal编程技术网

C++ 在OSX上从x86读取共享内存到x64,反之亦然

C++ 在OSX上从x86读取共享内存到x64,反之亦然,c++,macos,boost,shared-memory,boost-interprocess,C++,Macos,Boost,Shared Memory,Boost Interprocess,如果我从64位应用程序创建SM并在32位应用程序上打开它,它将失败 //for 64 bit shared_memory_object( create_only, "test" , read_write) ; // for 32 bit shared_memory_object (open_only, "test", read_write); 64位应用程序创建的文件位于以下路径: /private/tmp/boost_interprocess/AD21A54E0000000

如果我从64位应用程序创建SM并在32位应用程序上打开它,它将失败

//for 64 bit
    shared_memory_object( create_only, "test" , read_write) ; 
// for 32 bit
    shared_memory_object (open_only, "test", read_write);
64位应用程序创建的文件位于以下路径:

/private/tmp/boost_interprocess/AD21A54E000000000000000000000000/test
其中,32位应用程序搜索的as文件位于路径

/private/tmp/boost_interprocess/AD21A54E00000000/test
因此,32位应用程序无法读取该文件

我正在Mac OS X上使用boost 1.47.0。
是虫子吗?我需要做一些设置,使用一些宏来修复它吗?以前是否有人遇到过此问题?

共享内存由文件备份是否重要?如果不是,您可以考虑使用底层UNIX共享内存API:SHMGET、SHMAT、SHMDT和SHMCTL,它们都在sys /SHM.H中声明。我发现它们很容易使用

// create some shared memory
int id = shmget(0x12345678, 1024 * 1024, IPC_CREAT | 0666);

if (id >= 0)
{
    void* p = shmat(id, 0, 0);

    if (p != (void*)-1)
    {
        initialize_shared_memory(p);

        // detach from the shared memory when we are done;
        // it will still exist, waiting for another process to access it
        shmdt(p);
    }
    else
    {
        handle_error();
    }
}
else
{
    handle_error();
}
另一个进程将使用类似的方式访问共享内存:

// access the shared memory
int id = shmget(0x12345678, 0, 0);

if (id >= 0)
{
    // find out how big it is
    struct shmid_ds info = { { 0 } };

    if (shmctl(id, IPC_STAT, &info) == 0)
        printf("%d bytes of shared memory\n", (int)info.shm_segsz);
    else
        handle_error();

    // get its address
    void* p = shmat(id, 0, 0);

    if (p != (void*)-1)
    {
        do_something(p);

        // detach from the shared memory; it still exists, but we can't get to it
        shmdt(p);
    }
    else
    {
        handle_error();
    }
}
else
{
    handle_error();
}
然后,当所有进程都使用共享内存完成时,使用
shmctl(id,IPC\u RMID,0)
将其释放回系统

您可以在命令行上使用ipcs和ipcrm工具来管理共享内存。它们对于在首次编写共享内存代码时清除错误非常有用


尽管如此,我不确定32位和64位程序之间是否共享内存。我建议尝试unixapi,如果失败,可能无法实现。毕竟,它们是Boost在其实现中使用的。

我找到了问题的解决方案,正如预期的那样,这是一个bug

此错误存在于tmp_dir_helpers.hpp文件中

    inline void get_bootstamp(std::string &s, bool add = false)
    {
      ...
       std::size_t char_counter = 0;
       long  fields[2] = { result.tv_sec, result.tv_usec };
       for(std::size_t field = 0; field != 2; ++field){
          for(std::size_t i = 0; i != sizeof(long); ++i){
             const char *ptr = (const char *)&fields[field];
             bootstamp_str[char_counter++] = Characters[(ptr[i]&0xF0)>>4];
             bootstamp_str[char_counter++] = Characters[(ptr[i]&0x0F)];
          }
       ...
    }
本来应该是这样的事情

**long long** fields[2] = { result.tv_sec, result.tv_usec };
           for(std::size_t field = 0; field != 2; ++field){
              for(std::size_t i = 0; i != sizeof(**long long**); ++i)
我已经为这个bug创建了一个插件


谢谢。

对不起。我不能使用本机Unix共享内存API,因为我必须保持代码跨平台。@Rahul,您的目标平台是哪些?WIN XP/VISTA/7&MAC OS 10.6/7谢谢,@Rahul。好吧,至少你发现并解决了这个问题。抢手货