Git Product home page Git Product logo

multiprocessing's Introduction

C++ Multiprocessing with Shared Memory

By [email protected]

Producer-consumer is a popular architecture for building multiprocessing or multithreaded software. This implementation demonstrates multiprocessing, shared memory, memory pools, fixed-size queues and semaphores.

I've created C++ SharedMemory and Semaphore classes for multi-processing with support for Windows and UNIX. Features a fixed-size RingBuffer queue template I created that doesn't use the heap. Is anyone interested? Would like to hear about any similar efforts, what history there may be of trying to standardize these types of components. Links appreciated.

The Producer produces a Product object that contains a sequential id (int) and and random floating point number (double) at intervals of between 0.5 and 1.5 seconds each. The Consumer consumes Products at random intervals between 1.0 and 3.0 seconds. It is just an example to demonstrate MP concepts.

Build

Win64:

cmake .. -A x64

Linux:

cmake -DCMAKE_BUILD_TYPE=Debug ..

If you want to run in debugger.

cmake -DMEM_SYS_V ..

Builds with BSD shared memory by default. If you prefer System V, define MEM_SYS_V.

ulimit -c unlimited

FYI, how to enable core dumps in Linux bash, if you need.

Running

Simply run test_consumer. It will spawn test_producer automatically. For Windows, you must change the #define PRODUCER path in test_consumer.cpp to match where you build the test_producer executable. The Linux path should just work, spawns ./test_producer.

System APIs Utilized

  • System V
  • POSIX BSD
  • Windows

Single Codebase Design Using Cmake

  • Windows
  • Linux
  • MacOS (untested)
  • Solaris (untested)

C++ Classes

  1. SharedMemory classes encapsulate Windows, BSD and System V shared memory APIs
  2. MemoryPool class is base class of SharedMemory
  3. Semaphore class encapsulates Windows, BSD and System V APIs
  4. RingQueue template is a fixed-size nothrow queue class inspired by std::queue
  5. Product class contains a serial id and a floating point number
  6. test/Producer class implements producer logic
  7. test/Consumer class implements consumer logic

Design

Shared memory has yet to be standardized in C++, althougth WG21 SC22 is considering std::memory_mappable and std::mapped_region:

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n2044.html

And Boost offers boost::interprocess:

https://www.boost.org/doc/libs/1_54_0/doc/html/interprocess/sharedmemorybetweenprocesses.html

Because it's not standard, not using either of those. Created class SharedMemory instead. Using placement operator new() to put RingQueue in shared memory. RingQueue running in a single producer-consumer shared memory configuration is designed to be lockfree, but hasn't been tested for that. The purpose of the semaphore is to wake.

C++ offers std::mutux for multi-threaded applications, but to work across processes encapsulated OS-specific semaphore API instead.

Requirements Document

multiprocessing's People

Contributors

cinepaint avatar robinrowe avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.