Running programs from C ++, exchanging information with pipes, and then shared memory

I am currently developing a modular structure using shared memory in C and C ++. The goal is to have independent programs in both C and C ++, talk to each other through shared memory.

eg. one program is responsible for reading GPS and the other is responsible for processing data from several sensors.

The master program will start all subordinate programs (I am currently using it fp = popen(./slave1/slave1,"r");for this), and then create segments of shared memory that each subordinate can connect to. The idea that if the slave dies, it can be restored by the wizard and reconnected to the same shared memory segment.
Slaves can also be exchanged at runtime (for example, switching one GPS from another).

The problem is that I run the slave through popen and pass the shared memory identifier to the slave. Through the pipe, the follower transfers back the required size. After that, I want to redirect the slave channel to the terminal in order to display debug messages and not go through the master.

Suggestions are welcome, as well as other solutions to the problem. The key must have some form of communication before setting up shared memory.

+3
source share
4 answers

Pipes are not redirected - they go where they go when they were created. What you need to do is get the slave to close the pipe when it is over, and then open his camp again in another place. If you always want to output to the terminal, you can use it freopen("/dev/tty", "w", stdout), but then it will always work with the terminal - you cannot redirect it to another place.

0
source

I suggest using another means of communication. A named pipe is what I think. At best, redirecting the standard out / err will be difficult.

boost.interprocess IPC. :)

my2c

+1

, SCM_RIGHTS unix - , stderr .

( , Linux). /dev/shm . ftruncate mmap MAP_SHARED. , . - , , , .

By combining this, you can transfer your slaves to one end of the unix domain socketpair(). Just leave fd open over exec and pass the fd number on the command line. Pass any configuration information you want, like regular data on a pair of sockets, then pass a file descriptor that points to a shared memory segment.

+1
source

To fix a specific problem, send debugging messages to stderr, not to stdout.

0
source

Source: https://habr.com/ru/post/1790501/


All Articles