简体   繁体   English

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

[英]Reading Shared Memory from x86 to x64 and vice versa on OSX

If I create a SM from 64 bit application and open it on 32 bit application it fails. 如果我从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);

file created by 64bit application is at path as below: 64位应用程序创建的文件位于以下路径:

/private/tmp/boost_interprocess/AD21A54E000000000000000000000000/test

where as file searched by 32 bit application is at path 32位应用程序搜索的文件在路径中的位置

/private/tmp/boost_interprocess/AD21A54E00000000/test

Thus 32 bit applications cannot read the file. 因此,32位应用程序无法读取文件。

I am using boost 1.47.0 on Mac OS X. Is it a bug? 我在Mac OS X上使用boost 1.47.0。这是一个错误吗? Do I have to do some settings use some Macros in order to fix it? 我必须做一些设置才能使用一些宏来修复它吗? Has any one encountered this problem before? 有人遇到过这个问题吗?

Is it important that the shared memory be backed by a file? 共享内存由文件支持是否重要? If not, you might consider using the underlying Unix shared memory APIs: shmget, shmat, shmdt, and shmctl, all declared in sys/shm.h. 如果没有,您可以考虑使用底层Unix共享内存API:shmget,shmat,shmdt和shmctl,它们都在sys / shm.h中声明。 I have found them to be very easy to use. 我发现它们非常易于使用。

// 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();
}

Another process would use something like this to access the shared memory: 另一个进程将使用如下方式访问共享内存:

// 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();
}

Then, when all processes are done with the shared memory, use shmctl(id, IPC_RMID, 0) to release it back to the system. 然后,当所有进程都用共享内存完成时,请使用shmctl(id, IPC_RMID, 0)将其释放回系统。

You can use the ipcs and ipcrm tools on the command line to manage shared memory. 您可以在命令行上使用ipcs和ipcrm工具来管理共享内存。 They are useful for cleaning up mistakes when first writing shared memory code. 它们对于清除首次编写共享内存代码时的错误很有用。

All that being said, I am not sure about sharing memory between 32-bit and 64-bit programs. 话虽如此,我不确定在32位和64位程序之间共享内存。 I recommend trying the Unix APIs and if they fail, it probably cannot be done. 我建议尝试Unix API,如果它们失败,则可能无法完成。 They are, after all, what Boost uses in its implementation. 毕竟,它们是Boost在其实现中使用的东西。

I found the solution to the problem and as expected it is a bug. 我找到了解决问题的方法,并且按预期的那样是一个错误。

This Bug is present in tmp_dir_helpers.hpp file. 此错误存在于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)];
          }
       ...
    }

Where as it should have been some thing like this.. 应该在什么地方像这样。

**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)

I have created a ticket in boost for this bug. 我已经为此故障创建了一张票

Thank you. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM