1

I have a C++ (technically MATLAB mex) program, which I am planning to use to launch a stand-alone pure C++ slave program on my system. The master calling program may look something like the following:

void mexFunction(int nlhs,mxArray *plhs[],int nrhs,const mxArray *prhs[]){
    system ("path/to/slave/program");
}

Once the slave is launched, I would then like to have a second mex program which will communicate with and send data to the slave program. The data sent will be in the form of large-ish C++ arrays.

I imagine that I will need some kind of handle to the slave program (perhaps its pid?), a method for sending messages and presuamably a way for the slave program to listen out for incoming messages.

I have no experience in getting separate C++ programs to communicate with each other, so any hints in this area would be appreciated. In addition, if there are any specific MATLAB mex-specific caveats, I would be interested to hear about these.


EDIT: I should have mentioned that I am building this on Ubuntu, but will ultimately like it to work on all platforms. Platform specific advice very welcome, but multi-platform ideas are really what I'm after.

karlphillip
  • 92,053
  • 36
  • 243
  • 426
Bill Cheatham
  • 11,396
  • 17
  • 69
  • 104

5 Answers5

8

You are looking for an IPC (Interprocess Communication) mechanism.

Boost has an entire chapter on this and is a cross-platform solution.

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
karlphillip
  • 92,053
  • 36
  • 243
  • 426
2

If you have time, as suggested in previous answers, you should definitely go for IPC.

However, there is also many "quick and dirty" solutions, where you don't need to spend time reading any documentations.

The one I can recommend you, is to use files. When you want to communicate, Process 1 write down a file with the arguments. And then another file to say that the arguments are ready. Process 2, have a loop waiting for this second file. If it finds it, it remove it and then read the arguments.

I know this is dirty, but this is very fast to program, and no need to read any documentations.

If you have large arguments, and you would waste a lot of time writing them on the hard disk. I suggest you something even dirtier: mounting the RAM, and writing on it:

mkdir -p /tmp/ram
sudo mount -t tmpfs -o size=512M tmpfs /tmp/ram/
Oli
  • 15,935
  • 7
  • 50
  • 66
  • A great answer for many situations. I'm looking to make something a little more robust, but this KISS solution is a fantastic one. – Bill Cheatham Jan 16 '12 at 13:55
1

I highly recommend COM technology for all Windows communication.
http://www.microsoft.com/com/default.mspx

By the way, if you want to use Matlab code directly, you can compile COM component using Matlab Builder NE, so you don't need to write mex at all.

Andrey Rubshtein
  • 20,795
  • 11
  • 69
  • 104
  • Thanks, this looks like it could be worth looking into. I should have mentioned that I am building this on Ubuntu, but it ultimately will be used on Windows platforms as well (perhaps this will be the hardest challenge!) – Bill Cheatham Jan 05 '12 at 18:20
1

If you're looking for a cross platform solution for communication checkout boost::interprocess. The documentation also has quite a bit of information about how these things work.

alanxz
  • 1,966
  • 15
  • 17
  • Great thanks. So if I were to be sending ~10Mb using this, should I write it to shared memory? Like in this example http://www.boost.org/doc/libs/1_48_0/doc/html/interprocess/quick_guide.html ? – Bill Cheatham Jan 05 '12 at 18:30
  • Yes - I would use shared memory. You will also need to use something like a [condition](http://www.boost.org/doc/libs/1_48_0/doc/html/interprocess/synchronization_mechanisms.html#interprocess.synchronization_mechanisms.conditions) to signal to your child process that the data in the shared memory is ready to use. – alanxz Jan 05 '12 at 21:30
0

This is platform specific, but on a POSIX platform you can use popen(3) to launch a command, giving you a pipe you can use to write data to its standard input (and also read from its standard output).

More portably, but less simply, the Boost.Interprocess library has all sorts of ways of communicating between processes.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644