在OSX系统中,如何在x86和x64之间读取共享内存?

4
如果我从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位应用程序搜索的文件路径为

/private/tmp/boost_interprocess/AD21A54E00000000/test

因此,32位应用程序无法读取该文件。
我正在使用Mac OS X上的boost 1.47.0。 这是一个错误吗?我需要进行一些设置或使用一些宏来修复它吗?有人遇到过这个问题吗?
2个回答

1

共享内存是否需要由文件支持很重要吗?如果不需要,您可以考虑使用底层的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_RMID, 0)将其释放回系统。

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

话虽如此,我不确定32位和64位程序之间是否可以共享内存。我建议尝试Unix API,如果失败了,那么可能无法实现。毕竟,这就是Boost在其实现中使用的API。


抱歉,我不能使用本机Unix共享内存API,因为我必须保持代码跨平台。 - RLT
@Rahul,你的目标平台是哪些? - Randall Cook
WIN XP/VISTA/7和MAC OS 10.6/7 - RLT
谢谢,@Rahul。好的,至少你找到并解决了问题。干得好。 - Randall Cook

1

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

这个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)

我已在boost中创建了一个以修复这个bug。
谢谢。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接