Question

I have been learning cpp for a while now and I have been wondering how programms interact with each other and how to code such behaviour.

My objective is to learn how to code bots for simple games (minesweeper...) and automotive behaviour on startup for example.

I would like to know what the tools and libraries there are and if you could recommend some good tutorial/book about the subject. I prefer a high-level approach to begin with.

I am using Windows as well as linux.

Was it helpful?

Solution

Have a look into Inter-Process Communication (IPC).

Linux IPC

Windows IPC

Boost.Interprocess will help abstract out some of the differences between each platform.

Example Process Glasgow

#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;
}

Example Process Edinburgh

#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;
}

Build

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

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

Execution

./edinburgh

./glasgow

Output

Received 1 item.

First items content: You call that a capital city?.

Related question How do programs communicate with each other?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top