passing arguments to a child process in C++
Hi guys
Here I am with yet another problem. This time what I am trying to do is to pass an argument to the standard input stream of a child process. I mean I create two programs .. first one invokes the other. second one contains something like Code:
scanf("%d", &n); Please can someone help me with it?? I am pretty much excited about it and searching in the books of Operating systems that I have didn't give me anything about practically implementing this kinda logic. regards Anuvrat Parashar |
Are you using "fork" to create the child or is it a thread, or are you using "exec"?
If its "fork", then I imagine after you fork, you call some function to "handle" the child. If so, then look into http://linux.die.net/man/2/pipe pipes. After you set up the pipe, you can pass any FDs you need to, to the child function. You can setup the pipe for one-way communication so the parent writes and child reads. |
oh yes.... I did a bit of googling and did come across piping .. it seems to be able to do the thing I want to be done ... just trying to get through with it...
thanks anyways ... will post if I get stuck again :P |
forking is not the solution...
@ nadroj .. the tutorial was really helpful.
What fork does is that it creates a copy of the program its invoked from ... but i want other program to be executed by it. So how do I go about doing it?? execlp or something?? |
When I was learning "fork" and related calls, the basic template would be something like this
Code:
void doChild(int arg1, int arg2, etc) If the child is to call a different program, and not some code that you write, then inside the "doChild" function, yes, you would do whatever "exec"-type of call that makes sense. All of them basically do the "same" thing, which is replace the current process with some other process, usually some command line program. If you need to pass arguments to that command line program, of course it can only be done if the program accepts those arguments. For an unrealistic example, I cant have the child "exec" the "cat" program, and tell it to print some output in bold font, because "cat" of course doesnt take such an argument. If you are trying to send input to the "exec"ed program that the "child" calls, then I think you can use "dup" or "dup2", I forget what the differences are. For another example, say you want the child to run "cat" but have it read from the standard input instead of a file. Also, say you want the input to come from the parent process. To do this, if I recall correctly, basically you do something like this: Code:
// parent creates pipe |
Yes, bhanuvrat - fork(), followed by "exec()".
"exec()" (or friends like "execlp()" allow you to specify the arguments to the child: http://www.yolinux.com/TUTORIALS/ForkExecProcesses.html Command line arguments (argc/argv[]) are one thing; command-line input (stdin and pipes) are a completely different (but related!) thing. 'Hope that helps .. PSM |
ooh yeah... all that helped ... and most importantly the cambridge university page was very lucid and simple to understand
http://www.eng.cam.ac.uk/help/tpl/unix/fork.html well I am through the initial phase of my venture ... and now I need to pass a parameter to a program which does not echo the inputed string eg su ... the program does not accept the string I pass using printf from my parent program. Can you help me out with it? |
Here is something I wrote a while back. Maybe it will help.
https://developer.berlios.de/snippet...ppet&id=100036 Kevin Barry |
All times are GMT -5. The time now is 03:27 PM. |