Search code examples
c++cmatlabsystem-callsmex

Launch one C++ application from another, and communicate with it


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.


Solution

  • 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