C ++ multiple processes?

I have a project that consists of two processes, and I need to quickly and efficiently transfer some data between them.

I know that I could use sockets for this using TCP, although both processes will always exist on the same computer, however this is not a very efficient solution.

I see a lot of information about using pipes in Linux. However, I primarily want this for Windows and Linux (preferably through a cross-platform library), ideally in a safe type, a non-blocking way.

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

Also, is there a cross-platform way to create a new process?

+4
source share
4 answers

For IPC, Windows supports named pipes , just like Linux does , except pipe names follow a different format due to differences in path formats between the two operating systems. This is something you could overcome with a simple preprocessor. Both operating systems also support non-blocking pipe IOs and I / O multiplexing with select ().

+7
source

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:

  • Common memory.
  • Files with memory mapping.
  • Semaphores, mutexes, condition variables, and updatable mutex types for placing them in files with shared memory and memory.
  • Named versions of these synchronization objects, similar to the UNIX / Windows sem_open / CreateSemaphore API.
  • File Lock.
  • Relative pointers.
  • Message Queues

Boost.Interprocess also offers higher-level interprocess communication mechanisms for allocating dynamic sections of a shared memory or memory-mapped file (in general, for allocating portions of a fixed-size memory segment). Using these mechanisms, Boost.Interprocess offers useful tools for creating C ++ objects, including STL-like containers, in files with shared memory and memory:

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

Boost.Interprocess has been tested on 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
+12
source

Normal old TCP should work quite efficiently; as I understand it, a modern OS will detect when both ends of a TCP connection are located on the same computer and route this data internally using a quick, lightweight (tubular) mechanism, rather than through a regular TCP stack.

So, if you already have code that works over TCP, I say that I stick to this and do not spend a lot of extra development time so as not to gain much.

+2
source

This may be redundant, but you can use the Apache Portable Runtime; here are streaming and technological functions.

+1
source

All Articles