Console Interaction

I'm working on C++ on CodeBlocks with consoles of application.
I'd like to set the value of a variable from one program in another program (having two programs running simultaneously). Is that possible? If so how?
Thanks for the help, Lellba47.
you can of course, but with command-line parameters
use argc and argv in your both applications and interact between them
use system() to call the application you need
but please note that it's not secure
You may also want to use shared memory. A Linux example:

<http://stackoverflow.com/questions/5656530/how-to-use-shared-memory-with-linux-in-c>
Correct me if I'm wrong but I understand that argc and argv are only useful if you send the data before you run the program or when you first initiate it, I'm trying to get to programs wich are already running to send each other data. I searched a bit and found some stuff about sockets, would that be useful?
> I searched a bit and found some stuff about sockets, would that be useful?

Yes. Some kind of interprocess communication would be required.
I'll keep reading then, Thanks for the help!
btw it talks about connecting the programs trough the internet, could I connect them without using a network, having them running on the same computer?
Last edited on
Everywhere, IP sockets over localhost could be used.
http://compnetworking.about.com/od/workingwithipaddresses/g/127_0_0_1_def.htm

On Unix, another option is to use Unix domain sockets.
http://beej.us/guide/bgipc/output/html/multipage/unixsock.html
Well I ended up using just a text file and writing and reading with both programs, it works perfectly for what I needed an its much easier to set up :P Thanks for the help anyway!
Last edited on
> Well I ended up using just a text file and writing and reading with both programs,
> it works perfectly for what I needed an its much easier to set up

Yes.

Tempfiles

The use of tempfiles as communications drops between cooperating programs is the oldest IPC technique there is. Despite drawbacks, it's still useful in shellscripts, and in one-off programs where a more elaborate and coordinated method of communication would be overkill.

The most obvious problem with using tempfiles as an IPC technique is that it tends to leave garbage lying around if processing is interrupted before the tempfile can be deleted. A less obvious risk is that of collisions between multiple instances of a program using the same name for a tempfile. This is why it is conventional for shellscripts that make tempfiles to include $$ in their names; this shell variable expands to the process-ID of the enclosing shell and effectively guarantees that the filename will be unique (the same trick is supported in Perl).

Finally, if an attacker knows the location to which a tempfile will be written, it can overwrite on that name and possibly either read the producer's data or spoof the consumer process by inserting modified or spurious data into the file.[74] This is a security risk. If the processes involved have root privileges, this is a very serious risk. It can be mitigated by setting the permissions on the tempfile directory carefully, but such arrangements are notoriously likely to spring leaks.

All these problems aside, tempfiles still have a niche because they're easy to set up, they're flexible, and they're less vulnerable to deadlocks or race conditions than more elaborate methods. And sometimes, nothing else will do. The calling conventions of your child process may require that it be handed a file to operate on.

- ESR in "The Art of Unix Programming"
http://catb.org/~esr/writings/taoup/html/ch07s02.html
A very simple way would be to use a temp file to store data they could share.
If they are both sharing data, use 2 files(one for each) so they don't overwrite each other.
If you dont' use 2 files you have to put a file lock in place so that you dont' overwrite a file that has data that hasn't been processed yet.

The other way is to use shared memory, one way might be to use a multi thread program. put your shared data above int main ()
https://solarianprogrammer.com/2011/12/16/cpp-11-thread-tutorial/

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <string.h>

string myshareddata;  // shared data

int main ()
{
// call program 1;
// call program 2;
return 0;
}

Last edited on
Topic archived. No new replies allowed.