与其他计划互动;不是在c ++中

时间:2014-01-27 13:33:04

标签: c++ process ipc bots

我一直在学习cpp一段时间了,我一直想知道程序如何相互交互以及如何编写这样的行为。

我的目标是学习如何为简单的游戏(扫雷......)和启动时的汽车行为编码机器人。

我想知道有哪些工具和库,以及你是否可以推荐一些关于这个主题的好教程/书。我更喜欢高级方法开始。

我正在使用Windows和Linux。

1 个答案:

答案 0 :(得分:2)

查看Inter-Process Communication (IPC)

Linux IPC

Windows IPC

Boost.Interprocess将有助于抽象出每个平台之间的一些差异。

示例流程格拉斯哥

#include <cstdio>
#include <string>
#include <thread>
#include <chrono>
#include <cstdlib>

#include <boost/interprocess/creation_tags.hpp>
#include <boost/interprocess/containers/string.hpp>
#include <boost/interprocess/shared_memory_object.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <boost/interprocess/managed_shared_memory.hpp>

using std::string;
using std::char_traits;
using std::chrono::seconds;

using boost::interprocess::allocator;
using boost::interprocess::create_only;
using boost::interprocess::basic_string;
using boost::interprocess::shared_memory_object;
using boost::interprocess::managed_shared_memory;

typedef allocator< char, managed_shared_memory::segment_manager > shared_string_allocator;
typedef basic_string< char, char_traits< char >, shared_string_allocator > shared_string;

int main( int, char** )
{
    const char* TAG = "IPC-Example";
    const size_t LIMIT = 1024;

    shared_memory_object::remove( TAG );

    managed_shared_memory wormhole( create_only, TAG, LIMIT );

    wormhole.construct< shared_string >( "developer" )( "You call that a capital city?", wormhole.get_segment_manager( ) );

    std::this_thread::sleep_for( seconds( 10 * 60 ) );

    return EXIT_SUCCESS;
}

示例流程爱丁堡

#include <cstdio>
#include <string>
#include <cstdlib>

#include <boost/interprocess/creation_tags.hpp>
#include <boost/interprocess/containers/string.hpp>
#include <boost/interprocess/shared_memory_object.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <boost/interprocess/managed_shared_memory.hpp>

using std::pair;
using std::string;
using std::char_traits;

using boost::interprocess::allocator;
using boost::interprocess::basic_string;
using boost::interprocess::open_or_create;
using boost::interprocess::shared_memory_object;
using boost::interprocess::managed_shared_memory;

typedef allocator< char, managed_shared_memory::segment_manager > shared_string_allocator;
typedef basic_string< char, char_traits< char >, shared_string_allocator > shared_string;

int main( int, char** )
{
    const char* TAG = "IPC-Example";
    const size_t LIMIT = 1024;

    managed_shared_memory wormhole( open_or_create, TAG, LIMIT );

    auto item = wormhole.find< shared_string >( "developer" );
    auto content = item.first;
    auto count = item.second;

    printf( "Received %lu item.\n", count );
    printf( "First items content: %s.\n", content->data( ) );

    return EXIT_SUCCESS;
}

<强>构建

  

g ++ -std = c ++ 11 -o glasgow glasgow.cpp -lpthread -lrt

     

g ++ -std = c ++ 11 -o edinburgh edinburgh.cpp -lpthread -lrt

<强>执行

./爱丁堡

./格拉斯哥

<强>输出

  

收到1件物品。

     

第一项内容:您称之为首都?。

相关问题How do programs communicate with each other?