tags:

views:

207

answers:

9

I want to pass a value of an input variable in my program lets say#1 to another program #2 and i want #2 to print the data it got to screen, both are needed to be written in c++. The this will be on Linux.

+7  A: 

Depending on the platform there are a number of options available. What you are trying to do is typically called inter-process communication (IPC).

Some options include:

  • Sockets
  • Pipes
  • Queues
  • Shared Memory

What is easiest is probably dependent on the platform youa are using.

Nic Strong
This operation will take place on a GNU/Linux distro. Which way will be more efficient.
The most efficient way is most likely to be shared memory. I would follow @Space_C0wb0y advice and use a higher level library like Boost. This would simplify the problem for you.
Nic Strong
Efficiency is unlikely to be that important, despite newbies thinking that it is. Shared memory can be difficult to luse, and I would personally go for a socket-based solution. The use of libraries, boost or otherwise, is of course a good idea.
anon
Named Pipes is nothing that i am looking for, but boost would be ok i will go through its documents.
@cadthecoder: Named pipes seem like the best solution: they are reasonably efficient, easy to use, low overhead, and quite portable (POSIX). Why add the extra bloat that comes with a template library like Boost? It's completely unnecessary to solve the problem at hand. Named pipes would be *the* canonical solution on Linux (or any Unix variant, really), IMHO.
Dan Moulding
+1  A: 

If effeciency is not prime concern then use normal file i/o.

else go for IPC to do so.

As far as Windows is concern you have following options :

Clipboard , COM , Data Copy , DDE , File Mapping , Mailslots , Pipes , RPC , Windows Sockets

For Linux , use can use Name Pipes(efficient) or sockets.

Ashish
+6  A: 

As always, there is a Boost library for that (God, I like Boost).

Space_C0wb0y
A: 

If you're on Windows, you can use Microsoft Message Queueing. This is an example of queue mentioned previously.

jasonline
+3  A: 

Nic has covered all the 4 that I wanted to mention (on the same machine):

  • Sockets
  • Pipes
  • Queues
  • Shared Memory

If writing system calls is troublesome for you, you may want to use the following libraries:

  1. Boost http://www.boost.org/
  2. Poco http://pocoproject.org/blog/
  3. Nokia Qt http://qt.nokia.com/

Something you can read from http://stackoverflow.com/questions/1717540/qt-portable-ipc-only-qsharedmemory

Viet
A: 

If the data to be passed is just a variable, then one of the option is to set it as Environment Variable [ Var1 ] by program #1 and access it, in Program #2 [ if both are running on same env/machine ]. Guess this will be the easiest one, instead of making it complex, by using IPC/socket etc.

Roopesh Majeti
This would be easy, but will only work if the program setting the variable creates the process reading it.
anon
any good reference for the usage of this.
Yes Neil. You are right. I take my statement back. Sorry for that. But i guess, we can kick off prog2 from prog1, in this case, if both are not required to run parellely. Please correct me, if am wrong.
Roopesh Majeti
Ok well its possible to kick off p#2 from p#1 how will this environment variable work.
The following code snippet might help you : #include <iostream> #include <stdlib.h> using namespace std; int main() { putenv("T1=R"); system("checkenv"); } checkenv.cc ----------- #include <iostream> #include <stdlib.h> using namespace std; int main() { cout << getenv("T1") << endl; }
Roopesh Majeti
A: 

I think most of the answers have address the common IPC mechanisms. I'd just like to add that I would probably go for sockets because it's fairly most standard across several platforms. I decided to go for that when I needed to implement IPC that worked both on Symbian Series 60 and Windows Mobile.

The paradigm is straightforward and apart from a few platform glitches, the model worked the same for both platforms. I would also suggest using Protocol Buffers to format the data you send through. Google uses this a lot in its infrastructure. http://code.google.com/p/protobuf/

ruibm
A: 
  • DBUS
  • QtDbus
  • DBus-mm
vitaly.v.ch
A: 

In response to your comment to Roopesh Majeti's answer, here's a very simple example using environment variables:

First program:

// p1.cpp - set the variable
#include <cstdlib>
using namespace std;;    
int main() {
    _putenv( "MYVAR=foobar" );
    system( "p2.exe" );
}

Second program:

// p2.cpp - read the variable
#include <cstdlib>
#include <iostream>
using namespace std;;

int main() {
    char * p = getenv( "MYVAR" );
    if ( p == 0 ) {
     cout << "Not set" << endl;
    }
    else {
     cout << "Value: " << p << endl;
    }
}

Note:

  • there is no standard way of setting an environment variable
  • you will need to construct the name=value string from the variable contents
anon
This is perfect for me. Thank you all for help.
Just remember that environment variables will imply a parent/child relationship between the processes. In this case piping stanard input/output is also another simple solution and also a standard unix way of sharing data.
Nic Strong