passing arguments

hello,
So i have a program A that is sending a string to a another program B and uses that string and pass a string back to the program A.

The code: Program A
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include<cstdlib>
#include<string>

int main(int argc, char* argv[])
{
string passtosystem= "programB.exe"
string word; 

cout<< ""enter your word"; 
getline(cin,word); 

passtosystem=passtosystem+ " " +  word; 
system(passtosystem.c_str()); 

// return from program A 
cout<< "argv[1]; 

return 0; 

}



program B

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include<cstdlib>
#include<string>

using namespace std; 

int main(int argc, char* argv[])
{
string passtosystem= "programA.exe"
string word; 

if(argv[1]=="jx")
   word= "jm";
else
    argv[1]="jj";

word=argv[1]; 
passtosystem=passtosystem+ " " +  word; 
system(passtosystem.c_str()); 

return(0); 
}



The problem I am having is how do i use the word that is passed to the Program B by system(). How do i set it to the argv[] and use it in program B and then send it back.
Last edited on
thanks for replying. So can you give me an example on how I would set word as an argument for
Program A to program B.
What you can do is system((passtosystem + " " + word).c_str(), but I don't think that's really what you want. That will just cause an infinite loop of A running B running a new instance of A running a new instance of B running a new instance of A...

What you probably want is inter-process communication (IPC). There are a number of varieties of this, but the "pipe" sounds like what you want. Other IPCs are semaphores, mutexes, shared memory, message queues, or even using files in the file system.

Flavors of IPCs and the system calls to use them are OS dependent, so one size does not necessarily fit all.
can anyone give me an example on how i would use pipe for this type of program. I am really lost , Please help.

The code above doesn't return the argv[] to the program A and i am not sure why.
I don't program IPC code very often. If you are using Linux, here is a nice chapter discussing it (it's amazing what google will find for you). Section 5.4 discusses pipe. The flavor of pipe you probably want is a fifo, as described in 5.4.5.

http://www.advancedlinuxprogramming.com/alp-folder/alp-ch05-ipc.pdf

For examples of code, somebody else will have to help out.

Side note:

IPC is not rocket science, but it is not trivial, either. If I recall correctly (it's been a number of years), my Operating Systems course in college spent a few weeks covering the subtleties of IPC. I also wrote my Thesis about generating code to handle IPC while avoiding various problems. Unfortunately, I was generating Ada code, and I don't remember the coding details, just the high level concepts.

Various models of IPC challenges have been developed, from the simple Producer / Consumer model (your problem is a variation of this), to the Dining Philosophers model (the deadlock problem) to problems of starvation. So, while cutting and pasting some simple FIFO code from somebody on the forum (sorry, I don't have any available for you) might get you past the immediate problem at hand, there is a bigger realm of IPC that it would behoove you to learn about. You should at least learn the terminology and what the problems are so you are aware of what you are getting into in the future.
Topic archived. No new replies allowed.