当前位置: 首页>>代码示例>>C++>>正文


C++ SharedMemory::alloc方法代码示例

本文整理汇总了C++中SharedMemory::alloc方法的典型用法代码示例。如果您正苦于以下问题:C++ SharedMemory::alloc方法的具体用法?C++ SharedMemory::alloc怎么用?C++ SharedMemory::alloc使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在SharedMemory的用法示例。


在下文中一共展示了SharedMemory::alloc方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1:

TEST(SharedMemoryTest, Alone) {
  SharedMemory memory;
  EXPECT_TRUE(memory.get_block() == nullptr);
  EXPECT_TRUE(memory.is_null());
  std::string meta_path = base_path() + std::string("_alone");
  COERCE_ERROR(memory.alloc(meta_path, 1ULL << 21, 0));
  EXPECT_TRUE(memory.get_block() != nullptr);
  EXPECT_EQ(1ULL << 21, memory.get_size());
  EXPECT_EQ(meta_path, memory.get_meta_path());
  EXPECT_NE(0, memory.get_shmid());
  EXPECT_NE(0, memory.get_shmkey());
  EXPECT_EQ(0, memory.get_numa_node());
  EXPECT_FALSE(memory.is_null());
  EXPECT_TRUE(memory.is_owned());
  EXPECT_EQ(::getpid(), memory.get_owner_pid());
  memory.release_block();
  EXPECT_TRUE(memory.get_block() == nullptr);
  EXPECT_TRUE(memory.is_null());
}
开发者ID:kumagi,项目名称:foedus_code,代码行数:19,代码来源:test_shared_memory.cpp

示例2: if

TEST(SharedMemoryTest, ShareFork) {
  bool was_child = false;
  {
    SharedMemory memory;
    EXPECT_TRUE(memory.get_block() == nullptr);
    EXPECT_TRUE(memory.is_null());
    std::string meta_path = base_path() + std::string("_share_fork");
    COERCE_ERROR(memory.alloc(meta_path, 1ULL << 21, 0));
    memory.get_block()[3] = 42;
    pid_t pid = ::fork();
    if (pid == -1) {
      memory.mark_for_release();
      ASSERT_ND(false);
      EXPECT_TRUE(false);
    } else if (pid == 0) {
      // child
      SharedMemory memory_child;
      memory_child.attach(meta_path);
      EXPECT_FALSE(memory_child.is_null());
      EXPECT_EQ(1ULL << 21, memory_child.get_size());
      EXPECT_EQ(meta_path, memory_child.get_meta_path());
      EXPECT_NE(0, memory_child.get_shmid());
      EXPECT_NE(0, memory_child.get_shmkey());
      EXPECT_EQ(0, memory_child.get_numa_node());
      EXPECT_EQ(42, memory_child.get_block()[3]);
      EXPECT_FALSE(memory.is_owned());
      was_child = true;
    } else {
      // parent
      int status;
      pid_t result = ::waitpid(pid, &status, 0);
      EXPECT_EQ(pid, result);
      EXPECT_EQ(0, status);
    }

    memory.release_block();
  }
  if (was_child) {
    ::_exit(0);  // otherwise, it goes on to execute the following tests again.
  }
}
开发者ID:kumagi,项目名称:foedus_code,代码行数:41,代码来源:test_shared_memory.cpp

示例3: meta_path

TEST(SharedMemoryTest, ShareSpawn) {
  const char* env_meta_path = ::getenv("test_meta_path");
  if (env_meta_path) {
    // child process!
    {
      std::string meta_path(env_meta_path);
      std::cout << "I'm a child process(" << ::getpid()
        << "). meta_path=" << meta_path << std::endl;
      SharedMemory memory;
      memory.attach(meta_path);
      EXPECT_FALSE(memory.is_null());
      EXPECT_EQ(1ULL << 21, memory.get_size());
      EXPECT_EQ(meta_path, memory.get_meta_path());
      EXPECT_NE(0, memory.get_shmid());
      EXPECT_NE(0, memory.get_shmkey());
      EXPECT_EQ(0, memory.get_numa_node());
      EXPECT_EQ(42, memory.get_block()[3]);
      EXPECT_FALSE(memory.is_owned());
      memory.get_block()[5] = 67;
    }
    ::_exit(0);  // for the same reason, this should terminate now.
    return;
  } else {
    std::cout << "I'm a master process(" << ::getpid() << ")" << std::endl;
  }

  SharedMemory memory;
  EXPECT_TRUE(memory.get_block() == nullptr);
  EXPECT_TRUE(memory.is_null());
  std::string meta_path = base_path() + std::string("_share_spawn");
  COERCE_ERROR(memory.alloc(meta_path, 1ULL << 21, 0));
  memory.get_block()[3] = 42;


  posix_spawn_file_actions_t file_actions;
  posix_spawnattr_t attr;
  ::posix_spawn_file_actions_init(&file_actions);
  ::posix_spawnattr_init(&attr);

  std::string path = assorted::get_current_executable_path();
  // execute this test
  char* const new_argv[] = {
    const_cast<char*>(path.c_str()),
    const_cast<char*>("--gtest_filter=SharedMemoryTest.ShareSpawn"),
    nullptr};

  // with meta_path in environment variable
  std::string meta("test_meta_path=");
  meta += meta_path;
  char* const new_envp[] = {
    const_cast<char*>(meta.c_str()),
    nullptr};

  pid_t child_pid;
  std::cout << "spawning: " << path << std::endl;
  int ret = ::posix_spawn(&child_pid, path.c_str(), &file_actions, &attr, new_argv, new_envp);

  EXPECT_EQ(0, ret);
  if (ret == 0) {
    EXPECT_NE(0, child_pid);
    int status = 0;
    pid_t result = ::waitpid(child_pid, &status, 0);
    std::cout << "child process(" << child_pid << ") died. result=" << result << std::endl;
    EXPECT_EQ(child_pid, result);
    EXPECT_EQ(0, status);
  }
  assorted::memory_fence_acquire();
  EXPECT_EQ(67, memory.get_block()[5]);
  memory.release_block();
}
开发者ID:kumagi,项目名称:foedus_code,代码行数:70,代码来源:test_shared_memory.cpp


注:本文中的SharedMemory::alloc方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。