Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ multiple processes?

I've got a project that consists of two processes and I need to pass some data between them in a fast and efficent manner.

I'm aware that I could use sockets to do this using TCP, even though both processes will always exist on the same computer, however this does not seem to be a very efficient solution.

I see lots of information about using "pipes" on Linux. However I primarily want this for Windows and Linux (preferably via a cross platform library), ideally in a type safe, non-blocking manner.

Another important thing is I need to support multiple instances of the whole application (i.e. both processes), each with their own independent copy of the communication objects.

Also is there a cross platform way to spawn a new process?

like image 910
Fire Lancer Avatar asked Nov 28 '22 20:11

Fire Lancer


1 Answers

Take a look at Boost.Interprocess

Boost.Interprocess simplifies the use of common interprocess communication and synchronization mechanisms and offers a wide range of them:

  • Shared memory.
  • Memory-mapped files.
  • Semaphores, mutexes, condition variables and upgradable mutex types to place them in shared memory and memory mapped files.
  • Named versions of those synchronization objects, similar to UNIX/Windows sem_open/CreateSemaphore API.
  • File locking.
  • Relative pointers.
  • Message queues.

Boost.Interprocess also offers higher-level interprocess mechanisms to allocate dynamically portions of a shared memory or a memory mapped file (in general, to allocate portions of a fixed size memory segment). Using these mechanisms, Boost.Interprocess offers useful tools to construct C++ objects, including STL-like containers, in shared memory and memory mapped files:

  • Dynamic creation of anonymous and named objects in a shared memory or memory mapped file.
  • STL-like containers compatible with shared memory/memory-mapped files.
  • STL-like allocators ready for shared memory/memory-mapped files implementing several memory allocation patterns (like pooling).

Boost.Interprocess has been tested in the following compilers/platforms:

  • Visual 7.1 Windows XP
  • Visual 8.0 Windows XP
  • GCC 4.1.1 MinGW
  • GCC 3.4.4 Cygwin
  • Intel 9.1 Windows XP
  • GCC 4.1.2 Linux
  • GCC 3.4.3 Solaris 11
  • GCC 4.0 MacOs 10.4.1
like image 157
Kirill V. Lyadvinsky Avatar answered Dec 01 '22 11:12

Kirill V. Lyadvinsky