Reputation: 187
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.
Upvotes: 2
Views: 5081
Reputation: 1
For a very dirt and completely nonprofessional solution you can do it like me.
Save the variable in to a file and then read it (in an infinite loop every x time) with the other program.
fsexample.open("F:/etc etc ...");
fsexample >> data1 >> data2; // etc etc
and on the other side
fsexample.open("F:/etc etc ...");
fsexample << data1 << data2; // etc etc
The trick is that F
is a virtual drive created with ramdisk so it is fast
and heavy-duty proof.
You could have problem of simultaneous access but you can check it with
if (!fsexample.is_open()) {
fsexample_error = 1;
}
and retry on failure.
Upvotes: 0
Reputation:
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:
Upvotes: 0
Reputation: 11284
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/
Upvotes: 1
Reputation: 554
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.
Upvotes: 1
Reputation: 18404
Nic has covered all the 4 that I wanted to mention (on the same machine):
If writing system calls is troublesome for you, you may want to use the following libraries:
Something you can read from Qt portable IPC: only QSharedMemory?
Upvotes: 5
Reputation: 8529
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.
Upvotes: 2
Reputation: 9056
If you're on Windows, you can use Microsoft Message Queueing. This is an example of queue mentioned previously.
Upvotes: 1
Reputation: 76788
As always, there is a Boost library for that (God, I like Boost).
Upvotes: 7
Reputation: 6592
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:
What is easiest is probably dependent on the platform youa are using.
Upvotes: 9