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。这是一个错误吗?我是否必须使用某些宏进行一些设置才能修复它?有没有人遇到过这个问题?

4

2 回答 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 在其实现中使用的。

于 2011-10-24T23:05:16.367 回答
1

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

此错误存在于 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)

我已经为这个错误创建了一张票

谢谢你。

于 2011-10-25T08:58:15.167 回答