PDA

View Full Version : Passing commands to other programs: C++



qpwoeiruty
April 7th, 2007, 01:38 AM
How would I go about passing commands from one program to another?

I'd like to be able to start a program and be able to pass commands to it, by typing it into my program, having it execute in the terminal, and having output passed back to me.

Example: I type: "locate gnome" and the output of that command goes into a string back in my program.

duff
April 7th, 2007, 11:30 AM
The popen function would be the easiest way.

qpwoeiruty
April 10th, 2007, 12:38 AM
Thanks! Is there a way to continue entering commands into the pipe or is there a more powerful way to do this? I need the output to not go to the screen at all but to a string or something so that I can parse it eventually.



pid_t pid;
switch(pid = fork()) {
case 0:
pclose(popen("totem file","w"));
//need to write more to console (like totem --pause, etc)
break;
default:
//rest of program
}

j_g
April 10th, 2007, 03:03 AM
There are a few ways of passing data between programs (ie, processes).

The fastest way is to put that data in shared memory (which each process can access). Use shmget() to allocate some shared memory (and also to get access to it within each process). Use shmat() to map the memory into each process. This is similiar to Windows APIs CreateFileMapping and MapViewOfFile, respectively. You may also need to use some signals to synchronize access to the shared memory, if both processes access it simultaneously, or to alert another process that you want it to read the data that you just wrote into the shared memory.

Another way is with message queues. Not as fast as the above, but it does relieve you of dealing with synchronization. One caveat: Each message can transfer no more than 4,056 bytes of data.

Another way is with pipes. This is slower than shared memory, and often involves busy-wait polling if you want the process to be abortable.

If the processes are running on different machines then you may have to resort to something like the sockets library, Corba (ack), or Bonomo.

Go to google and type in "linux interprocess communication" to read numerous articles on the above.

slavik
April 10th, 2007, 03:40 AM
global variables and mutex (between threads)
shared memmory and semaphores (between processes)
pipes (between processes)
sockets (between processes, possibly over network)

qpwoeiruty
April 10th, 2007, 03:51 AM
Wow. TYVM, both of you! I couldn't figure out what to search for. Now I've got some reading to do :)

As it looks so far, difficulty wise, message queues seem the easiest to start with (if I can figure out how to make it do what I want that is ;)), although I'm not up to the section on shared memory yet.

Again, TYVM for the informative posts. I appreciate it and it really helps.

slavik
April 10th, 2007, 03:58 AM
threads are the easiest IMO ... but if you need to call another program, it's not an answer (same with pretty much everything except pipes)

pipes is the only thing which will work universally (as long as the program you need can read from stdin and write to stdout).

another thing that I missed is 'exec'